1 57 58 package com.sun.org.apache.xerces.internal.parsers; 59 60 import java.io.IOException ; 61 import java.util.Enumeration ; 62 import java.util.Hashtable ; 63 import java.util.Locale ; 64 65 import com.sun.org.apache.xerces.internal.impl.Constants; 66 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; 67 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 68 import com.sun.org.apache.xerces.internal.util.SymbolTable; 69 import com.sun.org.apache.xerces.internal.xni.XNIException; 70 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar; 71 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 72 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarLoader; 73 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 74 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 75 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 76 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 77 78 90 public class XMLGrammarPreparser { 91 92 96 private final static String CONTINUE_AFTER_FATAL_ERROR = 98 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 99 100 101 protected static final String SYMBOL_TABLE = 102 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 103 104 105 protected static final String ERROR_REPORTER = 106 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 107 108 109 protected static final String ERROR_HANDLER = 110 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 111 112 113 protected static final String ENTITY_RESOLVER = 114 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 115 116 117 protected static final String GRAMMAR_POOL = 118 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 119 120 private static final Hashtable KNOWN_LOADERS = new Hashtable (); 122 123 static { 124 KNOWN_LOADERS.put(XMLGrammarDescription.XML_SCHEMA, 125 "com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader"); 126 KNOWN_LOADERS.put(XMLGrammarDescription.XML_DTD, 127 "com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDLoader"); 128 } 129 130 131 private static final String [] RECOGNIZED_PROPERTIES = { 132 SYMBOL_TABLE, 133 ERROR_REPORTER, 134 ERROR_HANDLER, 135 ENTITY_RESOLVER, 136 GRAMMAR_POOL, 137 }; 138 139 protected SymbolTable fSymbolTable; 141 protected XMLErrorReporter fErrorReporter; 142 protected XMLEntityResolver fEntityResolver; 143 protected XMLGrammarPool fGrammarPool; 144 145 protected Locale fLocale; 146 147 private Hashtable fLoaders; 149 150 154 155 public XMLGrammarPreparser() { 156 this(new SymbolTable()); 157 } 159 164 public XMLGrammarPreparser (SymbolTable symbolTable) { 165 fSymbolTable = symbolTable; 166 167 fLoaders = new Hashtable (); 168 setLocale(Locale.getDefault()); 169 fErrorReporter = new XMLErrorReporter(); 170 fErrorReporter.setLocale(fLocale); 171 fEntityResolver = new XMLEntityManager(); 172 } 175 179 190 public boolean registerPreparser(String grammarType, XMLGrammarLoader loader) { 191 if(loader == null) { if(KNOWN_LOADERS.containsKey(grammarType)) { 193 String loaderName = (String )KNOWN_LOADERS.get(grammarType); 195 try { 196 ClassLoader cl = ObjectFactory.findClassLoader(); 197 XMLGrammarLoader gl = (XMLGrammarLoader)(ObjectFactory.newInstance(loaderName, cl, true)); 198 fLoaders.put(grammarType, gl); 199 } catch (Exception e) { 200 return false; 201 } 202 return true; 203 } 204 return false; 205 } 206 fLoaders.put(grammarType, loader); 208 return true; 209 } 211 227 public Grammar preparseGrammar(String type, XMLInputSource 228 is) throws XNIException, IOException { 229 if(fLoaders.containsKey(type)) { 230 XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type); 231 gl.setProperty(SYMBOL_TABLE, fSymbolTable); 233 gl.setProperty(ENTITY_RESOLVER, fEntityResolver); 234 gl.setProperty(ERROR_REPORTER, fErrorReporter); 235 if(fGrammarPool != null) { 237 try { 238 gl.setProperty(GRAMMAR_POOL, fGrammarPool); 239 } catch(Exception e) { 240 } 242 } 243 return gl.loadGrammar(is); 244 } 245 return null; 246 } 248 256 public void setLocale(Locale locale) { 257 fLocale = locale; 258 } 260 261 public Locale getLocale() { 262 return fLocale; 263 } 265 266 271 public void setErrorHandler(XMLErrorHandler errorHandler) { 272 fErrorReporter.setProperty(ERROR_HANDLER, errorHandler); 273 } 275 276 public XMLErrorHandler getErrorHandler() { 277 return fErrorReporter.getErrorHandler(); 278 } 280 285 public void setEntityResolver(XMLEntityResolver entityResolver) { 286 fEntityResolver = entityResolver; 287 } 289 290 public XMLEntityResolver getEntityResolver() { 291 return fEntityResolver; 292 } 294 299 public void setGrammarPool(XMLGrammarPool grammarPool) { 300 fGrammarPool = grammarPool; 301 } 303 304 public XMLGrammarPool getGrammarPool() { 305 return fGrammarPool; 306 } 308 public XMLGrammarLoader getLoader(String type) { 311 return (XMLGrammarLoader)fLoaders.get(type); 312 } 314 public void setFeature(String featureId, boolean value) { 320 Enumeration loaders = fLoaders.elements(); 321 while(loaders.hasMoreElements()){ 322 XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement(); 323 try { 324 gl.setFeature(featureId, value); 325 } catch(Exception e) { 326 } 328 } 329 if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) { 332 fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, value); 333 } 334 } 336 public void setProperty(String propId, Object value) { 344 Enumeration loaders = fLoaders.elements(); 345 while(loaders.hasMoreElements()){ 346 XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement(); 347 try { 348 gl.setProperty(propId, value); 349 } catch(Exception e) { 350 } 352 } 353 } 355 public boolean getFeature(String type, String featureId) { 362 XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type); 363 return gl.getFeature(featureId); 364 } 366 public Object getProperty(String type, String propertyId) { 375 XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type); 376 return gl.getProperty(propertyId); 377 } } | Popular Tags |