1 16 17 package org.apache.xerces.impl.dtd; 18 19 import org.apache.xerces.impl.Constants; 20 import org.apache.xerces.impl.XMLDTDScannerImpl; 21 import org.apache.xerces.impl.XMLErrorReporter; 22 import org.apache.xerces.impl.XMLEntityManager; 23 import org.apache.xerces.impl.msg.XMLMessageFormatter; 24 25 import org.apache.xerces.util.SymbolTable; 26 import org.apache.xerces.util.DefaultErrorHandler; 27 28 import org.apache.xerces.xni.XNIException; 29 import org.apache.xerces.xni.grammars.XMLGrammarPool; 30 import org.apache.xerces.xni.grammars.XMLGrammarLoader; 31 import org.apache.xerces.xni.grammars.Grammar; 32 import org.apache.xerces.xni.parser.XMLConfigurationException; 33 import org.apache.xerces.xni.parser.XMLErrorHandler; 34 import org.apache.xerces.xni.parser.XMLEntityResolver; 35 import org.apache.xerces.xni.parser.XMLInputSource; 36 37 import java.util.Locale ; 38 import java.io.IOException ; 39 import java.io.EOFException ; 40 41 64 public class XMLDTDLoader 65 extends XMLDTDProcessor 66 implements XMLGrammarLoader { 67 68 72 74 75 protected static final String STANDARD_URI_CONFORMANT_FEATURE = 76 Constants.XERCES_FEATURE_PREFIX + Constants.STANDARD_URI_CONFORMANT_FEATURE; 77 78 private static final String [] RECOGNIZED_FEATURES = { 80 VALIDATION, 81 WARN_ON_DUPLICATE_ATTDEF, 82 NOTIFY_CHAR_REFS, 83 STANDARD_URI_CONFORMANT_FEATURE 84 }; 85 86 88 89 protected static final String ERROR_HANDLER = 90 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 91 92 93 public static final String ENTITY_RESOLVER = 94 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 95 96 97 private static final String [] LOADER_RECOGNIZED_PROPERTIES = { 98 SYMBOL_TABLE, 99 ERROR_REPORTER, 100 ERROR_HANDLER, 101 ENTITY_RESOLVER, 102 GRAMMAR_POOL, 103 DTD_VALIDATOR, 104 }; 105 106 private boolean fStrictURI = false; 108 109 110 protected XMLEntityResolver fEntityResolver; 111 112 protected XMLDTDScannerImpl fDTDScanner; 114 115 protected XMLEntityManager fEntityManager; 117 118 protected Locale fLocale; 120 121 125 126 public XMLDTDLoader() { 127 this(new SymbolTable()); 128 } 130 public XMLDTDLoader(SymbolTable symbolTable) { 131 this(symbolTable, null); 132 } 134 public XMLDTDLoader(SymbolTable symbolTable, 135 XMLGrammarPool grammarPool) { 136 this(symbolTable, grammarPool, null, new XMLEntityManager()); 137 } 139 XMLDTDLoader(SymbolTable symbolTable, 140 XMLGrammarPool grammarPool, XMLErrorReporter errorReporter, 141 XMLEntityResolver entityResolver) { 142 fSymbolTable = symbolTable; 143 fGrammarPool = grammarPool; 144 if(errorReporter == null) { 145 errorReporter = new XMLErrorReporter(); 146 errorReporter.setProperty(ERROR_HANDLER, new DefaultErrorHandler()); 147 } 148 fErrorReporter = errorReporter; 149 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 151 XMLMessageFormatter xmft = new XMLMessageFormatter(); 152 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 153 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 154 } 155 fEntityResolver = entityResolver; 156 if(fEntityResolver instanceof XMLEntityManager) { 157 fEntityManager = (XMLEntityManager)fEntityResolver; 158 } else { 159 fEntityManager = new XMLEntityManager(); 160 } 161 fEntityManager.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY, errorReporter); 162 fDTDScanner = new XMLDTDScannerImpl(fSymbolTable, fErrorReporter, fEntityManager); 163 fDTDScanner.setDTDHandler(this); 164 fDTDScanner.setDTDContentModelHandler(this); 165 reset(); 166 } 168 170 185 public void setFeature(String featureId, boolean state) 186 throws XMLConfigurationException { 187 if(featureId.equals(VALIDATION)) { 188 fValidation = state; 189 } else if(featureId.equals(WARN_ON_DUPLICATE_ATTDEF)) { 190 fWarnDuplicateAttdef = state; 191 } else if(featureId.equals(NOTIFY_CHAR_REFS)) { 192 fDTDScanner.setFeature(featureId, state); 193 } else if(featureId.equals(STANDARD_URI_CONFORMANT_FEATURE)) { 194 fStrictURI = state; 195 } else { 196 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, featureId); 197 } 198 } 200 205 public String [] getRecognizedProperties() { 206 return (String [])(LOADER_RECOGNIZED_PROPERTIES.clone()); 207 } 209 216 public Object getProperty(String propertyId) 217 throws XMLConfigurationException { 218 if(propertyId.equals( SYMBOL_TABLE)) { 219 return fSymbolTable; 220 } else if(propertyId.equals( ERROR_REPORTER)) { 221 return fErrorReporter; 222 } else if(propertyId.equals( ERROR_HANDLER)) { 223 return fErrorReporter.getErrorHandler(); 224 } else if(propertyId.equals( ENTITY_RESOLVER)) { 225 return fEntityResolver; 226 } else if(propertyId.equals( GRAMMAR_POOL)) { 227 return fGrammarPool; 228 } else if(propertyId.equals( DTD_VALIDATOR)) { 229 return fValidator; 230 } 231 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, propertyId); 232 } 234 249 public void setProperty(String propertyId, Object value) 250 throws XMLConfigurationException { 251 if(propertyId.equals( SYMBOL_TABLE)) { 252 fSymbolTable = (SymbolTable)value; 253 fDTDScanner.setProperty(propertyId, value); 254 fEntityManager.setProperty(propertyId, value); 255 } else if(propertyId.equals( ERROR_REPORTER)) { 256 fErrorReporter = (XMLErrorReporter)value; 257 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 259 XMLMessageFormatter xmft = new XMLMessageFormatter(); 260 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 261 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 262 } 263 fDTDScanner.setProperty(propertyId, value); 264 fEntityManager.setProperty(propertyId, value); 265 } else if(propertyId.equals( ERROR_HANDLER)) { 266 fErrorReporter.setProperty(propertyId, value); 267 } else if(propertyId.equals( ENTITY_RESOLVER)) { 268 fEntityResolver = (XMLEntityResolver)value; 269 } else if(propertyId.equals( GRAMMAR_POOL)) { 270 fGrammarPool = (XMLGrammarPool)value; 271 } else { 272 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, propertyId); 273 } 274 } 276 283 public boolean getFeature(String featureId) 284 throws XMLConfigurationException { 285 if(featureId.equals( VALIDATION)) { 286 return fValidation; 287 } else if(featureId.equals( WARN_ON_DUPLICATE_ATTDEF)) { 288 return fWarnDuplicateAttdef; 289 } else if(featureId.equals( NOTIFY_CHAR_REFS)) { 290 return fDTDScanner.getFeature(featureId); 291 } 292 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, featureId); 293 } 295 303 public void setLocale(Locale locale) { 304 fLocale = locale; 305 } 307 308 public Locale getLocale() { 309 return fLocale; 310 } 312 313 318 public void setErrorHandler(XMLErrorHandler errorHandler) { 319 fErrorReporter.setProperty(ERROR_HANDLER, errorHandler); 320 } 322 323 public XMLErrorHandler getErrorHandler() { 324 return fErrorReporter.getErrorHandler(); 325 } 327 332 public void setEntityResolver(XMLEntityResolver entityResolver) { 333 fEntityResolver = entityResolver; 334 } 336 337 public XMLEntityResolver getEntityResolver() { 338 return fEntityResolver; 339 } 341 351 public Grammar loadGrammar(XMLInputSource source) 352 throws IOException , XNIException { 353 reset(); 354 String eid = XMLEntityManager.expandSystemId(source.getSystemId(), source.getBaseSystemId(), fStrictURI); 356 fDTDGrammar = new DTDGrammar(fSymbolTable, new XMLDTDDescription(source.getPublicId(), source.getSystemId(), source.getBaseSystemId(), eid, null)); 357 fGrammarBucket = new DTDGrammarBucket(); 358 fGrammarBucket.setStandalone(false); 359 fGrammarBucket.setActiveGrammar(fDTDGrammar); 360 363 try { 365 fDTDScanner.setInputSource(source); 366 fDTDScanner.scanDTDExternalSubset(true); 367 } catch (EOFException e) { 368 } 370 finally { 371 fEntityManager.closeReaders(); 373 } 374 if(fDTDGrammar != null && fGrammarPool != null) { 375 fGrammarPool.cacheGrammars(XMLDTDDescription.XML_DTD, new Grammar[] {fDTDGrammar}); 376 } 377 return fDTDGrammar; 378 } 380 protected void reset() { 382 super.reset(); 383 fDTDScanner.reset(); 384 fEntityManager.reset(); 385 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 386 } 387 388 } | Popular Tags |