package com.dbxml.xml.dom;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: DOMTableGenerator.java,v 1.3 2004/02/12 00:17:58 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 */
   }
}
