FileDocCategorySizeDatePackage
XMLReaderManager.javaAPI DocJava SE 6 API5983Tue Jun 10 00:23:12 BST 2008com.sun.org.apache.xml.internal.utils

XMLReaderManager

public class XMLReaderManager extends Object
Creates XMLReader objects and caches them for re-use. This class follows the singleton pattern.

Fields Summary
private static final String
NAMESPACES_FEATURE
private static final String
NAMESPACE_PREFIXES_FEATURE
private static final XMLReaderManager
m_singletonManager
private static SAXParserFactory
m_parserFactory
Parser factory to be used to construct XMLReader objects
private ThreadLocal
m_readers
Cache of XMLReader objects
private Hashtable
m_inUse
Keeps track of whether an XMLReader object is in use.
Constructors Summary
private XMLReaderManager()
Hidden constructor


           
      
    
Methods Summary
public static com.sun.org.apache.xml.internal.utils.XMLReaderManagergetInstance()
Retrieves the singleton reader manager

        return m_singletonManager;
    
public synchronized org.xml.sax.XMLReadergetXMLReader()
Retrieves a cached XMLReader for this thread, or creates a new XMLReader, if the existing reader is in use. When the caller no longer needs the reader, it must release it with a call to {@link #releaseXMLReader}.

        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 unless it isn't an 
        // instance of the class set in the 'org.xml.sax.driver' property
        reader = (XMLReader) m_readers.get();
        boolean threadHasReader = (reader != null);
        String factory = SecuritySupport.getInstance().getSystemProperty("org.xml.sax.driver");
        if (!threadHasReader || m_inUse.get(reader) == Boolean.TRUE ||
                !reader.getClass().getName().equals(factory)) {
            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.
                }
            } catch (ParserConfigurationException ex) {
                throw new SAXException(ex);
            } catch (FactoryConfigurationError ex1) {
                throw new SAXException(ex1.toString());
            } catch (NoSuchMethodError ex2) {
            } catch (AbstractMethodError ame) {
            }

            // 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);
        }

        return reader;
    
public synchronized voidreleaseXMLReader(org.xml.sax.XMLReader reader)
Mark the cached XMLReader as available. If the reader was not actually in the cache, do nothing.

param
reader The XMLReader that's being released.

        // 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);
        }