001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------
028: * FrontendDefaultHandler.java
029: * ------------
030: * (C) Copyright 2002-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: FrontendDefaultHandler.java,v 1.8 2006/11/20 21:36:30 taqua Exp $
036: *
037: * Changes
038: * -------
039: * 02-Feb-2005 : Initial version.
040: *
041: */
042: package org.jfree.xml;
043:
044: import java.net.MalformedURLException;
045: import java.net.URL;
046: import java.util.Iterator;
047: import java.util.Enumeration;
048:
049: import org.jfree.util.Configuration;
050: import org.jfree.util.DefaultConfiguration;
051: import org.xml.sax.Locator;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.helpers.DefaultHandler;
054:
055: /**
056: * The frontenddefault handler connects the SAX-backend with the handler implementations.
057: * It must be the base class for all parser implementations used by the ParserFrontEnd.
058: *
059: * @author Thomas Morgner
060: */
061: public abstract class FrontendDefaultHandler extends DefaultHandler
062: implements Configuration {
063: /**
064: * A key for the content base.
065: */
066: public static final String CONTENTBASE_KEY = "content-base";
067:
068: /**
069: * Storage for the parser configuration.
070: */
071: private DefaultConfiguration parserConfiguration;
072:
073: /**
074: * The DocumentLocator can be used to resolve the current parse position.
075: */
076: private Locator locator;
077:
078: /**
079: * The current comment handler used to receive xml comments.
080: */
081: private final CommentHandler commentHandler;
082:
083: /**
084: * Default constructor.
085: */
086: protected FrontendDefaultHandler() {
087: this .parserConfiguration = new DefaultConfiguration();
088: this .commentHandler = new CommentHandler();
089: }
090:
091: /**
092: * Returns the comment handler that is used to collect comments.
093: *
094: * @return the comment handler.
095: */
096: public CommentHandler getCommentHandler() {
097: return this .commentHandler;
098: }
099:
100: /**
101: * Receive an object for locating the origin of SAX document events.
102: * <p/>
103: * The locator allows the application to determine the end position of
104: * any document-related event, even if the parser is not reporting an
105: * error. Typically, the application will use this information for
106: * reporting its own errors (such as character content that does not
107: * match an application's business rules). The information returned by
108: * the locator is probably not sufficient for use with a search engine.
109: *
110: * @param locator the locator.
111: */
112: public void setDocumentLocator(final Locator locator) {
113: this .locator = locator;
114: }
115:
116: /**
117: * Returns the current locator.
118: *
119: * @return the locator.
120: */
121: public Locator getLocator() {
122: return this .locator;
123: }
124:
125: /**
126: * Returns the configuration property with the specified key.
127: *
128: * @param key the property key.
129: * @return the property value.
130: */
131: public String getConfigProperty(final String key) {
132: return getConfigProperty(key, null);
133: }
134:
135: /**
136: * Returns the configuration property with the specified key (or the specified default value
137: * if there is no such property).
138: * <p/>
139: * If the property is not defined in this configuration, the code will lookup the property in
140: * the parent configuration.
141: *
142: * @param key the property key.
143: * @param defaultValue the default value.
144: * @return the property value.
145: */
146: public String getConfigProperty(final String key,
147: final String defaultValue) {
148: return this .parserConfiguration.getConfigProperty(key,
149: defaultValue);
150: }
151:
152: /**
153: * Sets a parser configuration value.
154: *
155: * @param key the key.
156: * @param value the value.
157: */
158: public void setConfigProperty(final String key, final String value) {
159: if (value == null) {
160: this .parserConfiguration.remove(key);
161: } else {
162: this .parserConfiguration.setProperty(key, value);
163: }
164: }
165:
166: public Enumeration getConfigProperties() {
167: return parserConfiguration.getConfigProperties();
168: }
169:
170: /**
171: * Returns a new instance of the parser.
172: *
173: * @return a new instance of the parser.
174: */
175: public abstract FrontendDefaultHandler newInstance();
176:
177: /**
178: * Returns all keys with the given prefix.
179: *
180: * @param prefix the prefix
181: * @return the iterator containing all keys with that prefix
182: */
183: public Iterator findPropertyKeys(final String prefix) {
184: return this .parserConfiguration.findPropertyKeys(prefix);
185: }
186:
187: /**
188: * Returns the parse result. This method is called at the end of the
189: * parsing process and expects the generated object.
190: *
191: * @return the object.
192: * @throws SAXException if something went wrong.
193: */
194: public abstract Object getResult() throws SAXException;
195:
196: /**
197: * Gets the ContentBase used to resolve relative URLs.
198: *
199: * @return the current contentbase, or null if no contentBase is set.
200: */
201: public URL getContentBase() {
202: final String contentBase = getConfigProperty(Parser.CONTENTBASE_KEY);
203: if (contentBase == null) {
204: return null;
205: }
206: try {
207: return new URL(contentBase);
208: } catch (MalformedURLException mfe) {
209: throw new IllegalStateException("Content Base is illegal."
210: + contentBase);
211: }
212: }
213:
214: public Object clone() throws CloneNotSupportedException {
215: final FrontendDefaultHandler o = (FrontendDefaultHandler) super
216: .clone();
217: o.parserConfiguration = (DefaultConfiguration) parserConfiguration
218: .clone();
219: return o;
220: }
221: }
|