You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/12/25 21:15:57 UTC

svn commit: r490165 - in /cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl: AbstractJaxpParser.java DefaultEntityResolver.java JaxpDOMParser.java JaxpSAXParser.java

Author: cziegeler
Date: Mon Dec 25 12:15:56 2006
New Revision: 490165

URL: http://svn.apache.org/viewvc?view=rev&rev=490165
Log:
Make implementations thread safe

Modified:
    cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java
    cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java
    cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java
    cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java

Modified: cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java?view=diff&rev=490165&r1=490164&r2=490165
==============================================================================
--- cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java (original)
+++ cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java Mon Dec 25 12:15:56 2006
@@ -34,9 +34,6 @@
     /** the Entity Resolver */
     protected EntityResolver resolver;
 
-    /** do we want to reuse parsers ? */
-    protected boolean reuseParsers = true;
-
     /** Do we want to validate? */
     protected boolean validate = false;
 
@@ -57,24 +54,6 @@
 
     public EntityResolver getEntityResolver() {
         return this.resolver;
-    }
-
-    /**
-     * @see #setReuseParsers(boolean)
-     */
-    public boolean isReuseParsers() {
-        return reuseParsers;
-    }
-
-    /**
-     * Do we want to reuse parsers or create a new parser for each parse ?
-     * (Default is true)
-     * <i>Note</i> : even if this parameter is <code>true</code>, parsers are not
-     * recycled in case of parsing errors : some parsers (e.g. Xerces) don't like
-     * to be reused after failure.
-     */
-    public void setReuseParsers(boolean reuseParsers) {
-        this.reuseParsers = reuseParsers;
     }
 
     /**

Modified: cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java?view=diff&rev=490165&r1=490164&r2=490165
==============================================================================
--- cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java (original)
+++ cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java Mon Dec 25 12:15:56 2006
@@ -19,7 +19,6 @@
 import java.io.File;
 import java.io.IOException;
 
-import org.apache.cocoon.util.AbstractLogEnabled;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.xml.resolver.CatalogManager;

Modified: cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java?view=diff&rev=490165&r1=490164&r2=490165
==============================================================================
--- cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java (original)
+++ cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java Mon Dec 25 12:15:56 2006
@@ -41,10 +41,6 @@
     /** the Document Builder factory */
     protected DocumentBuilderFactory factory;
 
