avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/xml JaxpParser.java
Date Wed, 02 Jan 2002 10:15:48 GMT
cziegeler    02/01/02 02:15:48

  Modified:    src/scratchpad/org/apache/avalon/excalibur/xml
                        JaxpParser.java
  Log:
  Added configuration for handling errors and reformated code to fit better with the coding
standard
  
  Revision  Changes    Path
  1.7       +80 -33    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/xml/JaxpParser.java
  
  Index: JaxpParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/xml/JaxpParser.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- JaxpParser.java	27 Dec 2001 15:00:32 -0000	1.6
  +++ JaxpParser.java	2 Jan 2002 10:15:48 -0000	1.7
  @@ -34,6 +34,12 @@
    *     <i>Note</i> : setting this to <code>true</code> confuses
some XSL
    *     processors (e.g. Saxon).
    * </li>
  + * <li>stop-on-warning (boolean, default = <code>true</code>) : should
the parser
  + *     stop parsing if a warning occurs ?
  + * </li>
  + * <li>stop-on-recoverable-error (boolean, default = <code>true</code>)
: should the parser
  + *     stop parsing if a recoverable error occurs ?
  + * </li>
    * <li>reuse-parsers (boolean, default = <code>true</code>) : do we want
to reuse
    *     parsers or create a new parser for each parse ?<br>
    *     <i>Note</i> : even if this parameter is <code>true</code>,
parsers are not
  @@ -45,7 +51,7 @@
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
  - * @version CVS $Revision: 1.6 $ $Date: 2001/12/27 15:00:32 $
  + * @version CVS $Revision: 1.7 $ $Date: 2002/01/02 10:15:48 $
    */
   public class JaxpParser
   extends AbstractLogEnabled
  @@ -77,13 +83,21 @@
       /** 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 {
  +    throws ComponentException
  +    {
           this.manager = manager;
  -        if ( this.manager.hasComponent(EntityResolver.ROLE) )
  +        if (  this.manager.hasComponent(EntityResolver.ROLE) )
           {
               this.resolver = (EntityResolver)this.manager.lookup(EntityResolver.ROLE);
               if ( this.getLogger().isDebugEnabled() )
  @@ -102,6 +116,8 @@
           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);
   
           this.factory = SAXParserFactory.newInstance();
           this.factory.setNamespaceAware(true);
  @@ -114,7 +130,9 @@
           {
               this.getLogger().debug("JaxpParser: validating: " + validate +
                                      ", namespace-prefixes: " + this.nsPrefixes +
  -                                   ", reuse parser: " + this.reuseParsers);
  +                                   ", reuse parser: " + this.reuseParsers +
  +                                   ", stop on warning: " + this.stopOnWarning +
  +                                   ", stop on recoverable-error: " + this.stopOnRecoverableError);
           }
       }
   
  @@ -128,18 +146,22 @@
           this.reader = null;
   
           try {
  -            if (consumer instanceof LexicalHandler) {
  +            if (consumer instanceof LexicalHandler)
  +            {
                   tmpReader.setProperty("http://xml.org/sax/properties/lexical-handler",
                           (LexicalHandler)consumer);
               }
  -        } catch (SAXException e) {
  -            getLogger().warn("SAX2 driver does not support property: "+
  -                             "'http://xml.org/sax/properties/lexical-handler'");
  +        }
  +        catch (SAXException e)
  +        {
  +            this.getLogger().warn("SAX2 driver does not support property: "+
  +                                  "'http://xml.org/sax/properties/lexical-handler'");
           }
   
           tmpReader.setErrorHandler( this );
           tmpReader.setContentHandler( consumer );
  -        if ( null != this.resolver ) {
  +        if ( null != this.resolver )
  +        {
               tmpReader.setEntityResolver( this.resolver );
           }
   
  @@ -164,14 +186,15 @@
           DocumentBuilder tmpBuilder = this.docBuilder;
           this.docBuilder = null;
   
  -        if ( null != this.resolver ) {
  +        if ( null != this.resolver )
  +        {
               tmpBuilder.setEntityResolver(this.resolver);
           }
   
           Document result = tmpBuilder.parse(input);
   
           // Here, parsing was successful : restore this.builder
  -        if (this.reuseParsers)
  +        if ( this.reuseParsers)
               this.docBuilder = tmpBuilder;
   
           return result;
  @@ -180,14 +203,20 @@
       /**
        * Creates a new <code>XMLReader</code> if needed.
        */
  -    protected void setupXMLReader() throws SAXException {
  -        if (this.reader == null) {
  +    protected void setupXMLReader()
  +    throws SAXException
  +    {
  +        if ( null == this.reader )
  +        {
               // Create the XMLReader
  -            try {
  +            try
  +            {
                   this.reader = factory.newSAXParser().getXMLReader();
                   this.reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
this.nsPrefixes);
  -            } catch(Exception e) {
  -                getLogger().error("Cannot produce a valid parser", e);
  +            }
  +            catch(Exception e)
  +            {
  +                this.getLogger().error("Cannot produce a valid parser", e);
                   throw new SAXException("Cannot produce a valid parser", e);
               }
           }
  @@ -196,12 +225,17 @@
       /**
        * Creates a new <code>DocumentBuilder</code> if needed.
        */
  -    protected void setupDocumentBuilder() {
  -        if (this.docBuilder == null) {
  -            try {
  +    protected void setupDocumentBuilder()
  +    {
  +        if ( null == this.docBuilder )
  +        {
  +            try
  +            {
                   this.docBuilder = this.docFactory.newDocumentBuilder();
  -            } catch (ParserConfigurationException pce) {
  -                getLogger().error("Could not create DocumentBuilder", pce);
  +            }
  +            catch (ParserConfigurationException pce)
  +            {
  +                this.getLogger().error("Could not create DocumentBuilder", pce);
                   throw new org.apache.avalon.framework.CascadingRuntimeException(
                       "Could not create DocumentBuilder", pce);
               }
  @@ -211,18 +245,25 @@
       /**
        * 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);
  +    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().warn( msg, e );
       }
   
       /**
        * Receive notification of a fatal error.
        */
  -    public void fatalError(SAXParseException e)
  -    throws SAXException {
  +    public void fatalError( SAXParseException e )
  +    throws SAXException
  +    {
           throw new SAXException("Fatal error parsing "+e.getSystemId()+" (line "+
                                  e.getLineNumber()+" col. "+e.getColumnNumber()+
                                  "): "+e.getMessage(),e);
  @@ -231,10 +272,16 @@
       /**
        * 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);
  +    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 );
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message