| /* |
| * The Apache Software License, Version 1.1 |
| * |
| * |
| * Copyright (c) 1999,2000 The Apache Software Foundation. All rights |
| * reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
| * are met: |
| * |
| * 1. Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * |
| * 2. Redistributions in binary form must reproduce the above copyright |
| * notice, this list of conditions and the following disclaimer in |
| * the documentation and/or other materials provided with the |
| * distribution. |
| * |
| * 3. The end-user documentation included with the redistribution, |
| * if any, must include the following acknowledgment: |
| * "This product includes software developed by the |
| * Apache Software Foundation (http://www.apache.org/)." |
| * Alternately, this acknowledgment may appear in the software itself, |
| * if and wherever such third-party acknowledgments normally appear. |
| * |
| * 4. The names "Xerces" and "Apache Software Foundation" must |
| * not be used to endorse or promote products derived from this |
| * software without prior written permission. For written |
| * permission, please contact apache@apache.org. |
| * |
| * 5. Products derived from this software may not be called "Apache", |
| * nor may "Apache" appear in their name, without prior written |
| * permission of the Apache Software Foundation. |
| * |
| * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| * SUCH DAMAGE. |
| * ==================================================================== |
| * |
| * This software consists of voluntary contributions made by many |
| * individuals on behalf of the Apache Software Foundation and was |
| * originally based on software copyright (c) 1999, International |
| * Business Machines, Inc., http://www.apache.org. For more |
| * information on the Apache Software Foundation, please see |
| * <http://www.apache.org/>. |
| */ |
| |
| package org.apache.xerces.parsers; |
| |
| import java.io.IOException; |
| import java.util.Locale; |
| |
| import org.apache.xerces.impl.Constants; |
| import org.apache.xerces.xni.parser.XMLParserConfiguration; |
| |
| import org.xml.sax.EntityResolver; |
| import org.xml.sax.ErrorHandler; |
| import org.xml.sax.InputSource; |
| import org.xml.sax.SAXException; |
| import org.xml.sax.SAXNotRecognizedException; |
| import org.xml.sax.SAXNotSupportedException; |
| |
| /** |
| * Base class of all XML-related parsers. |
| * <p> |
| * In addition to the features and properties recognized by the parser |
| * configuration, this parser recognizes these additional features and |
| * properties: |
| * <ul> |
| * <li>Properties |
| * <ul> |
| * <li>http://apache.org/xml/properties/internal/error-handler</li> |
| * <li>http://apache.org/xml/properties/internal/entity-resolver</li> |
| * </ul> |
| * </ul> |
| * |
| * @author Stubs generated by DesignDoc on Mon Sep 11 11:10:57 PDT 2000 |
| * @author Arnaud Le Hors, IBM |
| * @author Andy Clark, IBM |
| * |
| * @version $Id$ |
| */ |
| public abstract class XMLParser { |
| |
| // |
| // Constants |
| // |
| |
| /** Property identifier: entity resolver. */ |
| protected static final String ENTITY_RESOLVER = |
| Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; |
| |
| /** Property identifier: error handler. */ |
| protected static final String ERROR_HANDLER = |
| Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; |
| |
| // |
| // Data |
| // |
| |
| /** The parser configuration. */ |
| protected XMLParserConfiguration fConfiguration; |
| |
| // |
| // Constructors |
| // |
| |
| /** |
| * Default Constructor. |
| */ |
| protected XMLParser(XMLParserConfiguration config) { |
| |
| // save configuration |
| fConfiguration = config; |
| |
| // add default recognized properties |
| final String[] recognizedProperties = { |
| ENTITY_RESOLVER, ERROR_HANDLER, |
| }; |
| fConfiguration.addRecognizedProperties(recognizedProperties); |
| |
| } // <init>(XMLParserConfiguration) |
| |
| // |
| // Public methods |
| // |
| |
| /** |
| * Parses the input source specified by the given system identifier. |
| * <p> |
| * This method is equivalent to the following: |
| * <pre> |
| * parse(new InputSource(systemId)); |
| * </pre> |
| * |
| * @param source The input source. |
| * |
| * @exception org.xml.sax.SAXException Throws exception on SAX error. |
| * @exception java.io.IOException Throws exception on i/o error. |
| */ |
| public void parse(String systemId) throws SAXException, IOException { |
| parse(new InputSource(systemId)); |
| } // parse(String) |
| |
| /** |
| * parse |
| * |
| * @param inputSource |
| * |
| * @exception org.xml.sax.SAXException |
| * @exception java.io.IOException |
| */ |
| public void parse(InputSource inputSource) |
| throws SAXException, IOException { |
| |
| fConfiguration.parse(inputSource); |
| |
| } // parse(InputSource) |
| |
| /** |
| * Sets the resolver used to resolve external entities. The EntityResolver |
| * interface supports resolution of public and system identifiers. |
| * |
| * @param resolver The new entity resolver. Passing a null value will |
| * uninstall the currently installed resolver. |
| */ |
| public void setEntityResolver(EntityResolver resolver) { |
| |
| try { |
| setProperty(ENTITY_RESOLVER, resolver); |
| } |
| catch (SAXException e) { |
| // do nothing |
| } |
| |
| } // setEntityResolver(EntityResolver) |
| |
| /** |
| * Return the current entity resolver. |
| * |
| * @return The current entity resolver, or null if none |
| * has been registered. |
| * @see #setEntityResolver |
| */ |
| public EntityResolver getEntityResolver() { |
| |
| EntityResolver entityResolver = null; |
| try { |
| entityResolver = (EntityResolver)getProperty(ENTITY_RESOLVER); |
| } |
| catch (SAXException e) { |
| // do nothing |
| } |
| return entityResolver; |
| |
| } // getEntityResolver():EntityResolver |
| |
| /** |
| * Allow an application to register an error event handler. |
| * |
| * <p>If the application does not register an error handler, all |
| * error events reported by the SAX parser will be silently |
| * ignored; however, normal processing may not continue. It is |
| * highly recommended that all SAX applications implement an |
| * error handler to avoid unexpected bugs.</p> |
| * |
| * <p>Applications may register a new or different handler in the |
| * middle of a parse, and the SAX parser must begin using the new |
| * handler immediately.</p> |
| * |
| * @param errorHandler The error handler. |
| * @exception java.lang.NullPointerException If the handler |
| * argument is null. |
| * @see #getErrorHandler |
| */ |
| public void setErrorHandler(ErrorHandler errorHandler) { |
| |
| try { |
| setProperty(ERROR_HANDLER, errorHandler); |
| } |
| catch (SAXException e) { |
| // do nothing |
| } |
| |
| } // setErrorHandler(ErrorHandler) |
| |
| /** |
| * Return the current error handler. |
| * |
| * @return The current error handler, or null if none |
| * has been registered. |
| * @see #setErrorHandler |
| */ |
| public ErrorHandler getErrorHandler() { |
| |
| ErrorHandler errorHandler = null; |
| try { |
| errorHandler = (ErrorHandler)getProperty(ERROR_HANDLER); |
| } |
| catch (SAXException e) { |
| // do nothing |
| } |
| return errorHandler; |
| |
| } // getErrorHandler():ErrorHandler |
| |
| /** |
| * Set the state of a feature. |
| * |
| * Set the state of any feature in a SAX2 parser. The parser |
| * might not recognize the feature, and if it does recognize |
| * it, it might not be able to fulfill the request. |
| * |
| * @param featureId The unique identifier (URI) of the feature. |
| * @param state The requested state of the feature (true or false). |
| * |
| * @exception org.xml.sax.SAXNotRecognizedException If the |
| * requested feature is not known. |
| * @exception org.xml.sax.SAXNotSupportedException If the |
| * requested feature is known, but the requested |
| * state is not supported. |
| * @exception org.xml.sax.SAXException If there is any other |
| * problem fulfilling the request. |
| */ |
| public void setFeature(String featureId, boolean state) |
| throws SAXNotRecognizedException, SAXNotSupportedException { |
| |
| fConfiguration.setFeature(featureId, state); |
| |
| } // setFeature(String,boolean) |
| |
| /** |
| * setProperty |
| * |
| * @param propertyId |
| * @param value |
| */ |
| public void setProperty(String propertyId, Object value) |
| throws SAXNotRecognizedException, SAXNotSupportedException { |
| |
| fConfiguration.setProperty(propertyId, value); |
| |
| } // setProperty(String,Object) |
| |
| /** |
| * Set the locale to use for messages. |
| * |
| * @param locale The locale object to use for localization of messages. |
| * |
| * @exception SAXException An exception thrown if the parser does not |
| * support the specified locale. |
| * |
| * @see org.xml.sax.Parser |
| */ |
| public void setLocale(Locale locale) throws SAXException { |
| |
| fConfiguration.setLocale(locale); |
| |
| } // setLocale(Locale) |
| |
| // |
| // XMLComponentManager methods |
| // |
| |
| /** |
| * Returns the state of a feature. |
| * |
| * @param featureId The feature identifier. |
| * |
| * @throws SAXNotRecognizedException Thrown if the feature is not |
| * recognized. |
| * @throws SAXNotSupportedException Thrown if the feature is not |
| * supported. |
| */ |
| public boolean getFeature(String featureId) |
| throws SAXNotRecognizedException, SAXNotSupportedException { |
| |
| return fConfiguration.getFeature(featureId); |
| |
| } // getFeature(String):boolean |
| |
| /** |
| * Returns the value of a property. |
| * |
| * @param propertyId The property identifier. |
| * |
| * @throws SAXNotRecognizedException Thrown if the feature is not |
| * recognized. |
| * @throws SAXNotSupportedException Thrown if the feature is not |
| * supported. |
| */ |
| public Object getProperty(String propertyId) |
| throws SAXNotRecognizedException, SAXNotSupportedException { |
| |
| return fConfiguration.getProperty(propertyId); |
| |
| } // getProperty(String):Object |
| |
| } // class XMLParser |