1 57 58 package com.sun.org.apache.xerces.internal.impl.dtd; 59 60 import com.sun.org.apache.xerces.internal.impl.Constants; 61 import com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl; 62 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 63 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; 64 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 65 66 import com.sun.org.apache.xerces.internal.util.SymbolTable; 67 import com.sun.org.apache.xerces.internal.util.DefaultErrorHandler; 68 69 import com.sun.org.apache.xerces.internal.xni.XNIException; 70 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 71 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarLoader; 72 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar; 73 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 74 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 75 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 76 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 77 78 import java.util.Locale ; 79 import java.io.IOException ; 80 import java.io.EOFException ; 81 82 103 public class XMLDTDLoader 104 extends XMLDTDProcessor 105 implements XMLGrammarLoader { 106 107 111 113 114 protected static final String STANDARD_URI_CONFORMANT_FEATURE = 115 Constants.XERCES_FEATURE_PREFIX + Constants.STANDARD_URI_CONFORMANT_FEATURE; 116 117 private static final String [] RECOGNIZED_FEATURES = { 119 VALIDATION, 120 WARN_ON_DUPLICATE_ATTDEF, 121 NOTIFY_CHAR_REFS, 122 STANDARD_URI_CONFORMANT_FEATURE 123 }; 124 125 127 128 protected static final String ERROR_HANDLER = 129 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 130 131 132 public static final String ENTITY_RESOLVER = 133 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 134 135 136 private static final String [] LOADER_RECOGNIZED_PROPERTIES = { 137 SYMBOL_TABLE, 138 ERROR_REPORTER, 139 ERROR_HANDLER, 140 ENTITY_RESOLVER, 141 GRAMMAR_POOL, 142 DTD_VALIDATOR, 143 }; 144 145 private boolean fStrictURI = false; 147 148 149 protected XMLEntityResolver fEntityResolver; 150 151 protected XMLDTDScannerImpl fDTDScanner; 153 154 protected XMLEntityManager fEntityManager; 156 157 protected Locale fLocale; 159 160 164 165 public XMLDTDLoader() { 166 this(new SymbolTable()); 167 } 169 public XMLDTDLoader(SymbolTable symbolTable) { 170 this(symbolTable, null); 171 } 173 public XMLDTDLoader(SymbolTable symbolTable, 174 XMLGrammarPool grammarPool) { 175 this(symbolTable, grammarPool, null, new XMLEntityManager()); 176 } 178 XMLDTDLoader(SymbolTable symbolTable, 179 XMLGrammarPool grammarPool, XMLErrorReporter errorReporter, 180 XMLEntityResolver entityResolver) { 181 fSymbolTable = symbolTable; 182 fGrammarPool = grammarPool; 183 if(errorReporter == null) { 184 errorReporter = new XMLErrorReporter(); 185 errorReporter.setProperty(ERROR_HANDLER, new DefaultErrorHandler()); 186 } 187 fErrorReporter = errorReporter; 188 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 190 XMLMessageFormatter xmft = new XMLMessageFormatter(); 191 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 192 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 193 } 194 fEntityResolver = entityResolver; 195 if(fEntityResolver instanceof XMLEntityManager) { 196 fEntityManager = (XMLEntityManager)fEntityResolver; 197 } else { 198 fEntityManager = new XMLEntityManager(); 199 } 200 fEntityManager.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY, errorReporter); 201 fDTDScanner = new XMLDTDScannerImpl(fSymbolTable, fErrorReporter, fEntityManager); 202 fDTDScanner.setDTDHandler(this); 203 fDTDScanner.setDTDContentModelHandler(this); 204 reset(); 205 } 207 209 224 public void setFeature(String featureId, boolean state) 225 throws XMLConfigurationException { 226 if(featureId.equals(VALIDATION)) { 227 fValidation = state; 228 } else if(featureId.equals(WARN_ON_DUPLICATE_ATTDEF)) { 229 fWarnDuplicateAttdef = state; 230 } else if(featureId.equals(NOTIFY_CHAR_REFS)) { 231 fDTDScanner.setFeature(featureId, state); 232 } else if(featureId.equals(STANDARD_URI_CONFORMANT_FEATURE)) { 233 fStrictURI = state; 234 } else { 235 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, featureId); 236 } 237 } 239 244 public String [] getRecognizedProperties() { 245 return (String [])(LOADER_RECOGNIZED_PROPERTIES.clone()); 246 } 248 255 public Object getProperty(String propertyId) 256 throws XMLConfigurationException { 257 if(propertyId.equals( SYMBOL_TABLE)) { 258 return fSymbolTable; 259 } else if(propertyId.equals( ERROR_REPORTER)) { 260 return fErrorReporter; 261 } else if(propertyId.equals( ERROR_HANDLER)) { 262 return fErrorReporter.getErrorHandler(); 263 } else if(propertyId.equals( ENTITY_RESOLVER)) { 264 return fEntityResolver; 265 } else if(propertyId.equals( GRAMMAR_POOL)) { 266 return fGrammarPool; 267 } else if(propertyId.equals( DTD_VALIDATOR)) { 268 return fValidator; 269 } 270 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, propertyId); 271 } 273 288 public void setProperty(String propertyId, Object value) 289 throws XMLConfigurationException { 290 if(propertyId.equals( SYMBOL_TABLE)) { 291 fSymbolTable = (SymbolTable)value; 292 fDTDScanner.setProperty(propertyId, value); 293 fEntityManager.setProperty(propertyId, value); 294 } else if(propertyId.equals( ERROR_REPORTER)) { 295 fErrorReporter = (XMLErrorReporter)value; 296 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 298 XMLMessageFormatter xmft = new XMLMessageFormatter(); 299 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 300 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 301 } 302 fDTDScanner.setProperty(propertyId, value); 303 fEntityManager.setProperty(propertyId, value); 304 } else if(propertyId.equals( ERROR_HANDLER)) { 305 fErrorReporter.setProperty(propertyId, value); 306 } else if(propertyId.equals( ENTITY_RESOLVER)) { 307 fEntityResolver = (XMLEntityResolver)value; 308 } else if(propertyId.equals( GRAMMAR_POOL)) { 309 fGrammarPool = (XMLGrammarPool)value; 310 } else { 311 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, propertyId); 312 } 313 } 315 322 public boolean getFeature(String featureId) 323 throws XMLConfigurationException { 324 if(featureId.equals( VALIDATION)) { 325 return fValidation; 326 } else if(featureId.equals( WARN_ON_DUPLICATE_ATTDEF)) { 327 return fWarnDuplicateAttdef; 328 } else if(featureId.equals( NOTIFY_CHAR_REFS)) { 329 return fDTDScanner.getFeature(featureId); 330 } 331 throw new XMLConfigurationException(XMLConfigurationException.NOT_RECOGNIZED, featureId); 332 } 334 342 public void setLocale(Locale locale) { 343 fLocale = locale; 344 } 346 347 public Locale getLocale() { 348 return fLocale; 349 } 351 352 357 public void setErrorHandler(XMLErrorHandler errorHandler) { 358 fErrorReporter.setProperty(ERROR_HANDLER, errorHandler); 359 } 361 362 public XMLErrorHandler getErrorHandler() { 363 return fErrorReporter.getErrorHandler(); 364 } 366 371 public void setEntityResolver(XMLEntityResolver entityResolver) { 372 fEntityResolver = entityResolver; 373 } 375 376 public XMLEntityResolver getEntityResolver() { 377 return fEntityResolver; 378 } 380 390 public Grammar loadGrammar(XMLInputSource source) 391 throws IOException , XNIException { 392 reset(); 393 String eid = XMLEntityManager.expandSystemId(source.getSystemId(), source.getBaseSystemId(), fStrictURI); 395 fDTDGrammar = new DTDGrammar(fSymbolTable, new XMLDTDDescription(source.getPublicId(), source.getSystemId(), source.getBaseSystemId(), eid, null)); 396 fGrammarBucket = new DTDGrammarBucket(); 397 fGrammarBucket.setStandalone(false); 398 fGrammarBucket.setActiveGrammar(fDTDGrammar); 399 402 try { 404 fDTDScanner.setInputSource(source); 405 fDTDScanner.scanDTDExternalSubset(true); 406 } catch (EOFException e) { 407 } 409 finally { 410 fEntityManager.closeReaders(); 412 } 413 if(fDTDGrammar != null && fGrammarPool != null) { 414 fGrammarPool.cacheGrammars(XMLDTDDescription.XML_DTD, new Grammar[] {fDTDGrammar}); 415 } 416 return fDTDGrammar; 417 } 419 protected void reset() { 421 super.reset(); 422 fDTDScanner.reset(); 423 fEntityManager.reset(); 424 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 425 } 426 427 } | Popular Tags |