1 19 20 package org.netbeans.core.startup; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.parsers.SAXParser ; 27 import javax.xml.parsers.SAXParserFactory ; 28 import org.xml.sax.SAXException ; 29 import org.xml.sax.SAXNotRecognizedException ; 30 import org.xml.sax.SAXNotSupportedException ; 31 32 38 public class SAXFactoryImpl extends SAXParserFactory { 39 private static Class first; 40 41 private Map <String ,Boolean > features = new HashMap <String ,Boolean >(); 42 43 44 private static final String SAXParserFactory_PROP = 45 "javax.xml.parsers.SAXParserFactory"; 47 public static void install() { 48 System.getProperties().put(SAXParserFactory_PROP, 49 SAXFactoryImpl.class.getName()); 50 } 51 52 static { 53 ClassLoader orig = Thread.currentThread().getContextClassLoader(); 54 try { 56 Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader().getParent()); 57 first = SAXParserFactory.newInstance().getClass(); 58 } finally { 59 Thread.currentThread().setContextClassLoader(orig); 60 } 61 DOMFactoryImpl.install(); 62 SAXFactoryImpl.install(); 63 } 64 65 public boolean getFeature(String name) throws ParserConfigurationException , SAXNotRecognizedException , SAXNotSupportedException { 66 return features.get(name); 67 } 68 69 public javax.xml.parsers.SAXParser newSAXParser() throws ParserConfigurationException , SAXException { 70 SAXParser parser = tryCreate(); 71 return parser; 72 } 73 74 public void setFeature(java.lang.String name, boolean value) throws ParserConfigurationException , SAXNotRecognizedException , SAXNotSupportedException { 75 features.put(name, Boolean.valueOf(value)); 76 tryCreate(); 77 } 78 79 private SAXParser tryCreate() throws ParserConfigurationException , SAXNotRecognizedException , SAXNotSupportedException { 80 for (Iterator it = new LazyIterator(first, SAXParserFactory .class, SAXFactoryImpl.class); it.hasNext(); ) { 81 try { 82 SAXParser parser = tryCreate((Class )it.next()); 83 return parser; 84 } catch (ParserConfigurationException e) { 85 if (!it.hasNext()) throw e; 86 } catch (SAXNotRecognizedException e) { 87 if (!it.hasNext()) throw e; 88 } catch (SAXNotSupportedException e) { 89 if (!it.hasNext()) throw e; 90 } catch (SAXException e) { 91 if (!it.hasNext()) throw new ParserConfigurationException (); 92 } 93 } 94 throw new IllegalStateException ("Can't get here!"); } 96 97 private SAXParser tryCreate(Class delClass) throws ParserConfigurationException , SAXException { 98 Exception ex = null; 99 try { 100 SAXParserFactory delegate = (SAXParserFactory )delClass.newInstance(); 101 delegate.setValidating(isValidating()); 102 delegate.setNamespaceAware(isNamespaceAware()); 103 for (Iterator it = features.entrySet().iterator(); it.hasNext(); ) { 104 Map.Entry entry = (Map.Entry )it.next(); 105 delegate.setFeature((String )entry.getKey(), ((Boolean )entry.getValue()).booleanValue()); 106 } 107 return delegate.newSAXParser(); 108 } catch (InstantiationException e) { 109 ex = e; 110 } catch (IllegalAccessException e) { 111 ex = e; 112 } 113 throw (ParserConfigurationException ) new ParserConfigurationException ("Broken factory").initCause(ex); } 115 } 116 | Popular Tags |