avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/xmlutil/src/java/org/apache/avalon/excalibur/xml XercesParser.java
Date Tue, 09 Jul 2002 13:57:57 GMT
donaldp     2002/07/09 06:57:57

  Modified:    xmlutil/src/java/org/apache/avalon/excalibur/xml
                        XercesParser.java
  Log:
  Start reworking class to be capable of being "ThreadSafe"
  
  Revision  Changes    Path
  1.3       +55 -13    jakarta-avalon-excalibur/xmlutil/src/java/org/apache/avalon/excalibur/xml/XercesParser.java
  
  Index: XercesParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/xmlutil/src/java/org/apache/avalon/excalibur/xml/XercesParser.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- XercesParser.java	7 Jul 2002 07:19:00 -0000	1.2
  +++ XercesParser.java	9 Jul 2002 13:57:57 -0000	1.3
  @@ -32,17 +32,12 @@
       implements Parser, ErrorHandler, SingleThreaded
   {
       /** the SAX Parser */
  -    private final SAXParser m_parser;
  +    private SAXParser m_parser;
   
       public XercesParser()
           throws SAXException
       {
  -        m_parser = new SAXParser();
  -
  -        m_parser.setFeature( "http://xml.org/sax/features/validation", false );
  -        m_parser.setFeature( "http://xml.org/sax/features/namespaces", true );
  -        m_parser.setFeature( "http://xml.org/sax/features/namespace-prefixes",
  -                             true );
  +        m_parser = createSAXParser();
       }
   
       public void parse( final InputSource in,
  @@ -69,14 +64,23 @@
                          final LexicalHandler lexicalHandler )
           throws SAXException, IOException
       {
  +        final SAXParser parser = getSAXParser();
  +
           if( null != lexicalHandler )
           {
  -            m_parser.setProperty( "http://xml.org/sax/properties/lexical-handler",
  -                                  lexicalHandler );
  +            parser.setProperty( "http://xml.org/sax/properties/lexical-handler",
  +                                lexicalHandler );
           }
  -        m_parser.setErrorHandler( this );
  -        m_parser.setContentHandler( contentHandler );
  -        m_parser.parse( in );
  +        parser.setErrorHandler( this );
  +        parser.setContentHandler( contentHandler );
  +        parser.parse( in );
  +
  +        //Note it is a deliberate choice to make sure that
  +        //the parser is only released when a successful parse
  +        //has occured. If an exception is generated the
  +        //parser becomes fubar so we need to let it go into
  +        //the void to be garbage collected
  +        releaseSAXParser( parser );
       }
   
       /**
  @@ -151,5 +155,43 @@
               spe.getLineNumber() + " col. " + spe.getColumnNumber() +
               "): " + spe.getMessage();
           throw new SAXException( message, spe );
  +    }
  +
  +    /**
  +     * Aquire a parser from the pool of {@link SAXParser} objects.
  +     *
  +     * @return the parser
  +     */
  +    private SAXParser getSAXParser()
  +        throws SAXException
  +    {
  +        return m_parser;
  +    }
  +
  +    /**
  +     * Utility method to release parser back into pool.
  +     *
  +     * @param parser the parser
  +     */
  +    private void releaseSAXParser( final SAXParser parser )
  +    {
  +        m_parser = parser;
  +    }
  +
  +    /**
  +     * Utility method to create a SAXParser.
  +     *
  +     * @return new SAXParser
  +     * @throws SAXException if unable to create parser
  +     */
  +    private SAXParser createSAXParser()
  +        throws SAXException
  +    {
  +        final SAXParser parser = new SAXParser();
  +        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 );
  +        return parser;
       }
   }
  
  
  

--
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