Return-Path: Delivered-To: apmail-jakarta-avalon-cvs-archive@apache.org Received: (qmail 85398 invoked from network); 15 Feb 2002 10:57:43 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 15 Feb 2002 10:57:43 -0000 Received: (qmail 21230 invoked by uid 97); 15 Feb 2002 10:57:57 -0000 Delivered-To: qmlist-jakarta-archive-avalon-cvs@jakarta.apache.org Received: (qmail 21177 invoked by uid 97); 15 Feb 2002 10:57:56 -0000 Mailing-List: contact avalon-cvs-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list avalon-cvs@jakarta.apache.org Received: (qmail 21166 invoked by uid 97); 15 Feb 2002 10:57:55 -0000 Date: 15 Feb 2002 10:57:40 -0000 Message-ID: <20020215105740.12273.qmail@icarus.apache.org> From: cziegeler@apache.org To: jakarta-avalon-excalibur-cvs@apache.org Subject: cvs commit: jakarta-avalon-excalibur/src/xdocs changes.xml X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N cziegeler 02/02/15 02:57:40 Modified: src/xdocs changes.xml Added: src/java/org/apache/avalon/excalibur/xml AbstractXMLConsumer.java ContentHandlerWrapper.java EntityResolver.java JaxpParser.java Parser.java XMLConsumer.java XMLFragment.java XMLizable.java XercesParser.java Removed: src/scratchpad/org/apache/avalon/excalibur/xml AbstractXMLConsumer.java ContentHandlerWrapper.java EntityResolver.java JaxpParser.java Parser.java XMLConsumer.java XMLFragment.java XMLizable.java XercesParser.java Log: Moved XML Parser, EntityResolver and some misc classes to the main branch Revision Changes Path 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/AbstractXMLConsumer.java Index: AbstractXMLConsumer.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; /** * This abstract class provides default implementation of the methods specified * by the XMLConsumer interface. * * @author Pierpaolo Fumagalli * (Apache Software Foundation, Exoffice Technologies) * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public abstract class AbstractXMLConsumer extends AbstractLogEnabled implements XMLConsumer { /** * Receive an object for locating the origin of SAX document events. * * @param locator An object that can return the location of any SAX * document event. */ public void setDocumentLocator(Locator locator) { } /** * Receive notification of the beginning of a document. */ public void startDocument() throws SAXException { } /** * Receive notification of the end of a document. */ public void endDocument() throws SAXException { } /** * Begin the scope of a prefix-URI Namespace mapping. * * @param prefix The Namespace prefix being declared. * @param uri The Namespace URI the prefix is mapped to. */ public void startPrefixMapping(String prefix, String uri) throws SAXException { } /** * End the scope of a prefix-URI mapping. * * @param prefix The prefix that was being mapping. */ public void endPrefixMapping(String prefix) throws SAXException { } /** * Receive notification of the beginning of an element. * * @param uri The Namespace URI, or the empty string if the element has no * Namespace URI or if Namespace * processing is not being performed. * @param loc The local name (without prefix), or the empty string if * Namespace processing is not being performed. * @param raw The raw XML 1.0 name (with prefix), or the empty string if * raw names are not available. * @param a The attributes attached to the element. If there are no * attributes, it shall be an empty Attributes object. */ public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException { } /** * Receive notification of the end of an element. * * @param uri The Namespace URI, or the empty string if the element has no * Namespace URI or if Namespace * processing is not being performed. * @param loc The local name (without prefix), or the empty string if * Namespace processing is not being performed. * @param raw The raw XML 1.0 name (with prefix), or the empty string if * raw names are not available. */ public void endElement(String uri, String loc, String raw) throws SAXException { } /** * Receive notification of character data. * * @param ch The characters from the XML document. * @param start The start position in the array. * @param len The number of characters to read from the array. */ public void characters(char ch[], int start, int len) throws SAXException { } /** * Receive notification of ignorable whitespace in element content. * * @param ch The characters from the XML document. * @param start The start position in the array. * @param len The number of characters to read from the array. */ public void ignorableWhitespace(char ch[], int start, int len) throws SAXException { } /** * Receive notification of a processing instruction. * * @param target The processing instruction target. * @param data The processing instruction data, or null if none was * supplied. */ public void processingInstruction(String target, String data) throws SAXException { } /** * Receive notification of a skipped entity. * * @param name The name of the skipped entity. If it is a parameter * entity, the name will begin with '%'. */ public void skippedEntity(String name) throws SAXException { } /** * Report the start of DTD declarations, if any. * * @param name The document type name. * @param publicId The declared public identifier for the external DTD * subset, or null if none was declared. * @param systemId The declared system identifier for the external DTD * subset, or null if none was declared. */ public void startDTD(String name, String publicId, String systemId) throws SAXException { } /** * Report the end of DTD declarations. */ public void endDTD() throws SAXException { } /** * Report the beginning of an entity. * * @param name The name of the entity. If it is a parameter entity, the * name will begin with '%'. */ public void startEntity(String name) throws SAXException { } /** * Report the end of an entity. * * @param name The name of the entity that is ending. */ public void endEntity(String name) throws SAXException { } /** * Report the start of a CDATA section. */ public void startCDATA() throws SAXException { } /** * Report the end of a CDATA section. */ public void endCDATA() throws SAXException { } /** * Report an XML comment anywhere in the document. * * @param ch An array holding the characters in the comment. * @param start The starting position in the array. * @param len The number of characters to use from the array. */ public void comment(char ch[], int start, int len) throws SAXException { } } 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/ContentHandlerWrapper.java Index: ContentHandlerWrapper.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.apache.avalon.excalibur.pool.Recyclable; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.ext.LexicalHandler; /** * This class is an utility class "wrapping" around a SAX version 2.0 * ContentHandler and forwarding it those events received throug * its XMLConsumers interface. *
* * @author Davanum Srinivas * (Apache Software Foundation, Computer Associates) * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public class ContentHandlerWrapper extends AbstractXMLConsumer implements Recyclable { /** The current ContentHandler. */ protected ContentHandler contentHandler; /** The optional LexicalHandler */ protected LexicalHandler lexicalHandler; /** * Create a new ContentHandlerWrapper instance. */ public ContentHandlerWrapper() { super(); } /** * Create a new ContentHandlerWrapper instance. */ public ContentHandlerWrapper(ContentHandler contentHandler) { this(); this.setContentHandler(contentHandler); } /** * Create a new ContentHandlerWrapper instance. */ public ContentHandlerWrapper(ContentHandler contentHandler, LexicalHandler lexicalHandler) { this(); this.setContentHandler(contentHandler); this.setLexicalHandler(lexicalHandler); } /** * Set the ContentHandler that will receive XML data. * * @exception IllegalStateException If the ContentHandler * was already set. */ public void setContentHandler(ContentHandler contentHandler) throws IllegalStateException { if (this.contentHandler!=null) throw new IllegalStateException(); this.contentHandler=contentHandler; } /** * Set the LexicalHandler that will receive XML data. * * @exception IllegalStateException If the LexicalHandler * was already set. */ public void setLexicalHandler(LexicalHandler lexicalHandler) throws IllegalStateException { if (this.lexicalHandler!=null) throw new IllegalStateException(); this.lexicalHandler=lexicalHandler; } public void recycle () { this.contentHandler = null; this.lexicalHandler = null; } /** * Receive an object for locating the origin of SAX document events. */ public void setDocumentLocator (Locator locator) { if (this.contentHandler==null) return; else this.contentHandler.setDocumentLocator(locator); } /** * Receive notification of the beginning of a document. */ public void startDocument () throws SAXException { if (this.contentHandler==null) throw new SAXException("ContentHandler not set"); this.contentHandler.startDocument(); } /** * Receive notification of the end of a document. */ public void endDocument () throws SAXException { this.contentHandler.endDocument(); } /** * Begin the scope of a prefix-URI Namespace mapping. */ public void startPrefixMapping(String prefix, String uri) throws SAXException { if (this.contentHandler==null) throw new SAXException("ContentHandler not set"); this.contentHandler.startPrefixMapping(prefix, uri); } /** * End the scope of a prefix-URI mapping. */ public void endPrefixMapping(String prefix) throws SAXException { this.contentHandler.endPrefixMapping(prefix); } /** * Receive notification of the beginning of an element. */ public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException { this.contentHandler.startElement(uri, loc, raw, a); } /** * Receive notification of the end of an element. */ public void endElement(String uri, String loc, String raw) throws SAXException { this.contentHandler.endElement(uri, loc, raw); } /** * Receive notification of character data. */ public void characters(char ch[], int start, int len) throws SAXException { this.contentHandler.characters(ch,start,len); } /** * Receive notification of ignorable whitespace in element content. */ public void ignorableWhitespace(char ch[], int start, int len) throws SAXException { this.contentHandler.ignorableWhitespace(ch,start,len); } /** * Receive notification of a processing instruction. */ public void processingInstruction(String target, String data) throws SAXException { this.contentHandler.processingInstruction(target,data); } /** * Receive notification of a skipped entity. * * @param name The name of the skipped entity. If it is a parameter * entity, the name will begin with '%'. */ public void skippedEntity(String name) throws SAXException { this.contentHandler.skippedEntity(name); } /** * Report the start of DTD declarations, if any. * * @param name The document type name. * @param publicId The declared public identifier for the external DTD * subset, or null if none was declared. * @param systemId The declared system identifier for the external DTD * subset, or null if none was declared. */ public void startDTD(String name, String publicId, String systemId) throws SAXException { if (this.lexicalHandler != null) this.lexicalHandler.startDTD(name, publicId, systemId); } /** * Report the end of DTD declarations. */ public void endDTD() throws SAXException { if (this.lexicalHandler != null) this.lexicalHandler.endDTD(); } /** * Report the beginning of an entity. * * @param name The name of the entity. If it is a parameter entity, the * name will begin with '%'. */ public void startEntity(String name) throws SAXException { if (this.lexicalHandler != null) this.lexicalHandler.startEntity(name); } /** * Report the end of an entity. * * @param name The name of the entity that is ending. */ public void endEntity(String name) throws SAXException { if (this.lexicalHandler != null) this.lexicalHandler.endEntity(name); } /** * Report the start of a CDATA section. */ public void startCDATA() throws SAXException { if (this.lexicalHandler != null) this.lexicalHandler.startCDATA(); } /** * Report the end of a CDATA section. */ public void endCDATA() throws SAXException { if (this.lexicalHandler != null) this.lexicalHandler.endCDATA(); } /** * Report an XML comment anywhere in the document. * * @param ch An array holding the characters in the comment. * @param start The starting position in the array. * @param len The number of characters to use from the array. */ public void comment(char ch[], int start, int len) throws SAXException { if (this.lexicalHandler != null) this.lexicalHandler.comment(ch, start, len); } } 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/EntityResolver.java Index: EntityResolver.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.apache.avalon.framework.component.Component; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import java.io.IOException; /** * A component that uses catalogs for resolving Entities. * * @author Davanum Srinivas * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public interface EntityResolver extends Component, org.xml.sax.EntityResolver { String ROLE = "org.apache.avalon.excalibur.xml.EntityResolver"; /** * Allow the application to resolve external entities. * *

The Parser will call this method before opening any external * entity except the top-level document entity (including the * external DTD subset, external entities referenced within the * DTD, and external entities referenced within the document * element): the application may request that the parser resolve * the entity itself, that it use an alternative URI, or that it * use an entirely different input source.

* *

Application writers can use this method to redirect external * system identifiers to secure and/or local URIs, to look up * public identifiers in a catalogue, or to read an entity from a * database or other input source (including, for example, a dialog * box).

* *

If the system identifier is a URL, the SAX parser must * resolve it fully before reporting it to the application.

* * @param publicId The public identifier of the external entity * being referenced, or null if none was supplied. * @param systemId The system identifier of the external entity * being referenced. * @return An InputSource object describing the new input source, * or null to request that the parser open a regular * URI connection to the system identifier. * @exception org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * @exception java.io.IOException A Java-specific IO exception, * possibly the result of creating a new InputStream * or Reader for the InputSource. * @see org.xml.sax.InputSource */ InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException; } 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/JaxpParser.java Index: JaxpParser.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.apache.avalon.excalibur.pool.Poolable; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.parameters.Parameterizable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.parameters.ParameterException; import org.w3c.dom.Document; import org.xml.sax.*; import org.xml.sax.ext.LexicalHandler; import javax.xml.parsers.*; import java.io.IOException; /** * An XMLParser that is only dependant on JAXP 1.1 compliant parsers. * * The configuration can contain the following parameters : *
    *
  • validate (boolean, default = false) : should the parser * validate parsed documents ? *
  • *
  • namespace-prefixes (boolean, default = false) : do we want * namespaces declarations also as 'xmlns:' attributes ?
    * Note : setting this to true confuses some XSL * processors (e.g. Saxon). *
  • *
  • stop-on-warning (boolean, default = true) : should the parser * stop parsing if a warning occurs ? *
  • *
  • stop-on-recoverable-error (boolean, default = true) : should the parser * stop parsing if a recoverable error occurs ? *
  • *
  • reuse-parsers (boolean, default = true) : do we want to reuse * parsers or create a new parser for each parse ?
    * Note : even if this parameter is true, parsers are not * recycled in case of parsing errors : some parsers (e.g. Xerces) don't like * to be reused after failure. *
  • *
  • sax-parser-factory (string, optional) : the name of the SAXParserFactory * implementation class to be used instead of using the standard JAXP mechanism * (SAXParserFactory.newInstance()). This allows to choose * unambiguously the JAXP implementation to be used when several of them are * available in the classpath. *
  • *
  • document-builder-factory (string, optional) : the name of the * DocumentBuilderFactory implementation to be used (similar to * sax-parser-factory for DOM). *
  • *
* * @author Berin Loritsch * @author Carsten Ziegeler * @author Sylvain Wallez * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public class JaxpParser extends AbstractLogEnabled implements Parser, ErrorHandler, Composable, Parameterizable, Poolable { /** the SAX Parser factory */ protected SAXParserFactory factory; /** the Document Builder factory */ protected DocumentBuilderFactory docFactory; /** The SAX reader. It is created lazily by {@link #setupXMLReader()} and cleared if a parsing error occurs. */ protected XMLReader reader; /** The DOM builder. It is created lazily by {@link #setupDocumentBuilder()} and cleared if a parsing error occurs. */ protected DocumentBuilder docBuilder; /** the component manager */ protected ComponentManager manager; /** the Entity Resolver */ protected EntityResolver resolver; /** do we want namespaces also as attributes ? */ protected boolean nsPrefixes; /** do we want to reuse parsers ? */ protected boolean reuseParsers; /** do we stop on warnings ? */ protected boolean stopOnWarning; /** do we stop on recoverable errors ? */ protected boolean stopOnRecoverableError; /** * Get the Entity Resolver from the component manager */ public void compose(ComponentManager manager) throws ComponentException { this.manager = manager; if ( this.manager.hasComponent(EntityResolver.ROLE) ) { this.resolver = (EntityResolver)this.manager.lookup(EntityResolver.ROLE); if ( this.getLogger().isDebugEnabled() ) { this.getLogger().debug("JaxpParser: Using EntityResolver: " + this.resolver); } } } /** * Configure */ public void parameterize( Parameters params ) throws ParameterException { // Validation and namespace prefixes parameters boolean validate = params.getParameterAsBoolean("validate", false); this.nsPrefixes = params.getParameterAsBoolean("namespace-prefixes", false); this.reuseParsers = params.getParameterAsBoolean("reuse-parsers", true); this.stopOnWarning = params.getParameterAsBoolean("stop-on-warning", true); this.stopOnRecoverableError = params.getParameterAsBoolean("stop-on-recoverable-error", true); // Get the SAXFactory final String saxParserFactoryName = params.getParameter("sax-parser-factory", "javax.xml.parsers.SAXParserFactory"); if ("javax.xml.parsers.SAXParserFactory".equals(saxParserFactoryName) ) { this.factory = SAXParserFactory.newInstance(); } else { try { final Class factoryClass = this.loadClass( saxParserFactoryName ); this.factory = (SAXParserFactory)factoryClass.newInstance(); } catch(Exception e) { throw new ParameterException("Cannot load SAXParserFactory class " + saxParserFactoryName, e); } } this.factory.setNamespaceAware(true); this.factory.setValidating(validate); // Get the DocumentFactory final String documentBuilderFactoryName = params.getParameter("document-builder-factory", "javax.xml.parsers.DocumentBuilderFactory"); if ("javax.xml.parsers.DocumentBuilderFactory".equals(documentBuilderFactoryName) ) { this.docFactory = DocumentBuilderFactory.newInstance(); } else { try { final Class factoryClass = this.loadClass( documentBuilderFactoryName ); this.docFactory = (DocumentBuilderFactory)factoryClass.newInstance(); } catch(Exception e) { throw new ParameterException("Cannot load DocumentBuilderFactory class " + documentBuilderFactoryName, e); } } this.docFactory.setNamespaceAware(true); this.docFactory.setValidating(validate); if ( this.getLogger().isDebugEnabled() ) { this.getLogger().debug("JaxpParser: validating: " + validate + ", namespace-prefixes: " + this.nsPrefixes + ", reuse parser: " + this.reuseParsers + ", stop on warning: " + this.stopOnWarning + ", stop on recoverable-error: " + this.stopOnRecoverableError + ", saxParserFactory: " + saxParserFactoryName + ", documentBuilderFactory: " + documentBuilderFactoryName); } } /** * Load a class */ protected Class loadClass( String name ) throws Exception { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { loader = this.getClass().getClassLoader(); } return loader.loadClass( name ); } /** * Parse the InputSource and send * SAX events to the consumer. * Attention: the consumer can either be an XMLConsumer * or implement the LexicalHandler as well. * The parse should take care of this. */ public void parse(InputSource in, ContentHandler consumer) throws SAXException, IOException { if (consumer instanceof LexicalHandler) { this.parse(in, consumer, (LexicalHandler)consumer); } else { this.parse(in, consumer, null); } } /** * Parse the InputSource and send * SAX events to the consumer. * Attention: the consumer can implement the * LexicalHandler as well. * The parse should take care of this. */ public void parse(InputSource in, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws SAXException, IOException { this.setupXMLReader(); // Ensure we will use a fresh new parser at next parse in case of failure XMLReader tmpReader = this.reader; this.reader = null; try { if ( null != lexicalHandler ) { tmpReader.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler); } } catch (SAXException e) { this.getLogger().warn("SAX2 driver does not support property: "+ "'http://xml.org/sax/properties/lexical-handler'"); } tmpReader.setErrorHandler( this ); tmpReader.setContentHandler( contentHandler ); if ( null != this.resolver ) { tmpReader.setEntityResolver( this.resolver ); } tmpReader.parse(in); // Here, parsing was successful : restore this.reader if ( this.reuseParsers ) this.reader = tmpReader; } /** * Parses a new Document object from the given InputSource. */ public Document parseDocument(InputSource input) throws SAXException, IOException { this.setupDocumentBuilder(); // Ensure we will use a fresh new parser at next parse in case of failure DocumentBuilder tmpBuilder = this.docBuilder; this.docBuilder = null; if ( null != this.resolver ) { tmpBuilder.setEntityResolver(this.resolver); } Document result = tmpBuilder.parse(input); // Here, parsing was successful : restore this.builder if ( this.reuseParsers) this.docBuilder = tmpBuilder; return result; } /** * Creates a new XMLReader if needed. */ protected void setupXMLReader() throws SAXException { if ( null == this.reader ) { // Create the XMLReader try { this.reader = factory.newSAXParser().getXMLReader(); } catch( ParserConfigurationException pce ) { throw new SAXException( "Cannot produce a valid parser", pce ); } if ( this.nsPrefixes ) { try { this.reader.setFeature("http://xml.org/sax/features/namespace-prefixes", this.nsPrefixes); } catch ( SAXException e ) { this.getLogger().warn("SAX2 XMLReader does not support setting feature: "+ "'http://xml.org/sax/features/namespace-prefixes'"); } } } } /** * Creates a new DocumentBuilder if needed. */ protected void setupDocumentBuilder() throws SAXException { if ( null == this.docBuilder ) { try { this.docBuilder = this.docFactory.newDocumentBuilder(); } catch (ParserConfigurationException pce) { throw new SAXException( "Could not create DocumentBuilder", pce ); } } } /** * Return a new Document. */ public Document createDocument() throws SAXException { this.setupDocumentBuilder(); return this.docBuilder.newDocument(); } /** * Receive notification of a recoverable error. */ public void error( SAXParseException e ) throws SAXException { final String msg = "Error parsing "+e.getSystemId()+" (line "+ e.getLineNumber()+" col. "+e.getColumnNumber()+ "): "+e.getMessage(); if ( this.stopOnRecoverableError ) { throw new SAXException( msg, e ); } this.getLogger().error( msg, e ); } /** * Receive notification of a fatal error. */ public void fatalError( SAXParseException e ) throws SAXException { throw new SAXException("Fatal error parsing "+e.getSystemId()+" (line "+ e.getLineNumber()+" col. "+e.getColumnNumber()+ "): "+e.getMessage(),e); } /** * Receive notification of a warning. */ public void warning( SAXParseException e ) throws SAXException { final String msg = "Warning parsing "+e.getSystemId()+" (line "+ e.getLineNumber()+" col. "+e.getColumnNumber()+ "): "+e.getMessage(); if ( this.stopOnWarning ) { throw new SAXException( msg, e ); } this.getLogger().warn( msg, e ); } } 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/Parser.java Index: Parser.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.apache.avalon.framework.component.Component; import org.w3c.dom.Document; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.ext.LexicalHandler; import java.io.IOException; /** * * The parser can be used to parse any XML document given * by a InputSource object. * It can either send XML events or create a DOM from * the parsed document. * * @author Carsten Ziegeler * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public interface Parser extends Component { String ROLE = "org.apache.avalon.excalibur.xml.Parser"; /** * Parse the InputSource and send * SAX events to the consumer. * Attention: the consumer can implement the * LexicalHandler as well. * The parse should take care of this. */ void parse(InputSource in, ContentHandler consumer) throws SAXException, IOException; /** * Parse the InputSource and send * SAX events to the content handler and * the lexical handler. */ void parse(InputSource in, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws SAXException, IOException; /** * Parse the InputSource and create * a DOM out of it. */ Document parseDocument(InputSource in) throws SAXException, IOException; /** * Return a new Document. */ Document createDocument() throws SAXException; } 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/XMLConsumer.java Index: XMLConsumer.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.xml.sax.ContentHandler; import org.xml.sax.ext.LexicalHandler; /** * This interfaces identifies classes that consume XML data, receiving * notification of SAX events. *
* This interface unites the idea of SAX ContentHandler and * LexicalHandler. * * @author Pierpaolo Fumagalli * (Apache Software Foundation, Exoffice Technologies) * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public interface XMLConsumer extends ContentHandler, LexicalHandler { } 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/XMLFragment.java Index: XMLFragment.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.w3c.dom.DOMException; import org.w3c.dom.Node; /** * This interface must be implemented by classes willing * to provide an XML representation of their current state. *
* * @author Sylvain Wallez * @author Ricardo Rocha for the original XObject class * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public interface XMLFragment { /** * Appends children representing the object's state to the given node. */ void toDOM(Node node) throws DOMException; } 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/XMLizable.java Index: XMLizable.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; /** * This interface can be implemented by classes willing to provide an XML representation * of their current state as SAX events. * * @author Sylvain Wallez * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public interface XMLizable { /** * Generates SAX events representing the object's state.
* NOTE : if the implementation can produce lexical events, care should be taken * that handler can actually be a {@link XMLConsumer} that accepts such * events or directly implements the LexicalHandler interface! */ void toSAX(ContentHandler handler) throws SAXException; } 1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/xml/XercesParser.java Index: XercesParser.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.avalon.excalibur.xml; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.thread.SingleThreaded; import org.apache.xerces.dom.DocumentImpl; import org.apache.xerces.parsers.DOMParser; import org.apache.xerces.parsers.SAXParser; import org.w3c.dom.Document; import org.xml.sax.ContentHandler; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.ext.LexicalHandler; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; /** * * @author Pierpaolo Fumagalli * (Apache Software Foundation, Exoffice Technologies) * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 10:57:39 $ */ public class XercesParser extends AbstractLogEnabled implements Parser, ErrorHandler, SingleThreaded { /** the SAX Parser */ final SAXParser parser; public XercesParser () throws SAXException { this.parser = new SAXParser(); this.parser.setFeature("http://xml.org/sax/features/validation",false); this.parser.setFeature("http://xml.org/sax/features/namespaces",true); this.parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true); } public void parse(InputSource in, ContentHandler consumer) throws SAXException, IOException { if (consumer instanceof LexicalHandler) { this.parse(in, consumer, (LexicalHandler)consumer); } else { this.parse(in, consumer, null); } } /** * Parse the InputSource and send * SAX events to the content handler and * the lexical handler. */ public void parse(InputSource in, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws SAXException, IOException { if ( null != lexicalHandler ) { this.parser.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler); } this.parser.setErrorHandler(this); this.parser.setContentHandler(contentHandler); this.parser.parse(in); } /** * Parses a new Document object from the given InputSource. */ public Document parseDocument(InputSource input) throws SAXException, IOException { DOMParser parser = null; try { parser = new DOMParser(); parser.setFeature("http://xml.org/sax/features/validation",false); parser.setFeature("http://xml.org/sax/features/namespaces",true); parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true); parser.parse(input); } catch (Exception pce) { getLogger().error("Could not build DocumentBuilder", pce); return null; } return parser.getDocument(); } /** * Return a new Document. */ public Document createDocument() throws SAXException { return new DocumentImpl(); } /** * Receive notification of a recoverable error. */ public void error(SAXParseException e) throws SAXException { throw new SAXException("Error parsing "+e.getSystemId()+" (line "+ e.getLineNumber()+" col. "+e.getColumnNumber()+ "): "+e.getMessage(),e); } /** * Receive notification of a fatal error. */ public void fatalError(SAXParseException e) throws SAXException { throw new SAXException("Fatal error parsing "+e.getSystemId()+" (line "+ e.getLineNumber()+" col. "+e.getColumnNumber()+ "): "+e.getMessage(),e); } /** * Receive notification of a warning. */ public void warning(SAXParseException e) throws SAXException { throw new SAXException("Warning parsing "+e.getSystemId()+" (line "+ e.getLineNumber()+" col. "+e.getColumnNumber()+ "): "+e.getMessage(),e); } } 1.13 +7 -1 jakarta-avalon-excalibur/src/xdocs/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/xdocs/changes.xml,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- changes.xml 30 Jan 2002 18:28:11 -0000 1.12 +++ changes.xml 15 Feb 2002 10:57:40 -0000 1.13 @@ -13,8 +13,14 @@ - + + + + Added XML Parser role and implementation, EntityResolver role and + some misc. XML support (from Cocoon). + + Initial port of Cocoon's Source resolvers and XML parsers -- To unsubscribe, e-mail: For additional commands, e-mail: