1 16 17 package org.apache.xerces.jaxp; 18 19 import java.util.Hashtable ; 20 21 import javax.xml.XMLConstants ; 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.validation.Schema ; 26 27 import org.apache.xerces.parsers.DOMParser; 28 import org.apache.xerces.util.SAXMessageFormatter; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.SAXNotRecognizedException ; 31 import org.xml.sax.SAXNotSupportedException ; 32 33 38 public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory { 39 40 private Hashtable attributes; 41 private Hashtable features; 42 private Schema grammar; 43 private boolean isXIncludeAware; 44 45 48 private boolean fSecureProcess = false; 49 50 54 public DocumentBuilder newDocumentBuilder() 55 throws ParserConfigurationException 56 { 57 58 if (grammar != null && attributes != null) { 59 if (attributes.containsKey(JAXPConstants.JAXP_SCHEMA_LANGUAGE)) { 60 throw new ParserConfigurationException ( 61 SAXMessageFormatter.formatMessage(null, 62 "schema-already-specified", new Object [] {JAXPConstants.JAXP_SCHEMA_LANGUAGE})); 63 } 64 else if (attributes.containsKey(JAXPConstants.JAXP_SCHEMA_SOURCE)) { 65 throw new ParserConfigurationException ( 66 SAXMessageFormatter.formatMessage(null, 67 "schema-already-specified", new Object [] {JAXPConstants.JAXP_SCHEMA_SOURCE})); 68 } 69 } 70 71 try { 72 return new DocumentBuilderImpl(this, attributes, features, fSecureProcess); 73 } catch (SAXException se) { 74 throw new ParserConfigurationException (se.getMessage()); 76 } 77 } 78 79 85 public void setAttribute(String name, Object value) 86 throws IllegalArgumentException 87 { 88 if (value == null) { 90 if (attributes != null) { 91 attributes.remove(name); 92 } 93 return; 95 } 96 97 100 if (attributes == null) { 102 attributes = new Hashtable (); 103 } 104 105 attributes.put(name, value); 106 107 try { 109 new DocumentBuilderImpl(this, attributes, features); 110 } catch (Exception e) { 111 attributes.remove(name); 112 throw new IllegalArgumentException (e.getMessage()); 113 } 114 } 115 116 120 public Object getAttribute(String name) 121 throws IllegalArgumentException 122 { 123 if (attributes != null) { 125 Object val = attributes.get(name); 126 if (val != null) { 127 return val; 128 } 129 } 130 131 DOMParser domParser = null; 132 try { 133 domParser = 136 new DocumentBuilderImpl(this, attributes, features).getDOMParser(); 137 return domParser.getProperty(name); 138 } catch (SAXException se1) { 139 try { 141 boolean result = domParser.getFeature(name); 142 return result ? Boolean.TRUE : Boolean.FALSE; 144 } catch (SAXException se2) { 145 throw new IllegalArgumentException (se1.getMessage()); 147 } 148 } 149 } 150 151 public Schema getSchema() { 152 return grammar; 153 } 154 155 public void setSchema(Schema grammar) { 156 this.grammar = grammar; 157 } 158 159 public boolean isXIncludeAware() { 160 return this.isXIncludeAware; 161 } 162 163 public void setXIncludeAware(boolean state) { 164 this.isXIncludeAware = state; 165 } 166 167 public boolean getFeature(String name) 168 throws ParserConfigurationException { 169 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 170 return fSecureProcess; 171 } 172 if (features != null) { 174 Object val = features.get(name); 175 if (val != null) { 176 return ((Boolean ) val).booleanValue(); 177 } 178 } 179 try { 180 DOMParser domParser = new DocumentBuilderImpl(this, attributes, features).getDOMParser(); 181 return domParser.getFeature(name); 182 } 183 catch (SAXException e) { 184 throw new ParserConfigurationException (e.getMessage()); 185 } 186 } 187 188 public void setFeature(String name, boolean value) 189 throws ParserConfigurationException { 190 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 192 fSecureProcess = value; 193 return; 194 } 195 if (features == null) { 196 features = new Hashtable (); 197 } 198 features.put(name, value ? Boolean.TRUE : Boolean.FALSE); 199 try { 201 new DocumentBuilderImpl(this, attributes, features); 202 } 203 catch (SAXNotSupportedException e) { 204 features.remove(name); 205 throw new ParserConfigurationException (e.getMessage()); 206 } 207 catch (SAXNotRecognizedException e) { 208 features.remove(name); 209 throw new ParserConfigurationException (e.getMessage()); 210 } 211 } 212 } 213 | Popular Tags |