-    /** The DOM builder. It is created lazily by {@link #setupDocumentBuilder()}
-     and cleared if a parsing error occurs. */
-    protected DocumentBuilder docBuilder;
-
     protected String documentBuilderFactoryName = "javax.xml.parsers.DocumentBuilderFactory";
 
     public String getDocumentBuilderFactoryName() {
@@ -58,7 +54,7 @@
     /**
      * Initialize the dom builder factory.
      */
-    protected void initDomBuilderFactory()
+    protected synchronized void initDomBuilderFactory()
     throws Exception {
         if ( this.factory == null ) {
             if( "javax.xml.parsers.DocumentBuilderFactory".equals( this.documentBuilderFactoryName ) ) {
@@ -73,57 +69,49 @@
     }
 
     /**
-     * Parses a new Document object from the given InputSource.
+     * @see org.apache.cocoon.core.xml.DOMParser#parseDocument(org.xml.sax.InputSource)
      */
     public Document parseDocument( final 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( this.resolver != null ) {
-            tmpBuilder.setEntityResolver( this.resolver );
-        }
+        final DocumentBuilder tmpBuilder = this.setupDocumentBuilder();
 
         final Document result = tmpBuilder.parse( input );
 
-        // Here, parsing was successful : restore builder
-        if( this.reuseParsers ) {
-            this.docBuilder = tmpBuilder;
-        }
-
         return result;
     }
 
     /**
      * Creates a new {@link DocumentBuilder} if needed.
      */
-    protected void setupDocumentBuilder()
+    protected DocumentBuilder setupDocumentBuilder()
     throws SAXException {
-        try {
-            this.initDomBuilderFactory();
-        } catch (Exception e) {
-            final String message = "Cannot initialize dom builder factory";
-            throw new SAXException( message, e );
-        }
-        if( this.docBuilder == null ) {
+        if ( this.factory == null ) {
             try {
-                this.docBuilder = this.factory.newDocumentBuilder();
-            } catch( final ParserConfigurationException pce ) {
-                final String message = "Could not create DocumentBuilder";
-                throw new SAXException( message, pce );
+                this.initDomBuilderFactory();
+            } catch (Exception e) {
+                final String message = "Cannot initialize dom builder factory";
+                throw new SAXException( message, e );
             }
         }
+        DocumentBuilder docBuilder;
+        try {
+            docBuilder = this.factory.newDocumentBuilder();
+        } catch( final ParserConfigurationException pce ) {
+            final String message = "Could not create DocumentBuilder";
+            throw new SAXException( message, pce );
+        }
+        if( this.resolver != null ) {
+            docBuilder.setEntityResolver( this.resolver );
+        }
+
+        return docBuilder;
     }
 
     /**
-     * Return a new {@link Document}.
+     * @see org.apache.cocoon.core.xml.DOMParser#createDocument()
      */
     public Document createDocument()
     throws SAXException {
-        setupDocumentBuilder();
-        return this.docBuilder.newDocument();
+        return this.setupDocumentBuilder().newDocument();
     }
 }

Modified: cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java?view=diff&rev=490165&r1=490164&r2=490165
==============================================================================
--- cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java (original)
+++ cocoon/trunk/core/cocoon-xml/cocoon-xml-impl/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java Mon Dec 25 12:15:56 2006
@@ -44,10 +44,6 @@
     /** the SAX Parser factory */
     protected SAXParserFactory factory;
 
-    /** The SAX reader. It is created lazily by {@link #setupXMLReader()}
-     and cleared if a parsing error occurs. */
-    protected XMLReader reader;
-
     /** do we want namespaces also as attributes ? */
     protected boolean nsPrefixes = false;
 
@@ -150,7 +146,7 @@
     /**
      * Initialize the sax parser factory.
      */
-    protected void initSaxParserFactory()
+    protected synchronized void initSaxParserFactory()
     throws Exception {
         if ( this.factory == null ) {
             if( "javax.xml.parsers.SAXParserFactory".equals( this.saxParserFactoryName ) ) {
@@ -165,21 +161,13 @@
     }
 
     /**
-     * Parse the <code>InputSource</code> and send
-     * SAX events to the consumer.
-     * Attention: the consumer can  implement the
-     * <code>LexicalHandler</code> as well.
-     * The parse should take care of this.
+     * @see org.apache.cocoon.core.xml.SAXParser#parse(org.xml.sax.InputSource, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler)
      */
     public void parse( final InputSource in,
                        final ContentHandler contentHandler,
                        final 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;
+        final XMLReader tmpReader = this.setupXMLReader();
 
         try {
             LexicalHandler theLexicalHandler = null;
@@ -203,19 +191,9 @@
                 "'http://xml.org/sax/properties/lexical-handler'";
             this.getLogger().warn( message );
         }
-
-        tmpReader.setErrorHandler( this );
         tmpReader.setContentHandler( contentHandler );
-        if( this.resolver != null  ) {
-            tmpReader.setEntityResolver( this.resolver );
-        }
 
         tmpReader.parse( in );
-
-        // Here, parsing was successful : restore reader
-        if ( this.reuseParsers ) {
-            this.reader = tmpReader;
-        }
     }
 
     /**
@@ -230,37 +208,44 @@
     /**
      * Creates a new {@link XMLReader} if needed.
      */
-    protected void setupXMLReader()
+    protected XMLReader setupXMLReader()
     throws SAXException {
+        if ( this.factory == null ) {
+            try {
+                this.initSaxParserFactory();
+            } catch (Exception e) {
+                final String message = "Cannot initialize sax parser factory";
+                throw new SAXException( message, e );
+            }
+        }
+        XMLReader reader;
+        // Create the XMLReader
         try {
-            this.initSaxParserFactory();
-        } catch (Exception e) {
-            final String message = "Cannot initialize sax parser factory";
-            throw new SAXException( message, e );
+            reader = this.factory.newSAXParser().getXMLReader();
+        } catch( final ParserConfigurationException pce ) {
+            final String message = "Cannot produce a valid parser";
+            throw new SAXException( message, pce );
         }
-        if( this.reader == null ) {
-            // Create the XMLReader
+
+        reader.setFeature( "http://xml.org/sax/features/namespaces", true );
+
+        if( this.nsPrefixes ) {
             try {
-                this.reader = this.factory.newSAXParser().getXMLReader();
-            } catch( final ParserConfigurationException pce ) {
-                final String message = "Cannot produce a valid parser";
-                throw new SAXException( message, pce );
-            }
-            
-            this.reader.setFeature( "http://xml.org/sax/features/namespaces", true );
-            
-            if( this.nsPrefixes ) {
-                try {
-                    this.reader.setFeature( "http://xml.org/sax/features/namespace-prefixes",
-                                            this.nsPrefixes );
-                } catch( final SAXException se ) {
-                    final String message =
-                        "SAX2 XMLReader does not support setting feature: " +
-                        "'http://xml.org/sax/features/namespace-prefixes'";
-                    this.getLogger().warn( message );
-                }
+                reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
+                                  this.nsPrefixes );
+            } catch( final SAXException se ) {
+                final String message =
+                    "SAX2 XMLReader does not support setting feature: " +
+                    "'http://xml.org/sax/features/namespace-prefixes'";
+                this.getLogger().warn( message );
             }
         }
+        reader.setErrorHandler( this );
+        if( this.resolver != null  ) {
+            reader.setEntityResolver( this.resolver );
+        }
+
+        return reader;
     }
 
     /**