001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018/* $Id: AbstractObjectReader.java 25099 2010-07-13 23:03:43Z barseghian $ */
019 
020package org.kepler.reporting.rio.fop.tools;
021
022//Java
023import java.io.IOException;
024import java.util.Map;
025
026import org.xml.sax.ContentHandler;
027import org.xml.sax.DTDHandler;
028import org.xml.sax.EntityResolver;
029import org.xml.sax.ErrorHandler;
030import org.xml.sax.InputSource;
031import org.xml.sax.SAXException;
032import org.xml.sax.XMLReader;
033
034/**
035 * This class can be used as base class for XMLReaders that generate SAX 
036 * events from Java objects.
037 */
038
039public abstract class AbstractObjectReader implements XMLReader {
040
041    private static final String NAMESPACES =
042        "http://xml.org/sax/features/namespaces";
043    private static final String NS_PREFIXES =
044        "http://xml.org/sax/features/namespace-prefixes";
045        
046    private Map features = new java.util.HashMap();
047    private ContentHandler orgHandler;
048    
049    /** Proxy for easy SAX event generation */
050    protected EasyGenerationContentHandlerProxy handler;
051    /** Error handler */
052    protected ErrorHandler errorHandler;
053
054
055    /**
056     * Constructor for the AbstractObjectReader object
057     */
058    public AbstractObjectReader() {
059        setFeature(NAMESPACES, false);
060        setFeature(NS_PREFIXES, false);
061    }
062    
063    /* ============ XMLReader interface ============ */
064
065    /**
066     * @see org.xml.sax.XMLReader#getContentHandler()
067     */
068    public ContentHandler getContentHandler() {
069        return this.orgHandler;
070    }
071
072    /**
073     * @see org.xml.sax.XMLReader#setContentHandler(ContentHandler)
074     */
075    public void setContentHandler(ContentHandler handler) {
076        this.orgHandler = handler;
077        this.handler = new EasyGenerationContentHandlerProxy(handler);
078    }
079
080    /**
081     * @see org.xml.sax.XMLReader#getErrorHandler()
082     */
083    public ErrorHandler getErrorHandler() {
084        return this.errorHandler;
085    }
086
087    /**
088     * @see org.xml.sax.XMLReader#setErrorHandler(ErrorHandler)
089     */
090    public void setErrorHandler(ErrorHandler handler) {
091        this.errorHandler = handler;
092    }
093
094    /**
095     * @see org.xml.sax.XMLReader#getDTDHandler()
096     */
097    public DTDHandler getDTDHandler() {
098        return null;
099    }
100
101    /**
102     * @see org.xml.sax.XMLReader#setDTDHandler(DTDHandler)
103     */
104    public void setDTDHandler(DTDHandler handler) {
105    }
106
107    /**
108     * @see org.xml.sax.XMLReader#getEntityResolver()
109     */
110    public EntityResolver getEntityResolver() {
111        return null;
112    }
113
114    /**
115     * @see org.xml.sax.XMLReader#setEntityResolver(EntityResolver)
116     */
117    public void setEntityResolver(EntityResolver resolver) {
118    }
119
120    /**
121     * @see org.xml.sax.XMLReader#getProperty(String)
122     */
123    public Object getProperty(java.lang.String name) {
124        return null;
125    }
126
127    /**
128     * @see org.xml.sax.XMLReader#setProperty(String, Object)
129     */
130    public void setProperty(java.lang.String name, java.lang.Object value) {
131    }
132
133    /**
134     * @see org.xml.sax.XMLReader#getFeature(String)
135     */
136    public boolean getFeature(java.lang.String name) {
137        return ((Boolean) features.get(name)).booleanValue();
138    }
139
140    /**
141     * Returns true if the NAMESPACES feature is enabled.
142     * @return boolean true if enabled
143     */
144    protected boolean isNamespaces() {
145        return getFeature(NAMESPACES);
146    }
147
148    /**
149     * Returns true if the MS_PREFIXES feature is enabled.
150     * @return boolean true if enabled
151     */
152    protected boolean isNamespacePrefixes() {
153        return getFeature(NS_PREFIXES);
154    }
155
156    /**
157     * @see org.xml.sax.XMLReader#setFeature(String, boolean)
158     */
159    public void setFeature(java.lang.String name, boolean value) {
160        this.features.put(name, Boolean.valueOf(value));
161    }
162
163    /**
164     * @see org.xml.sax.XMLReader#parse(String)
165     */
166    public void parse(String systemId) throws IOException, SAXException {
167        throw new SAXException(
168            this.getClass().getName()
169                + " cannot be used with system identifiers (URIs)");
170    }
171
172    /**
173     * @see org.xml.sax.XMLReader#parse(InputSource)
174     */
175    public abstract void parse(InputSource input)
176        throws IOException, SAXException;
177
178}