package com.dbxml.xml.dtsm;

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

import com.dbxml.xml.SymbolTable;
import java.util.HashMap;
import java.util.Map;

/**
 * Producer
 */

public class Producer {
   protected Map props = new HashMap();
   protected PullProcessor processor;
   protected SymbolTable symbols;
   protected DocumentTable table;

   /**
    * setProperty sets a named property value for this Producer.
    *
    * @param name the property name
    * @param value the property value
    */
   public void setProperty(String name, Object value) {
      props.put(name, value);
   }

   /**
    * getProperty returns a named property value for this Producer.
    *
    * @param name the property name
    * @return the property value
    */
   public Object getProperty(String name) {
      return props.get(name);
   }

   /**
    * getDocumentTable returns the DocumentTable that this Producer
    * will use to generate an alternative representation.
    *
    * @return the DocumentTable
    */
   public DocumentTable getDocumentTable() {
      return table;
   }

   /**
    * setDocumentTable sets the DocumentTable that this Producer
    * will use to generate an alternative representation.
    *
    * @param table the DocumentTable
    */
   public void setDocumentTable(DocumentTable table) {
      this.table = table;
      symbols = table.getSymbolTable();
   }

   /**
    * process effectively kicks off the process that will produce
    * the alternative representation of the associated DocumentTable.
    * This form of the method will check the current instance to see
    * if it implements Consumer, and then call process against itself.
    */
   public final void process() throws Exception {
      if ( this instanceof Consumer )
         process((Consumer)this);
      else
         throw new DTSMException("This Object is not a Consumer");
   }

   /**
    * process effectively kicks off the process that will produce
    * the alternative representation of the associated DocumentTable.
    *
    * @param consumer The Consumer to target for this processing
    */
   public void process(Consumer consumer) throws Exception {
      processor = new PullProcessor(table);
      while ( processor.hasNext() ) {
         DocumentTableEntry entry = processor.next();
         switch ( entry.getTypeID() ) {
            case Constants.OBJ_BEGIN_DOCUMENT:
               consumer.beginDocument(entry);
               break;

            case Constants.OBJ_END_DOCUMENT:
               consumer.endDocument(entry);
               break;

            case Constants.OBJ_BEGIN_ELEMENT:
               consumer.beginElement(entry);
               break;

            case Constants.OBJ_END_ELEMENT:
               consumer.endElement(entry);
               break;

            case Constants.OBJ_BEGIN_PROCINST:
               consumer.procInst(entry);
               break;

            case Constants.OBJ_TEXT:
               consumer.text(entry);
               break;

            case Constants.OBJ_CDATA:
               consumer.cdata(entry);
               break;

            case Constants.OBJ_COMMENT:
               consumer.comment(entry);
               break;

            case Constants.OBJ_BEGIN_DOCTYPE:
               consumer.docType(entry);
               break;

            case Constants.OBJ_BEGIN_ENTITY:
               consumer.entity(entry);
               break;

            case Constants.OBJ_ENTITY_REF:
               consumer.entityRef(entry);
               break;

            case Constants.OBJ_BEGIN_NOTATION:
               consumer.notation(entry);
               break;
         }
      }
   }
}
