Package com.dbxml.xml.dom

Source Code of com.dbxml.xml.dom.DOMProducer

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: DOMProducer.java,v 1.6 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.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();
   }
}
TOP

Related Classes of com.dbxml.xml.dom.DOMProducer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.