1 16 19 package com.sun.org.apache.xml.internal.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(); 96 boolean threadHasReader = (reader != null); 97 String factory = SecuritySupport.getInstance().getSystemProperty("org.xml.sax.driver"); 98 if (!threadHasReader || m_inUse.get(reader) == Boolean.TRUE || 99 !reader.getClass().getName().equals(factory)) { 100 try { 101 try { 102 reader = XMLReaderFactory.createXMLReader(); 107 } catch (Exception e) { 108 try { 109 if (m_parserFactory == null) { 112 m_parserFactory = SAXParserFactory.newInstance(); 113 m_parserFactory.setNamespaceAware(true); 114 } 115 116 reader = m_parserFactory.newSAXParser().getXMLReader(); 117 } catch (ParserConfigurationException pce) { 118 throw pce; } 120 } 121 try { 122 reader.setFeature(NAMESPACES_FEATURE, true); 123 reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false); 124 } catch (SAXException se) { 125 } 128 } catch (ParserConfigurationException ex) { 129 throw new SAXException (ex); 130 } catch (FactoryConfigurationError ex1) { 131 throw new SAXException (ex1.toString()); 132 } catch (NoSuchMethodError ex2) { 133 } catch (AbstractMethodError ame) { 134 } 135 136 if (!threadHasReader) { 139 m_readers.set(reader); 140 m_inUse.put(reader, Boolean.TRUE); 141 } 142 } else { 143 m_inUse.put(reader, Boolean.TRUE); 144 } 145 146 return reader; 147 } 148 149 155 public synchronized void releaseXMLReader(XMLReader reader) { 156 if (m_readers.get() == reader && reader != null) { 159 m_inUse.remove(reader); 160 } 161 } 162 } 163 | Popular Tags |