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: DOMProducer.java,v 1.5 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.dtsm.Constants;
import com.dbxml.xml.dtsm.Consumer;
import com.dbxml.xml.dtsm.DocumentTable;
import com.dbxml.xml.dtsm.DocumentTableEntry;
import com.dbxml.xml.dtsm.Producer;
import java.util.Stack;

/**
 * DOMProducer
 */

public final class DOMProducer extends Producer implements Consumer {
   private Stack stack = new Stack();
   private Document doc = DOMHelper.newDocument();
   private Node node;
   private Node context;

   public DOMProducer(DocumentTable table) {
      setDocumentTable(table);
   }

   public Node getNode() {
      return node;
   }

   public void beginDocument(DocumentTableEntry entry) {
      if ( node == null )
         node = doc;
      pushContext(doc);
   }

   public void endDocument(DocumentTableEntry entry) {
      popContext();
   }

   public void beginElement(DocumentTableEntry entry) {
      int symID = entry.getValID();
      QName qname = symbols.getQName(symID);
      String uri = qname.getURI();

      Element elem;
      if ( uri != null )
         elem = doc.createElementNS(uri, qname.getName());
      else
         elem = doc.createElement(qname.getName());

      if ( node == null )
         node = elem;
      if ( context != null )
         context.appendChild(elem);

      pushContext(elem);

      while ( processor.peek().getTypeID() == Constants.OBJ_BEGIN_ATTRIBUTE )
         attribute(processor.next());
   }

   public void endElement(DocumentTableEntry entry) {
      popContext();
   }

   public void attribute(DocumentTableEntry entry) {
      int symID = entry.getValID();
      QName qname = symbols.getQName(symID);
      String uri = qname.getURI();
      StringBuffer sb = new StringBuffer();
      do {
         entry = processor.next();
         if ( entry.getTypeID() == Constants.OBJ_TEXT ) {
            int valID = entry.getValID();
            sb.append(UTF8.toCharArray(table.getValue(valID)));
         }
      }
      while ( entry.getTypeID() != Constants.OBJ_END_ATTRIBUTE );

      Element elem = (Element)context;
      if ( uri != null )
         elem.setAttributeNS(uri, qname.getName(), sb.toString());
      else
         elem.setAttribute(qname.getName(), sb.toString());
   }

   public void procInst(DocumentTableEntry entry) {
      int symID = entry.getValID();
      QName qname = symbols.getQName(symID);
      entry = processor.next();
      int valID = entry.getValID();
      String data = table.getValue(valID).toString();
      processor.next(); // Eat the OBJ_END_PROCINST
      ProcessingInstruction pi = doc.createProcessingInstruction(qname.getName(), data);
      if ( node == null )
         node = pi;
      if ( context != null )
         context.appendChild(pi);
   }

   public void text(DocumentTableEntry entry) {
      int valID = entry.getValID();
      String data = table.getValue(valID).toString();
      Text text = doc.createTextNode(data);
      if ( node == null )
         node = text;
      if ( context != null )
         context.appendChild(text);
   }

   public void cdata(DocumentTableEntry entry) {
      int valID = entry.getValID();
      String data = table.getValue(valID).toString();
      CDATASection cdata = doc.createCDATASection(data);
      if ( node == null )
         node = cdata;
      if ( context != null )
         context.appendChild(cdata);
   }

   public void comment(DocumentTableEntry entry) {
      int valID = entry.getValID();
      String data = table.getValue(valID).toString();
      Comment comment = doc.createComment(data);
      if ( node == null )
         node = comment;
      if ( context != null )
         context.appendChild(comment);
   }

   public void entity(DocumentTableEntry entry) {
      int symID = entry.getValID();
      String name = symbols.getQName(symID).getName();
      entry = processor.next(); // Get Public ID
      int valID = entry.getValID();
      String publicID = table.getValue(valID).toString();
      entry = processor.next(); // Get System ID
      valID = entry.getValID();
      String systemID = table.getValue(valID).toString();
      entry = processor.next(); // Get Notation Name
      valID = entry.getValID();
      String notation = table.getValue(valID).toString();
      processor.next(); // Eat the OBJ_END_ENTITY
      /** @todo Figure out how to create an Entity node with DOM */
   }

   public void notation(DocumentTableEntry entry) {
      int symID = entry.getValID();
      String name = symbols.getQName(symID).getName();
      entry = processor.next(); // Get Public ID
      int valID = entry.getValID();
      String publicID = table.getValue(valID).toString();
      entry = processor.next(); // Get System ID
      valID = entry.getValID();
      String systemID = table.getValue(valID).toString();
      processor.next(); // Eat the OBJ_END_NOTATION
      /** @todo Figure out how to create a Notation node with DOM */
   }

   public void docType(DocumentTableEntry entry) {
      processor.next(); // Eat the OBJ_END_DOCTYPE
   }

   public void entityRef(DocumentTableEntry entry) {
      /** @todo This */
   }

   private void pushContext(Node node) {
      stack.push(context);
      context = node;
   }

   private void popContext() {
      context = (Node)stack.pop();
   }
}
