--- xalan-j_2_7_1/src/org/apache/xml/utils/XMLReaderManager.java	2007-11-22 13:43:53.000000000 -0800
+++ xalan-j_2_7_1.kev/src/org/apache/xml/utils/XMLReaderManager.java	2011-08-29 12:17:30.000000000 -0700
@@ -20,14 +20,12 @@
  */
 package org.apache.xml.utils;
 
-import java.util.Hashtable;
-
 import javax.xml.parsers.FactoryConfigurationError;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
 import org.xml.sax.XMLReader;
-import org.xml.sax.helpers.XMLReaderFactory;
 import org.xml.sax.SAXException;
 
 /**
@@ -36,10 +34,6 @@
  */
 public class XMLReaderManager {
 
-    private static final String NAMESPACES_FEATURE =
-                             "http://xml.org/sax/features/namespaces";
-    private static final String NAMESPACE_PREFIXES_FEATURE =
-                             "http://xml.org/sax/features/namespace-prefixes";
     private static final XMLReaderManager m_singletonManager =
                                                      new XMLReaderManager();
 
@@ -49,14 +43,13 @@
     private static SAXParserFactory m_parserFactory;
 
     /**
-     * Cache of XMLReader objects
+     * Cached SAXParser object
      */
-    private ThreadLocal m_readers;
-
+    private ThreadLocal m_parser = new ThreadLocal();
     /**
-     * Keeps track of whether an XMLReader object is in use.
+     * Active XMLReader
      */
-    private Hashtable m_inUse;
+    private ThreadLocal m_activeReader = new ThreadLocal();
 
     /**
      * Hidden constructor
@@ -77,72 +70,31 @@
      * longer needs the reader, it must release it with a call to
      * {@link #releaseXMLReader}.
      */
-    public synchronized XMLReader getXMLReader() throws SAXException {
-        XMLReader reader;
-        boolean readerInUse;
-
-        if (m_readers == null) {
-            // When the m_readers.get() method is called for the first time
-            // on a thread, a new XMLReader will automatically be created.
-            m_readers = new ThreadLocal();
-        }
-
-        if (m_inUse == null) {
-            m_inUse = new Hashtable();
-        }
-
-        // If the cached reader for this thread is in use, construct a new
-        // one; otherwise, return the cached reader.
-        reader = (XMLReader) m_readers.get();
-        boolean threadHasReader = (reader != null);
-        if (!threadHasReader || m_inUse.get(reader) == Boolean.TRUE) {
-            try {
-                try {
-                    // According to JAXP 1.2 specification, if a SAXSource
-                    // is created using a SAX InputSource the Transformer or
-                    // TransformerFactory creates a reader via the
-                    // XMLReaderFactory if setXMLReader is not used
-                    reader = XMLReaderFactory.createXMLReader();
-                } catch (Exception e) {
-                   try {
-                        // If unable to create an instance, let's try to use
-                        // the XMLReader from JAXP
-                        if (m_parserFactory == null) {
-                            m_parserFactory = SAXParserFactory.newInstance();
-                            m_parserFactory.setNamespaceAware(true);
-                        }
-
-                        reader = m_parserFactory.newSAXParser().getXMLReader();
-                   } catch (ParserConfigurationException pce) {
-                       throw pce;   // pass along pce
-                   }
-                }
-                try {
-                    reader.setFeature(NAMESPACES_FEATURE, true);
-                    reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false);
-                } catch (SAXException se) {
-                    // Try to carry on if we've got a parser that
-                    // doesn't know about namespace prefixes.
+    public XMLReader getXMLReader() throws SAXException {
+        try {
+            if (m_activeReader.get() == null) {
+                final SAXParser cached = (SAXParser) m_parser.get() ;
+                final XMLReader reader ;
+                if (cached != null) {
+                    reader = cached.getXMLReader() ;
+                } else {
+                    final SAXParser parser = m_parserFactory.newSAXParser() ;
+                    m_parser.set(parser) ;
+                    reader = parser.getXMLReader() ;
                 }
-            } catch (ParserConfigurationException ex) {
-                throw new SAXException(ex);
-            } catch (FactoryConfigurationError ex1) {
-                throw new SAXException(ex1.toString());
-            } catch (NoSuchMethodError ex2) {
-            } catch (AbstractMethodError ame) {
+                m_activeReader.set(reader) ;
+                return reader ;
+            } else{
+                return m_parserFactory.newSAXParser().getXMLReader();
             }
-
-            // Cache the XMLReader if this is the first time we've created
-            // a reader for this thread.
-            if (!threadHasReader) {
-                m_readers.set(reader);
-                m_inUse.put(reader, Boolean.TRUE);
-            }
-        } else {
-            m_inUse.put(reader, Boolean.TRUE);
+        } catch (ParserConfigurationException ex) {
+            throw new SAXException(ex);
+        } catch (FactoryConfigurationError ex1) {
+            throw new SAXException(ex1.toString());
+        } catch (NoSuchMethodError ex2) {
+        } catch (AbstractMethodError ame) {
         }
-
-        return reader;
+        return null ;
     }
 
     /**
@@ -151,11 +103,19 @@
      *
      * @param reader The XMLReader that's being released.
      */
-    public synchronized void releaseXMLReader(XMLReader reader) {
-        // If the reader that's being released is the cached reader
-        // for this thread, remove it from the m_isUse list.
-        if (m_readers.get() == reader && reader != null) {
-            m_inUse.remove(reader);
-        }
+    public void releaseXMLReader(XMLReader reader) {
+    	if (reader == m_activeReader.get()) {
+    		m_activeReader.set(null) ;
+    		try {
+        		((SAXParser)m_parser.get()).reset() ;
+    		} catch (final Throwable th) {
+    			m_parser.set(null) ;
+    		}
+    	}
+    }
+
+    static {
+        m_parserFactory = SAXParserFactory.newInstance();
+        m_parserFactory.setNamespaceAware(true);
     }
 }
