package com.dbxml.xml.dom;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: DOMTableGenerator.java,v 1.4 2006/02/02 19:04:15 bradford Exp $
*/
import org.w3c.dom.*;
import com.dbxml.util.UTF8;
import com.dbxml.xml.QName;
import com.dbxml.xml.SymbolTable;
import com.dbxml.xml.dtsm.*;
/**
* DOMTableGenerator
*/
public final class DOMTableGenerator extends TableGenerator {
private TableBuilder builder;
private Node node;
public DOMTableGenerator(Node node) {
this.node = node;
}
public DocumentTable process() throws DTSMException {
if ( symbols == null )
symbols = new SymbolTable();
builder = new TableBuilder(symbols);
process(node);
return builder.buildDocumentTable();
}
private void process(Node node) {
switch ( node.getNodeType() ) {
case Node.DOCUMENT_NODE:
documentNode((Document)node);
break;
case Node.ELEMENT_NODE:
elementNode((Element)node);
break;
case Node.ATTRIBUTE_NODE:
attributeNode((Attr)node);
break;
case Node.TEXT_NODE:
textNode((Text)node);
break;
case Node.COMMENT_NODE:
commentNode((Comment)node);
break;
case Node.CDATA_SECTION_NODE:
cDataSectionNode((CDATASection)node);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
procInstNode((ProcessingInstruction)node);
break;
case Node.DOCUMENT_FRAGMENT_NODE:
documentFragmentNode((DocumentFragment)node);
break;
case Node.DOCUMENT_TYPE_NODE:
documentTypeNode((DocumentType)node);
break;
case Node.NOTATION_NODE:
notationNode((Notation)node);
break;
case Node.ENTITY_NODE:
entityNode((Entity)node);
break;
case Node.ENTITY_REFERENCE_NODE:
entityReferenceNode((EntityReference)node);
break;
}
}
private void processNamedNodeMap(NamedNodeMap nm) {
for ( int i = 0; i < nm.getLength(); i++ )
process(nm.item(i));
}
private void processNodeList(NodeList nl) {
for ( int i = 0; i < nl.getLength(); i++ )
process(nl.item(i));
}
private void documentNode(Document doc) {
builder.addEntry(Constants.OBJ_BEGIN_DOCUMENT);
processNodeList(doc.getChildNodes());
builder.addEntry(Constants.OBJ_END_DOCUMENT);
}
private void elementNode(Element elem) {
String name = elem.getTagName();
String uri = elem.getNamespaceURI();
QName q = new QName(name, uri);
int symID = builder.addEntry(Constants.OBJ_BEGIN_ELEMENT, q);
processNamedNodeMap(elem.getAttributes());
processNodeList(elem.getChildNodes());
builder.addEntry(Constants.OBJ_END_ELEMENT, symID);
}
private void attributeNode(Attr attr) {
String name = attr.getName();
String uri = attr.getNamespaceURI();
QName q = new QName(name, uri);
int symID = builder.addEntry(Constants.OBJ_BEGIN_ATTRIBUTE, q);
processNodeList(attr.getChildNodes());
builder.addEntry(Constants.OBJ_END_ATTRIBUTE, symID);
}
private void textNode(Text text) {
String t = text.getData();
byte[] b = UTF8.toUTF8(t);
builder.addEntry(Constants.OBJ_TEXT, b);
}
private void commentNode(Comment comment) {
String t = comment.getData();
byte[] b = UTF8.toUTF8(t);
builder.addEntry(Constants.OBJ_COMMENT, b);
}
private void cDataSectionNode(CDATASection cData) {
String t = cData.getData();
byte[] b = UTF8.toUTF8(t);
builder.addEntry(Constants.OBJ_CDATA, b);
}
private void procInstNode(ProcessingInstruction procInst) {
String name = procInst.getTarget();
String uri = procInst.getNamespaceURI();
QName q = new QName(name, uri);
int symID = builder.addEntry(Constants.OBJ_BEGIN_PROCINST, q);
String v = procInst.getData();
byte[] b = UTF8.toUTF8(v);
builder.addEntry(Constants.OBJ_TEXT, b);
builder.addEntry(Constants.OBJ_END_PROCINST, symID);
}
private void documentFragmentNode(DocumentFragment fragment) {
processNodeList(fragment.getChildNodes());
}
private void documentTypeNode(DocumentType docType) {
/** @todo Still have to work this out */
}
private void notationNode(Notation notation) {
QName qname = new QName(notation.getNodeName());
int symID = builder.addEntry(Constants.OBJ_BEGIN_NOTATION, qname);
String publicID = notation.getPublicId();
byte[] b = UTF8.toUTF8(publicID);
builder.addEntry(Constants.OBJ_TEXT, b);
String systemID = notation.getSystemId();
b = UTF8.toUTF8(systemID);
builder.addEntry(Constants.OBJ_TEXT, b);
builder.addEntry(Constants.OBJ_END_NOTATION, symID);
}
private void entityNode(Entity entity) {
QName qname = new QName(entity.getNodeName());
int symID = builder.addEntry(Constants.OBJ_BEGIN_ENTITY, qname);
String publicID = entity.getPublicId();
byte[] b = UTF8.toUTF8(publicID);
builder.addEntry(Constants.OBJ_TEXT, b);
String systemID = entity.getSystemId();
b = UTF8.toUTF8(systemID);
builder.addEntry(Constants.OBJ_TEXT, b);
String notation = entity.getNotationName();
b = UTF8.toUTF8(notation);
builder.addEntry(Constants.OBJ_TEXT, b);
builder.addEntry(Constants.OBJ_END_ENTITY, symID);
}
private void entityReferenceNode(EntityReference entityRef) {
/** @todo Sorta at a loss of what to do here */
}
}