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 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding.tools;
021:
022: //Java
023: import java.io.IOException;
024: import java.util.Map;
025:
026: //SAX
027: import org.xml.sax.SAXException;
028: import org.xml.sax.InputSource;
029: import org.xml.sax.XMLReader;
030: import org.xml.sax.ContentHandler;
031: import org.xml.sax.DTDHandler;
032: import org.xml.sax.ErrorHandler;
033: import org.xml.sax.EntityResolver;
034:
035: /**
036: * This class can be used as base class for XMLReaders that generate SAX
037: * events from Java objects.
038: */
039:
040: public abstract class AbstractObjectReader implements XMLReader {
041:
042: private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
043: private static final String NS_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
044:
045: private Map features = new java.util.HashMap();
046: private ContentHandler orgHandler;
047:
048: /** Proxy for easy SAX event generation */
049: protected EasyGenerationContentHandlerProxy handler;
050: /** Error handler */
051: protected ErrorHandler errorHandler;
052:
053: /**
054: * Constructor for the AbstractObjectReader object
055: */
056: public AbstractObjectReader() {
057: setFeature(NAMESPACES, false);
058: setFeature(NS_PREFIXES, false);
059: }
060:
061: /* ============ XMLReader interface ============ */
062:
063: /**
064: * @see org.xml.sax.XMLReader#getContentHandler()
065: */
066: public ContentHandler getContentHandler() {
067: return this .orgHandler;
068: }
069:
070: /**
071: * @see org.xml.sax.XMLReader#setContentHandler(ContentHandler)
072: */
073: public void setContentHandler(ContentHandler handler) {
074: this .orgHandler = handler;
075: this .handler = new EasyGenerationContentHandlerProxy(handler);
076: }
077:
078: /**
079: * @see org.xml.sax.XMLReader#getErrorHandler()
080: */
081: public ErrorHandler getErrorHandler() {
082: return this .errorHandler;
083: }
084:
085: /**
086: * @see org.xml.sax.XMLReader#setErrorHandler(ErrorHandler)
087: */
088: public void setErrorHandler(ErrorHandler handler) {
089: this .errorHandler = handler;
090: }
091:
092: /**
093: * @see org.xml.sax.XMLReader#getDTDHandler()
094: */
095: public DTDHandler getDTDHandler() {
096: return null;
097: }
098:
099: /**
100: * @see org.xml.sax.XMLReader#setDTDHandler(DTDHandler)
101: */
102: public void setDTDHandler(DTDHandler handler) {
103: }
104:
105: /**
106: * @see org.xml.sax.XMLReader#getEntityResolver()
107: */
108: public EntityResolver getEntityResolver() {
109: return null;
110: }
111:
112: /**
113: * @see org.xml.sax.XMLReader#setEntityResolver(EntityResolver)
114: */
115: public void setEntityResolver(EntityResolver resolver) {
116: }
117:
118: /**
119: * @see org.xml.sax.XMLReader#getProperty(String)
120: */
121: public Object getProperty(java.lang.String name) {
122: return null;
123: }
124:
125: /**
126: * @see org.xml.sax.XMLReader#setProperty(String, Object)
127: */
128: public void setProperty(java.lang.String name,
129: java.lang.Object value) {
130: }
131:
132: /**
133: * @see org.xml.sax.XMLReader#getFeature(String)
134: */
135: public boolean getFeature(java.lang.String name) {
136: return ((Boolean) features.get(name)).booleanValue();
137: }
138:
139: /**
140: * Returns true if the NAMESPACES feature is enabled.
141: * @return boolean true if enabled
142: */
143: protected boolean isNamespaces() {
144: return getFeature(NAMESPACES);
145: }
146:
147: /**
148: * Returns true if the MS_PREFIXES feature is enabled.
149: * @return boolean true if enabled
150: */
151: protected boolean isNamespacePrefixes() {
152: return getFeature(NS_PREFIXES);
153: }
154:
155: /**
156: * @see org.xml.sax.XMLReader#setFeature(String, boolean)
157: */
158: public void setFeature(java.lang.String name, boolean value) {
159: this .features.put(name, new Boolean(value));
160: }
161:
162: /**
163: * @see org.xml.sax.XMLReader#parse(String)
164: */
165: public void parse(String systemId) throws IOException, SAXException {
166: throw new SAXException(this .getClass().getName()
167: + " cannot be used with system identifiers (URIs)");
168: }
169:
170: /**
171: * @see org.xml.sax.XMLReader#parse(InputSource)
172: */
173: public abstract void parse(InputSource input) throws IOException,
174: SAXException;
175:
176: }
|