package org.apache.cocoon.transformation;

import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.Map;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.transformation.AbstractTransformer;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.apache.excalibur.source.SourceValidity;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;


public class DigesterTransformer
    extends AbstractTransformer
{

    private Map            objectModel;
    private SourceResolver resolver;
    private Digester       digester;

    /**
     * DOCUMENT ME!
     * 
     * @param resolver DOCUMENT ME!
     * @param objectModel DOCUMENT ME!
     * @param src DOCUMENT ME!
     * @param params DOCUMENT ME!
     * @throws org.apache.cocoon.ProcessingException DOCUMENT ME!
     * @throws org.xml.sax.SAXException DOCUMENT ME!
     * @throws java.io.IOException DOCUMENT ME!
     */
    public void setup(SourceResolver resolver, Map objectModel, String src, 
                      Parameters params)
               throws ProcessingException, SAXException, IOException
    {
        this.objectModel = objectModel;
        this.resolver = resolver;

        // Configure the digester using an XML rule file
        try{
           String fileName = params.getParameter("rules", "digester.xml");

	       String file = resolver.resolveURI(fileName).getSystemId();
           URL url = new URL(file);

           digester = DigesterLoader.createDigester(url);
        }
        catch(Exception e){
          getLogger().error("Could not read digester rules",e);
        }
        // TODO: Configure a task class that is executed when the digester finishes
    }

    // Configurable
    //    public void configure(Configuration configuration) throws ConfigurationException {
    //
    //    }

    // Initializable
    // public void initialize()
    //                    throws Exception
    //{
    //}

    /**
     * Receive notification of the beginning of a document.
     */
    public void startDocument()
                       throws SAXException
    {
        digester.startDocument();
    }

    /**
     * Receive notification of the end of a document.
     */
    public void endDocument()
                     throws SAXException
    {
        digester.endDocument();
    }

    /**
     * Begin the scope of a prefix-URI Namespace mapping.
     * 
     * @param prefix The Namespace prefix being declared.
     * @param uri The Namespace URI the prefix is mapped to.
     */
    public void startPrefixMapping(String prefix, String uri)
                            throws SAXException
    {
        digester.startPrefixMapping(prefix, uri);
    }

    /**
     * End the scope of a prefix-URI mapping.
     * 
     * @param prefix The prefix that was being mapping.
     */
    public void endPrefixMapping(String prefix)
                          throws SAXException
    {
        digester.endPrefixMapping(prefix);
    }

    /**
     * Receive notification of the beginning of an element.
     * 
     * @param uri The Namespace URI, or the empty string if the element has no
     *        Namespace URI or if Namespace processing is not being
     *        performed.
     * @param loc The local name (without prefix), or the empty string if
     *        Namespace processing is not being performed.
     * @param raw The raw XML 1.0 name (with prefix), or the empty string if
     *        raw names are not available.
     * @param a The attributes attached to the element. If there are no
     *        attributes, it shall be an empty Attributes object.
     */
    public void startElement(String uri, String loc, String raw, Attributes a)
                      throws SAXException
    {
        digester.startElement(uri, loc, raw, a);
    }

    /**
     * Receive notification of the end of an element.
     * 
     * @param uri The Namespace URI, or the empty string if the element has no
     *        Namespace URI or if Namespace processing is not being
     *        performed.
     * @param loc The local name (without prefix), or the empty string if
     *        Namespace processing is not being performed.
     * @param raw The raw XML 1.0 name (with prefix), or the empty string if
     *        raw names are not available.
     */
    public void endElement(String uri, String loc, String raw)
                    throws SAXException
    {
        digester.endElement(uri, loc, raw);
    }

    /**
     * Receive notification of character data.
     * 
     * @param c The characters from the XML document.
     * @param start The start position in the array.
     * @param len The number of characters to read from the array.
     */
    public void characters(char[] c, int start, int len)
                    throws SAXException
    {
        digester.characters(c, start, len);
    }

    /**
     * Receive notification of ignorable whitespace in element content.
     * 
     * @param c The characters from the XML document.
     * @param start The start position in the array.
     * @param len The number of characters to read from the array.
     */
    public void ignorableWhitespace(char[] c, int start, int len)
                             throws SAXException
    {
        digester.ignorableWhitespace(c, start, len);
    }

    /**
     * Receive notification of a processing instruction.
     * 
     * @param target The processing instruction target.
     * @param data The processing instruction data, or null if none was
     *        supplied.
     */
    public void processingInstruction(String target, String data)
                               throws SAXException
    {
        digester.processingInstruction(target, data);
    }

    /**
     * Receive notification of a skipped entity.
     * 
     * @param name The name of the skipped entity.  If it is a  parameter
     *        entity, the name will begin with '%'.
     */
    public void skippedEntity(String name)
                       throws SAXException
    {
        digester.skippedEntity(name);
    }

    /**
     * Report the start of DTD declarations, if any.
     * 
     * @param name The document type name.
     * @param publicId The declared public identifier for the external DTD
     *        subset, or null if none was declared.
     * @param systemId The declared system identifier for the external DTD
     *        subset, or null if none was declared.
     */
    public void startDTD(String name, String publicId, String systemId)
                  throws SAXException
    {

        // Unknown in digester?
        //       digester.startDTD(name,publicId,systemId);
    }

    /**
     * Report the end of DTD declarations.
     */
    public void endDTD()
                throws SAXException
    {

        // Unknown in digester?
        //       digester.endDTD();
    }

    /**
     * Report the beginning of an entity.
     * 
     * @param name The name of the entity. If it is a parameter entity, the
     *        name will begin with '%'.
     */
    public void startEntity(String name)
                     throws SAXException
    {

        // Unknown in digester?
        //      digester.startEntity(name);
    }

    /**
     * Report the end of an entity.
     * 
     * @param name The name of the entity that is ending.
     */
    public void endEntity(String name)
                   throws SAXException
    {

        // Unknown in digester?
        //      digester.endEntity(name);
    }

    /**
     * Report the start of a CDATA section.
     */
    public void startCDATA()
                    throws SAXException
    {

        // Unknown in digester?
        //     digester.startCDATA();
    }

    /**
     * Report the end of a CDATA section.
     */
    public void endCDATA()
                  throws SAXException
    {

        // Unknown in digester?
        //      digester.endCDATA();
    }

    /**
     * Report an XML comment anywhere in the document.
     * 
     * @param ch An array holding the characters in the comment.
     * @param start The starting position in the array.
     * @param len The number of characters to use from the array.
     */
    public void comment(char[] ch, int start, int len)
                 throws SAXException
    {

        //    Unknown in digester?
        //    digester.comment(ch,start,len);
    }
}
