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.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 35 public class DOMFactoryImpl extends DocumentBuilderFactory { 36 private static Class first; 37 38 private Map <String , Object > attributes = new HashMap <String , Object >(); 39 40 41 private static final String Factory_PROP = 42 "javax.xml.parsers.DocumentBuilderFactory"; 44 public static void install() { 45 System.getProperties().put(Factory_PROP, 46 DOMFactoryImpl.class.getName()); 47 } 48 49 static { 50 ClassLoader orig = Thread.currentThread().getContextClassLoader(); 51 try { 53 Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader().getParent()); 54 first = DocumentBuilderFactory.newInstance().getClass(); 55 } finally { 56 Thread.currentThread().setContextClassLoader(orig); 57 } 58 59 DOMFactoryImpl.install(); 60 SAXFactoryImpl.install(); 61 } 62 63 public java.lang.Object getAttribute(java.lang.String name) throws java.lang.IllegalArgumentException { 64 return attributes.get(name); 65 } 66 67 public boolean getFeature (String name) { 68 return Boolean.TRUE.equals (getAttribute (name)); 69 } 70 71 public void setFeature(String name, boolean value) throws ParserConfigurationException { 72 try { 73 setAttribute (name, Boolean.valueOf (value)); 74 } catch (IllegalArgumentException ex) { 75 ParserConfigurationException p = new ParserConfigurationException (); 76 p.initCause (ex); 77 throw p; 78 } 79 } 80 81 82 83 public DocumentBuilder newDocumentBuilder() throws javax.xml.parsers.ParserConfigurationException { 84 try { 85 return tryCreate(); 86 } catch (IllegalArgumentException e) { 87 throw (ParserConfigurationException ) new ParserConfigurationException (e.toString()).initCause(e); 88 } 89 } 90 91 92 public void setAttribute(java.lang.String name, java.lang.Object value) throws java.lang.IllegalArgumentException { 93 attributes.put(name, value); 94 try { 95 tryCreate(); 96 } catch (ParserConfigurationException e) { 97 throw (IllegalArgumentException ) new IllegalArgumentException (e.toString()).initCause(e); 98 } 99 } 100 101 private DocumentBuilder tryCreate() throws ParserConfigurationException , IllegalArgumentException { 102 for (Iterator it = new LazyIterator(first, DocumentBuilderFactory .class, DOMFactoryImpl.class); it.hasNext(); ) { 103 try { 104 DocumentBuilder builder = tryCreate((Class )it.next()); 105 return builder; 106 } catch (ParserConfigurationException e) { 107 if (!it.hasNext()) throw e; 108 } catch (IllegalArgumentException e) { 109 if (!it.hasNext()) throw e; 110 } 111 } 112 throw new IllegalStateException ("Can't get here!"); } 114 115 private DocumentBuilder tryCreate(Class delClass) throws ParserConfigurationException , IllegalArgumentException { 116 Exception ex = null; 117 try { 118 DocumentBuilderFactory delegate = (DocumentBuilderFactory )delClass.newInstance(); 119 delegate.setNamespaceAware(isNamespaceAware()); 120 delegate.setValidating(isValidating()); 121 delegate.setIgnoringElementContentWhitespace(isIgnoringElementContentWhitespace()); 122 delegate.setExpandEntityReferences(isExpandEntityReferences()); 123 delegate.setIgnoringComments(isIgnoringComments()); 124 delegate.setCoalescing(isCoalescing()); 125 126 for (Iterator it = attributes.entrySet().iterator(); it.hasNext(); ) { 127 Map.Entry entry = (Map.Entry )it.next(); 128 delegate.setAttribute((String )entry.getKey(), entry.getValue()); 129 } 130 return delegate.newDocumentBuilder(); 131 } catch (InstantiationException e) { 132 ex = e; 133 } catch (IllegalAccessException e) { 134 ex = e; 135 } 136 throw (ParserConfigurationException ) new ParserConfigurationException ("Broken factory").initCause(ex); } 138 } 139 | Popular Tags |