| 1 16 17 package xni.parser; 18 19 import java.io.FileInputStream ; 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Hashtable ; 25 import java.util.Locale ; 26 import java.util.Vector ; 27 28 import org.apache.xerces.xni.XMLDocumentHandler; 29 import org.apache.xerces.xni.XMLDTDHandler; 30 import org.apache.xerces.xni.XMLDTDContentModelHandler; 31 import org.apache.xerces.xni.XNIException; 32 33 import org.apache.xerces.xni.parser.XMLComponent; 34 import org.apache.xerces.xni.parser.XMLConfigurationException; 35 import org.apache.xerces.xni.parser.XMLEntityResolver; 36 import org.apache.xerces.xni.parser.XMLErrorHandler; 37 import org.apache.xerces.xni.parser.XMLInputSource; 38 import org.apache.xerces.xni.parser.XMLParserConfiguration; 39 40 57 public abstract class AbstractConfiguration 58 implements XMLParserConfiguration { 59 60 64 66 67 protected final Vector fRecognizedFeatures = new Vector (); 68 69 70 protected final Vector fRecognizedProperties = new Vector (); 71 72 73 protected final Hashtable fFeatures = new Hashtable (); 74 75 76 protected final Hashtable fProperties = new Hashtable (); 77 78 80 81 protected XMLEntityResolver fEntityResolver; 82 83 84 protected XMLErrorHandler fErrorHandler; 85 86 87 protected XMLDocumentHandler fDocumentHandler; 88 89 90 protected XMLDTDHandler fDTDHandler; 91 92 93 protected XMLDTDContentModelHandler fDTDContentModelHandler; 94 95 96 protected Locale fLocale; 97 98 100 101 protected final Vector fComponents = new Vector (); 102 103 107 114 public void addRecognizedFeatures(String [] featureIds) { 115 int length = featureIds != null ? featureIds.length : 0; 116 for (int i = 0; i < length; i++) { 117 String featureId = featureIds[i]; 118 if (!fRecognizedFeatures.contains(featureId)) { 119 fRecognizedFeatures.addElement(featureId); 120 } 121 } 122 } 124 134 public void setFeature(String featureId, boolean state) 135 throws XMLConfigurationException { 136 if (!fRecognizedFeatures.contains(featureId)) { 137 short type = XMLConfigurationException.NOT_RECOGNIZED; 138 throw new XMLConfigurationException(type, featureId); 139 } 140 fFeatures.put(featureId, state ? Boolean.TRUE : Boolean.FALSE); 141 int length = fComponents.size(); 142 for (int i = 0; i < length; i++) { 143 XMLComponent component = (XMLComponent)fComponents.elementAt(i); 144 component.setFeature(featureId, state); 145 } 146 } 148 156 public boolean getFeature(String featureId) 157 throws XMLConfigurationException { 158 if (!fRecognizedFeatures.contains(featureId)) { 159 short type = XMLConfigurationException.NOT_RECOGNIZED; 160 throw new XMLConfigurationException(type, featureId); 161 } 162 Boolean state = (Boolean )fFeatures.get(featureId); 163 return state != null ? state.booleanValue() : false; 164 } 166 173 public void addRecognizedProperties(String [] propertyIds) { 174 int length = propertyIds != null ? propertyIds.length : 0; 175 for (int i = 0; i < length; i++) { 176 String propertyId = propertyIds[i]; 177 if (!fRecognizedProperties.contains(propertyId)) { 178 fRecognizedProperties.addElement(propertyId); 179 } 180 } 181 } 183 193 public void setProperty(String propertyId, Object value) 194 throws XMLConfigurationException { 195 if (!fRecognizedProperties.contains(propertyId)) { 196 short type = XMLConfigurationException.NOT_RECOGNIZED; 197 throw new XMLConfigurationException(type, propertyId); 198 } 199 if (value != null) { 200 fProperties.put(propertyId, value); 201 } 202 else { 203 fProperties.remove(propertyId); 204 } 205 int length = fComponents.size(); 206 for (int i = 0; i < length; i++) { 207 XMLComponent component = (XMLComponent)fComponents.elementAt(i); 208 component.setProperty(propertyId, value); 209 } 210 } 212 220 public Object getProperty(String propertyId) 221 throws XMLConfigurationException { 222 if (!fRecognizedProperties.contains(propertyId)) { 223 short type = XMLConfigurationException.NOT_RECOGNIZED; 224 throw new XMLConfigurationException(type, propertyId); 225 } 226 Object value = fProperties.get(propertyId); 227 return value; 228 } 230 235 public void setEntityResolver(XMLEntityResolver resolver) { 236 fEntityResolver = resolver; 237 } 239 240 public XMLEntityResolver getEntityResolver() { 241 return fEntityResolver; 242 } 244 249 public void setErrorHandler(XMLErrorHandler handler) { 250 fErrorHandler = handler; 251 } 253 254 public XMLErrorHandler getErrorHandler() { 255 return fErrorHandler; 256 } 258 263 public void setDocumentHandler(XMLDocumentHandler handler) { 264 fDocumentHandler = handler; 265 } 267 268 public XMLDocumentHandler getDocumentHandler() { 269 return fDocumentHandler; 270 } 272 277 public void setDTDHandler(XMLDTDHandler handler) { 278 fDTDHandler = handler; 279 } 281 282 public XMLDTDHandler getDTDHandler() { 283 return fDTDHandler; 284 } 286 291 public void setDTDContentModelHandler(XMLDTDContentModelHandler handler) { 292 fDTDContentModelHandler = handler; 293 } 295 296 public XMLDTDContentModelHandler getDTDContentModelHandler() { 297 return fDTDContentModelHandler; 298 } 300 327 public abstract void parse(XMLInputSource inputSource) 328 throws IOException , XNIException; 329 330 338 public void setLocale(Locale locale) { 339 fLocale = locale; 340 } 342 343 344 public Locale getLocale() { 345 return fLocale; 346 } 348 352 365 protected void addComponent(XMLComponent component) { 366 if (!fComponents.contains(component)) { 367 fComponents.addElement(component); 368 addRecognizedFeatures(component.getRecognizedFeatures()); 369 addRecognizedProperties(component.getRecognizedProperties()); 370 } 371 } 373 380 protected void resetComponents() 381 throws XMLConfigurationException { 382 int length = fComponents.size(); 383 for (int i = 0; i < length; i++) { 384 XMLComponent component = (XMLComponent)fComponents.elementAt(i); 385 component.reset(this); 386 } 387 } 389 399 protected void openInputSourceStream(XMLInputSource source) 400 throws IOException { 401 if (source.getCharacterStream() != null) { 402 return; 403 } 404 InputStream stream = source.getByteStream(); 405 if (stream == null) { 406 String systemId = source.getSystemId(); 407 try { 408 URL url = new URL (systemId); 409 stream = url.openStream(); 410 } 411 catch (MalformedURLException e) { 412 stream = new FileInputStream (systemId); 413 } 414 source.setByteStream(stream); 415 } 416 } 418 } | Popular Tags |