package com.dbxml.xml.sax;

/*
 * 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: SAXProducer.java,v 1.7 2004/02/12 00:17:58 bradford Exp $
 */

import java.util.*;
import org.xml.sax.*;

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 org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.AttributesImpl;

/**
 * SAXProducer
 */

public final class SAXProducer extends Producer implements Consumer {
   private static final AttributesImpl EmptyAttributes = new AttributesImpl();

   private Set features = new HashSet();
   private ContentHandler content;
   private ErrorHandler errors;
   private EntityResolver entities;
   private DTDHandler dtd;
   private LexicalHandler lex;
   private boolean nsReporting;
   private boolean nsPrefixes;
   private Stack prefixStack; // of Map
   private AttributesImpl attrs;

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

   public SAXProducer(DocumentTable table, ContentHandler handler) {
      setDocumentTable(table);
      setContentHandler(handler);
   }

   public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
      return features.contains(name);
   }

   public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
      if ( value ) {
         if ( !features.contains(name) )
            features.add(name);
      }
      else {
         if ( features.contains(name) )
            features.remove(name);
      }
   }

   public void setProperty(String name, Object value) {
      super.setProperty(name, value);
      if ( name.equals("http://xml.org/sax/properties/lexical-handler") ) {
         if ( value instanceof LexicalHandler )
            lex = (LexicalHandler)value;
         else
            lex = null;
      }
   }

   public void setEntityResolver(EntityResolver resolver) {
      entities = resolver;
   }

   public EntityResolver getEntityResolver() {
      return entities;
   }

   public void setDTDHandler(DTDHandler handler) {
      dtd = handler;
   }

   public DTDHandler getDTDHandler() {
      return dtd;
   }

   public void setContentHandler(ContentHandler handler) {
      content = handler;
   }

   public ContentHandler getContentHandler() {
      return content;
   }

   public void setErrorHandler(ErrorHandler handler) {
      errors = handler;
   }

   public ErrorHandler getErrorHandler() {
      return errors;
   }

   public void process(Consumer consumer) throws Exception {
      nsReporting = features.contains("http://xml.org/sax/features/namespaces");
      nsPrefixes = features.contains("http://xml.org/sax/features/namespace-prefixes");

      if ( nsReporting )
         prefixStack = new Stack();

      super.process(consumer);
   }

   public void beginDocument(DocumentTableEntry entry) {
   }

   public void endDocument(DocumentTableEntry entry) {
   }

   public void beginElement(DocumentTableEntry entry) throws Exception {
      if ( nsReporting )
         prefixStack.push(new HashMap());

      int symID = entry.getValID();
      QName qname = symbols.getQName(symID);
      String uri = qname.getSAXURI();

      if ( processor.peek().getTypeID() == Constants.OBJ_BEGIN_ATTRIBUTE )
         attrs = new AttributesImpl();
      else
         attrs = EmptyAttributes;

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

      content.startElement(uri, qname.getLocalName(), qname.getName(), attrs);
   }

   public void endElement(DocumentTableEntry entry) throws Exception {
      int symID = entry.getValID();
      QName qname = symbols.getQName(symID);
      String uri = qname.getSAXURI();
      content.endElement(uri, qname.getLocalName(), qname.getName());

      if ( nsReporting ) {
         Map prefixMap = (Map)prefixStack.pop();
         if ( prefixMap.size() > 0 ) {
            Iterator keys = prefixMap.keySet().iterator();
            while ( keys.hasNext() )
               content.endPrefixMapping((String)keys.next());
         }
      }
   }

   public void attribute(DocumentTableEntry entry) throws Exception {
      int symID = entry.getValID();
      QName qname = symbols.getQName(symID);
      String uri = qname.getSAXURI();
      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 );

      String value = sb.toString();

      if ( nsReporting && qname.isNamespaceDecl() ) {
         Map prefixMap = (Map)prefixStack.peek();

         String prefix = qname.getSAXPrefix();

         prefixMap.put(prefix, value);
         content.startPrefixMapping(prefix, value);

         if ( nsPrefixes )
            attrs.addAttribute(uri, qname.getLocalName(), qname.getName(), "", value);
      }
      else
         attrs.addAttribute(uri, qname.getLocalName(), qname.getName(), "", value);
   }

   public void procInst(DocumentTableEntry entry) throws Exception {
      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
      content.processingInstruction(qname.getName(), data);
   }

   public void text(DocumentTableEntry entry) throws Exception {
      int valID = entry.getValID();
      char[] data = UTF8.toCharArray(table.getValue(valID));
      content.characters(data, 0, data.length);
   }

   public void cdata(DocumentTableEntry entry) throws Exception {
      int valID = entry.getValID();
      char[] data = UTF8.toCharArray(table.getValue(valID));
      if ( lex != null ) {
         lex.startCDATA();
         content.characters(data, 0, data.length);
         lex.endCDATA();
      }
      else
         content.characters(data, 0, data.length);
   }

   public void comment(DocumentTableEntry entry) throws Exception {
      int valID = entry.getValID();
      char[] data = UTF8.toCharArray(table.getValue(valID));
      if ( lex != null )
         lex.comment(data, 0, data.length);
   }

   public void entity(DocumentTableEntry entry) throws Exception {
      int symID = entry.getValID();
      String name = symbols.getQName(symID).getName();
      entry = processor.next(); // Get Public ID
      int pubID = entry.getValID();
      entry = processor.next(); // Get System ID
      int sysID = entry.getValID();
      entry = processor.next(); // Get Notation Name
      int notID = entry.getValID();
      processor.next(); // Eat the OBJ_END_ENTITY
      if ( dtd != null ) {
         String publicID = table.getValue(pubID).toString();
         String systemID = table.getValue(sysID).toString();
         String notation = table.getValue(notID).toString();
         dtd.unparsedEntityDecl(name, publicID, systemID, notation);
      }
   }

   public void notation(DocumentTableEntry entry) throws Exception {
      int symID = entry.getValID();
      String name = symbols.getQName(symID).getName();
      entry = processor.next(); // Get Public ID
      int pubID = entry.getValID();
      entry = processor.next(); // Get System ID
      int sysID = entry.getValID();
      processor.next(); // Eat the OBJ_END_NOTATION
      if ( dtd != null ) {
         String publicID = table.getValue(pubID).toString();
         String systemID = table.getValue(sysID).toString();
         dtd.notationDecl(name, publicID, systemID);
      }
   }

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

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