1 16 17 package org.apache.xerces.parsers; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 import java.util.Locale ; 23 24 import org.apache.xerces.impl.Constants; 25 import org.apache.xerces.impl.XMLEntityManager; 26 import org.apache.xerces.impl.XMLErrorReporter; 27 import org.apache.xerces.util.SymbolTable; 28 import org.apache.xerces.xni.XNIException; 29 import org.apache.xerces.xni.grammars.Grammar; 30 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 31 import org.apache.xerces.xni.grammars.XMLGrammarLoader; 32 import org.apache.xerces.xni.grammars.XMLGrammarPool; 33 import org.apache.xerces.xni.parser.XMLEntityResolver; 34 import org.apache.xerces.xni.parser.XMLErrorHandler; 35 import org.apache.xerces.xni.parser.XMLInputSource; 36 37 49 public class XMLGrammarPreparser { 50 51 55 private final static String CONTINUE_AFTER_FATAL_ERROR = 57 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 58 59 60 protected static final String SYMBOL_TABLE = 61 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 62 63 64 protected static final String ERROR_REPORTER = 65 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 66 67 68 protected static final String ERROR_HANDLER = 69 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 70 71 72 protected static final String ENTITY_RESOLVER = 73 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 74 75 76 protected static final String GRAMMAR_POOL = 77 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 78 79 private static final Hashtable KNOWN_LOADERS = new Hashtable (); 81 82 static { 83 KNOWN_LOADERS.put(XMLGrammarDescription.XML_SCHEMA, 84 "org.apache.xerces.impl.xs.XMLSchemaLoader"); 85 KNOWN_LOADERS.put(XMLGrammarDescription.XML_DTD, 86 "org.apache.xerces.impl.dtd.XMLDTDLoader"); 87 } 88 89 90 private static final String [] RECOGNIZED_PROPERTIES = { 91 SYMBOL_TABLE, 92 ERROR_REPORTER, 93 ERROR_HANDLER, 94 ENTITY_RESOLVER, 95 GRAMMAR_POOL, 96 }; 97 98 protected SymbolTable fSymbolTable; 100 protected XMLErrorReporter fErrorReporter; 101 protected XMLEntityResolver fEntityResolver; 102 protected XMLGrammarPool fGrammarPool; 103 104 protected Locale fLocale; 105 106 private Hashtable fLoaders; 108 109 113 114 public XMLGrammarPreparser() { 115 this(new SymbolTable()); 116 } 118 123 public XMLGrammarPreparser (SymbolTable symbolTable) { 124 fSymbolTable = symbolTable; 125 126 fLoaders = new Hashtable (); 127 setLocale(Locale.getDefault()); 128 fErrorReporter = new XMLErrorReporter(); 129 fErrorReporter.setLocale(fLocale); 130 fEntityResolver = new XMLEntityManager(); 131 } 134 138 149 public boolean registerPreparser(String grammarType, XMLGrammarLoader loader) { 150 if(loader == null) { if(KNOWN_LOADERS.containsKey(grammarType)) { 152 String loaderName = (String )KNOWN_LOADERS.get(grammarType); 154 try { 155 ClassLoader cl = ObjectFactory.findClassLoader(); 156 XMLGrammarLoader gl = (XMLGrammarLoader)(ObjectFactory.newInstance(loaderName, cl, true)); 157 fLoaders.put(grammarType, gl); 158 } catch (Exception e) { 159 return false; 160 } 161 return true; 162 } 163 return false; 164 } 165 fLoaders.put(grammarType, loader); 167 return true; 168 } 170 186 public Grammar preparseGrammar(String type, XMLInputSource 187 is) throws XNIException, IOException { 188 if(fLoaders.containsKey(type)) { 189 XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type); 190 gl.setProperty(SYMBOL_TABLE, fSymbolTable); 192 gl.setProperty(ENTITY_RESOLVER, fEntityResolver); 193 gl.setProperty(ERROR_REPORTER, fErrorReporter); 194 if(fGrammarPool != null) { 196 try { 197 gl.setProperty(GRAMMAR_POOL, fGrammarPool); 198 } catch(Exception e) { 199 } 201 } 202 return gl.loadGrammar(is); 203 } 204 return null; 205 } 207 215 public void setLocale(Locale locale) { 216 fLocale = locale; 217 } 219 220 public Locale getLocale() { 221 return fLocale; 222 } 224 225 230 public void setErrorHandler(XMLErrorHandler errorHandler) { 231 fErrorReporter.setProperty(ERROR_HANDLER, errorHandler); 232 } 234 235 public XMLErrorHandler getErrorHandler() { 236 return fErrorReporter.getErrorHandler(); 237 } 239 244 public void setEntityResolver(XMLEntityResolver entityResolver) { 245 fEntityResolver = entityResolver; 246 } 248 249 public XMLEntityResolver getEntityResolver() { 250 return fEntityResolver; 251 } 253 258 public void setGrammarPool(XMLGrammarPool grammarPool) { 259 fGrammarPool = grammarPool; 260 } 262 263 public XMLGrammarPool getGrammarPool() { 264 return fGrammarPool; 265 } 267 public XMLGrammarLoader getLoader(String type) { 270 return (XMLGrammarLoader)fLoaders.get(type); 271 } 273 public void setFeature(String featureId, boolean value) { 279 Enumeration loaders = fLoaders.elements(); 280 while(loaders.hasMoreElements()){ 281 XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement(); 282 try { 283 gl.setFeature(featureId, value); 284 } catch(Exception e) { 285 } 287 } 288 if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) { 291 fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, value); 292 } 293 } 295 public void setProperty(String propId, Object value) { 303 Enumeration loaders = fLoaders.elements(); 304 while(loaders.hasMoreElements()){ 305 XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement(); 306 try { 307 gl.setProperty(propId, value); 308 } catch(Exception e) { 309 } 311 } 312 } 314 public boolean getFeature(String type, String featureId) { 321 XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type); 322 return gl.getFeature(featureId); 323 } 325 public Object getProperty(String type, String propertyId) { 334 XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type); 335 return gl.getProperty(propertyId); 336 } } | Popular Tags |