1 16 19 package org.apache.xml.utils; 20 21 import java.util.Hashtable ; 22 23 import javax.xml.parsers.FactoryConfigurationError ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.parsers.SAXParserFactory ; 26 27 import org.xml.sax.XMLReader ; 28 import org.xml.sax.helpers.XMLReaderFactory ; 29 import org.xml.sax.SAXException ; 30 31 35 public class XMLReaderManager { 36 37 private static final String NAMESPACES_FEATURE = 38 "http://xml.org/sax/features/namespaces"; 39 private static final String NAMESPACE_PREFIXES_FEATURE = 40 "http://xml.org/sax/features/namespace-prefixes"; 41 private static final XMLReaderManager m_singletonManager = 42 new XMLReaderManager(); 43 44 47 private static SAXParserFactory m_parserFactory; 48 49 52 private ThreadLocal m_readers; 53 54 57 private Hashtable m_inUse; 58 59 62 private XMLReaderManager() { 63 } 64 65 68 public static XMLReaderManager getInstance() { 69 return m_singletonManager; 70 } 71 72 78 public synchronized XMLReader getXMLReader() throws SAXException { 79 XMLReader reader; 80 boolean readerInUse; 81 82 if (m_readers == null) { 83 m_readers = new ThreadLocal (); 86 } 87 88 if (m_inUse == null) { 89 m_inUse = new Hashtable (); 90 } 91 92 reader = (XMLReader ) m_readers.get(); 95 boolean threadHasReader = (reader != null); 96 if (!threadHasReader || m_inUse.get(reader) == Boolean.TRUE) { 97 try { 98 try { 99 reader = XMLReaderFactory.createXMLReader(); 104 } catch (Exception e) { 105 try { 106 if (m_parserFactory == null) { 109 m_parserFactory = SAXParserFactory.newInstance(); 110 m_parserFactory.setNamespaceAware(true); 111 } 112 113 reader = m_parserFactory.newSAXParser().getXMLReader(); 114 } catch (ParserConfigurationException pce) { 115 throw pce; } 117 } 118 try { 119 reader.setFeature(NAMESPACES_FEATURE, true); 120 reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false); 121 } catch (SAXException se) { 122 } 125 } catch (ParserConfigurationException ex) { 126 throw new SAXException (ex); 127 } catch (FactoryConfigurationError ex1) { 128 throw new SAXException (ex1.toString()); 129 } catch (NoSuchMethodError ex2) { 130 } catch (AbstractMethodError ame) { 131 } 132 133 if (!threadHasReader) { 136 m_readers.set(reader); 137 m_inUse.put(reader, Boolean.TRUE); 138 } 139 } else { 140 m_inUse.put(reader, Boolean.TRUE); 141 } 142 143 return reader; 144 } 145 146 152 public synchronized void releaseXMLReader(XMLReader reader) { 153 if (m_readers.get() == reader) { 156 m_inUse.put(reader, Boolean.FALSE); 157 } 158 } 159 } 160 | Popular Tags |