package com.outline.docdb.jdbc.helpers;

import java.io.IOException;
import org.xml.sax.*;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.base.ErrorCodes;

/**
 * XMLResourceReader is an XMLReader that can be used as input for a JAXP 1.1
 * transformation together with XMLResourceInputSource.
 *
 * @author    Jeremias Märki
 * @created   18. Juli 2001
 * @version   1.0
 * @see       XMLResourceInputSource
 */

public class XMLResourceReader implements XMLReader {

    private ContentHandler handler;
    private ErrorHandler errorHandler;


    public XMLResourceReader() {
    }


    public ContentHandler getContentHandler() {
        return this.handler;
    }


    public void setContentHandler(ContentHandler handler) {
        this.handler = handler;
    }


    public ErrorHandler getErrorHandler() {
        return this.errorHandler;
    }


    public void setErrorHandler(ErrorHandler handler) {
        this.errorHandler = handler;
    }


    public DTDHandler getDTDHandler() {
        return null;
    }


    public void setDTDHandler(DTDHandler handler) {
    }


    public EntityResolver getEntityResolver() {
        return null;
    }


    public void setEntityResolver(EntityResolver resolver) {
    }


    public Object getProperty(java.lang.String name) {
        return null;
    }


    public void setProperty(java.lang.String name, java.lang.Object value) {
    }


    public boolean getFeature(java.lang.String name) {
        return false;
    }


    public void setFeature(java.lang.String name, boolean value) {
    }


    public void parse(String systemId) throws IOException, SAXException {
        throw new SAXException(this.getClass().getName()+" cannot be used with system identifiers (URIs)");
    }


    public void parse(InputSource input) throws IOException, SAXException {
        if (input instanceof XMLResourceInputSource) {
            parse((XMLResourceInputSource)input);
        } else throw new SAXException(this.getClass().getName()+" can only work with XMLResourceInputSource");
    }


    public void parse(XMLResourceInputSource input) throws IOException, SAXException {
        XMLResource xres = input.getXMLResource();
        if (xres == null) throw new SAXException("No input document (XMLResource) available");

        if (handler == null)
                throw new SAXException("No content handler available");

        try {
            xres.getContentAsSAX(handler);
        } catch (XMLDBException e) {
            throw new SAXException("Error while accessing XMLResource: "+e.getMessage()+" ("+e.errorCode+")", e);
        }
    }



}
