/*
 * $Id: DomProducer.java,v 1.1.1.1 2002/09/30 15:08:51 smartine Exp $
 * Copyright (C) 1999-2000 David Brownell
 * 
 * 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
 */

package xml.pipeline;

import java.io.IOException;

import org.w3c.dom.Node;
import org.xml.sax.*;
import org.xml.sax.ext.*;

import xml.*;


// $Id: DomProducer.java,v 1.1.1.1 2002/09/30 15:08:51 smartine Exp $

/**
 * A SAX event source generated from a DOM node, normally a document.  This
 * producer can send multiple DOM nodes down an event pipeline after it has
 * been established.
 *
 * <p> Note that if you want to turn a DOM tree into XML text, you
 * should as a rule send output first through a filter to restore XML
 * namespace syntax.  With DOM, it is highly likely that such information
 * will be discarded at some point in processing, and printing XML text
 * often requires that elements should use and declare prefixes.
 *
 * @see NSFilter
 *
 * @author David Brownell
 * @version $Date: 2002/09/30 15:08:51 $
 */
public class DomProducer extends EventProducer
    // won't extend it for much longer ...
{
    private DomParser2		parser;

    /**
     * Produces a series of events corresponding to the appropriate
     * traversal of the specified DOM node.  If the node isn't a
     * document, startDocument and endDocument calls won't be made.
     */
    public DomProducer (Node start)
    {
	this (new DomParser2 (start));
    }

    private DomProducer (DomParser2 parser)
    {
	super (parser, new InputSource ("doesn't matter"));
	this.parser = parser;
    }

    /**
     * Not supported for this kind of event producer; always throws an
     * exception.
     */
    public void parseDocument (InputSource source)
    throws SAXException, IOException
    {
	throw new IllegalArgumentException ("need DOM Document");
    }

    // overrides superclass method
    void parseDocument ()
    throws SAXException, IOException
    {
	parser.parse ("doesn't matter");
    }

    public void produce (EventConsumer out)
    throws SAXException
    {
	try {
	    super.produce (out);
	} catch (IOException e) {
	    throw new SAXException ("can't happen!", e);
	}
    }

    /**
     * Start firing events to the next stage in the pipeline, returning
     * when the events for the specified node are all reported.  If the
     * node isn't a document, then startDocument and endDocument won't
     * be invoked.
     */
    public void parseDocument (Node source)
    throws SAXException, IOException
    {
	parser.setProperty ("dom-node", source);
	parser.parse ("doesn't matter");
    }
}
