1 16 17 package org.apache.xerces.parsers; 18 19 import java.io.IOException ; 20 21 import org.apache.xerces.impl.Constants; 22 import org.apache.xerces.impl.dtd.DTDGrammar; 23 import org.apache.xerces.impl.dtd.XMLDTDLoader; 24 import org.apache.xerces.impl.xs.SchemaGrammar; 25 import org.apache.xerces.impl.xs.XMLSchemaLoader; 26 import org.apache.xerces.impl.xs.XSMessageFormatter; 27 import org.apache.xerces.util.SymbolTable; 28 import org.apache.xerces.util.SynchronizedSymbolTable; 29 import org.apache.xerces.util.XMLGrammarPoolImpl; 30 import org.apache.xerces.xni.XNIException; 31 import org.apache.xerces.xni.grammars.Grammar; 32 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 33 import org.apache.xerces.xni.grammars.XMLGrammarPool; 34 import org.apache.xerces.xni.parser.XMLComponentManager; 35 import org.apache.xerces.xni.parser.XMLConfigurationException; 36 import org.apache.xerces.xni.parser.XMLEntityResolver; 37 import org.apache.xerces.xni.parser.XMLInputSource; 38 39 63 public class XMLGrammarCachingConfiguration 64 extends XIncludeAwareParserConfiguration { 65 66 70 public static final int BIG_PRIME = 2039; 75 76 protected static final SynchronizedSymbolTable fStaticSymbolTable = 78 new SynchronizedSymbolTable(BIG_PRIME); 79 80 protected static final XMLGrammarPoolImpl fStaticGrammarPool = 82 new XMLGrammarPoolImpl(); 83 84 protected static final String SCHEMA_FULL_CHECKING = 86 Constants.XERCES_FEATURE_PREFIX+Constants.SCHEMA_FULL_CHECKING; 87 88 90 protected XMLSchemaLoader fSchemaLoader; 92 93 protected XMLDTDLoader fDTDLoader; 95 96 100 101 public XMLGrammarCachingConfiguration() { 102 this(fStaticSymbolTable, fStaticGrammarPool, null); 103 } 105 110 public XMLGrammarCachingConfiguration(SymbolTable symbolTable) { 111 this(symbolTable, fStaticGrammarPool, null); 112 } 114 125 public XMLGrammarCachingConfiguration(SymbolTable symbolTable, 126 XMLGrammarPool grammarPool) { 127 this(symbolTable, grammarPool, null); 128 } 130 142 public XMLGrammarCachingConfiguration(SymbolTable symbolTable, 143 XMLGrammarPool grammarPool, 144 XMLComponentManager parentSettings) { 145 super(symbolTable, grammarPool, parentSettings); 146 147 150 fSchemaLoader = new XMLSchemaLoader(fSymbolTable); 155 fSchemaLoader.setProperty(XMLGRAMMAR_POOL, fGrammarPool); 156 157 fDTDLoader = new XMLDTDLoader(fSymbolTable, fGrammarPool); 159 } 161 165 169 public void lockGrammarPool() { 170 fGrammarPool.lockPool(); 171 } 173 177 public void clearGrammarPool() { 178 fGrammarPool.clear(); 179 } 181 185 public void unlockGrammarPool() { 186 fGrammarPool.unlockPool(); 187 } 189 203 public Grammar parseGrammar(String type, String uri) 204 throws XNIException, IOException { 205 XMLInputSource source = new XMLInputSource(null, uri, null); 206 return parseGrammar(type, source); 207 208 } 209 210 226 public Grammar parseGrammar(String type, XMLInputSource 227 is) throws XNIException, IOException { 228 if(type.equals(XMLGrammarDescription.XML_SCHEMA)) { 229 return parseXMLSchema(is); 231 } else if(type.equals(XMLGrammarDescription.XML_DTD)) { 232 return parseDTD(is); 233 } 234 return null; 236 } 238 242 244 256 protected void checkFeature(String featureId) 257 throws XMLConfigurationException { 258 259 super.checkFeature(featureId); 260 261 } 263 276 protected void checkProperty(String propertyId) 277 throws XMLConfigurationException { 278 super.checkProperty(propertyId); 279 280 } 282 284 289 SchemaGrammar parseXMLSchema(XMLInputSource is) 290 throws IOException { 291 XMLEntityResolver resolver = getEntityResolver(); 292 if(resolver != null) { 293 fSchemaLoader.setEntityResolver(resolver); 294 } 295 if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 296 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter()); 297 } 298 fSchemaLoader.setProperty(ERROR_REPORTER, fErrorReporter); 299 300 String propPrefix = Constants.XERCES_PROPERTY_PREFIX; 301 String propName = propPrefix + Constants.SCHEMA_LOCATION; 302 fSchemaLoader.setProperty(propName, getProperty(propName)); 303 propName = propPrefix + Constants.SCHEMA_NONS_LOCATION; 304 fSchemaLoader.setProperty(propName, getProperty(propName)); 305 propName = Constants.JAXP_PROPERTY_PREFIX+Constants.SCHEMA_SOURCE; 306 fSchemaLoader.setProperty(propName, getProperty(propName)); 307 fSchemaLoader.setFeature(SCHEMA_FULL_CHECKING, getFeature(SCHEMA_FULL_CHECKING)); 308 309 SchemaGrammar grammar = (SchemaGrammar)fSchemaLoader.loadGrammar(is); 313 if (grammar != null) { 315 fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, 316 new Grammar[]{grammar}); 317 } 318 319 return grammar; 320 321 } 323 325 DTDGrammar parseDTD(XMLInputSource is) 326 throws IOException { 327 XMLEntityResolver resolver = getEntityResolver(); 328 if(resolver != null) { 329 fDTDLoader.setEntityResolver(resolver); 330 } 331 fDTDLoader.setProperty(ERROR_REPORTER, fErrorReporter); 332 333 DTDGrammar grammar = (DTDGrammar)fDTDLoader.loadGrammar(is); 337 if (grammar != null) { 339 fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_DTD, 340 new Grammar[]{grammar}); 341 } 342 343 return grammar; 344 345 } 347 348 } | Popular Tags |