1 16 17 package org.apache.xerces.parsers; 18 19 import java.util.Vector ; 20 21 import org.apache.xerces.dom.ASModelImpl; 22 import org.apache.xerces.dom3.as.ASModel; 23 import org.apache.xerces.dom3.as.DOMASBuilder; 24 import org.apache.xerces.dom3.as.DOMASException; 25 import org.apache.xerces.impl.Constants; 26 import org.apache.xerces.impl.xs.SchemaGrammar; 27 import org.apache.xerces.impl.xs.XSGrammarBucket; 28 import org.apache.xerces.util.SymbolTable; 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.XMLGrammarPool; 33 import org.apache.xerces.xni.parser.XMLInputSource; 34 import org.w3c.dom.ls.LSInput ; 35 36 46 47 public class DOMASBuilderImpl 48 extends DOMParserImpl implements DOMASBuilder { 49 50 54 56 protected static final String SCHEMA_FULL_CHECKING = 57 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 58 59 61 protected static final String ERROR_REPORTER = 62 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 63 64 protected static final String SYMBOL_TABLE = 65 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 66 67 protected static final String ENTITY_MANAGER = 68 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 69 70 71 75 protected XSGrammarBucket fGrammarBucket; 76 77 protected ASModelImpl fAbstractSchema; 78 79 83 86 public DOMASBuilderImpl() { 87 super(new XMLGrammarCachingConfiguration()); 88 } 90 95 public DOMASBuilderImpl(XMLGrammarCachingConfiguration config) { 96 super(config); 97 } 99 102 public DOMASBuilderImpl(SymbolTable symbolTable) { 103 super(new XMLGrammarCachingConfiguration(symbolTable)); 104 } 106 107 114 public DOMASBuilderImpl(SymbolTable symbolTable, XMLGrammarPool grammarPool) { 115 super(new XMLGrammarCachingConfiguration(symbolTable, grammarPool)); 116 } 117 118 122 129 public ASModel getAbstractSchema() { 130 return fAbstractSchema; 131 } 132 133 140 public void setAbstractSchema(ASModel abstractSchema) { 141 142 fAbstractSchema = (ASModelImpl)abstractSchema; 148 149 XMLGrammarPool grammarPool = (XMLGrammarPool)fConfiguration.getProperty(StandardParserConfiguration.XMLGRAMMAR_POOL); 151 if (grammarPool == null) { 154 grammarPool = new XMLGrammarPoolImpl(); 156 fConfiguration.setProperty(StandardParserConfiguration.XMLGRAMMAR_POOL, 157 grammarPool); 158 } 159 if (fAbstractSchema != null) { 160 initGrammarPool(fAbstractSchema, grammarPool); 161 } 162 } 163 164 187 public ASModel parseASURI(String uri) 188 throws DOMASException, Exception { 189 XMLInputSource source = new XMLInputSource(null, uri, null); 190 return parseASInputSource(source); 191 } 192 193 218 public ASModel parseASInputSource(LSInput is) 219 throws DOMASException, Exception { 220 221 XMLInputSource xis = this.dom2xmlInputSource(is); 223 try { 224 return parseASInputSource(xis); 225 } 226 catch (XNIException e) { 227 Exception ex = e.getException(); 228 throw ex; 229 } 230 } 231 232 ASModel parseASInputSource(XMLInputSource is) throws Exception { 233 234 if (fGrammarBucket == null) { 235 fGrammarBucket = new XSGrammarBucket(); 236 } 237 238 initGrammarBucket(); 239 240 XMLGrammarCachingConfiguration gramConfig = (XMLGrammarCachingConfiguration)fConfiguration; 243 gramConfig.lockGrammarPool(); 245 SchemaGrammar grammar = gramConfig.parseXMLSchema(is); 246 gramConfig.unlockGrammarPool(); 247 248 ASModelImpl newAsModel = null; 249 if (grammar != null) { 250 newAsModel = new ASModelImpl(); 251 fGrammarBucket.putGrammar (grammar, true); 252 addGrammars(newAsModel, fGrammarBucket); 253 } 254 return newAsModel; 255 } 256 257 private void initGrammarBucket() { 259 fGrammarBucket.reset(); 260 if (fAbstractSchema != null) 261 initGrammarBucketRecurse(fAbstractSchema); 262 } 263 private void initGrammarBucketRecurse(ASModelImpl currModel) { 264 if(currModel.getGrammar() != null) { 265 fGrammarBucket.putGrammar(currModel.getGrammar()); 266 } 267 for(int i = 0; i < currModel.getInternalASModels().size(); i++) { 268 ASModelImpl nextModel = (ASModelImpl)(currModel.getInternalASModels().elementAt(i)); 269 initGrammarBucketRecurse(nextModel); 270 } 271 } 272 273 private void addGrammars(ASModelImpl model, XSGrammarBucket grammarBucket) { 274 SchemaGrammar [] grammarList = grammarBucket.getGrammars(); 275 for(int i=0; i<grammarList.length; i++) { 276 ASModelImpl newModel = new ASModelImpl(); 277 newModel.setGrammar(grammarList[i]); 278 model.addASModel(newModel); 279 } 280 } 282 private void initGrammarPool(ASModelImpl currModel, XMLGrammarPool grammarPool) { 283 Grammar[] grammars = new Grammar[1]; 286 if ((grammars[0] = (Grammar)currModel.getGrammar()) != null) { 287 grammarPool.cacheGrammars(grammars[0].getGrammarDescription().getGrammarType(), grammars); 288 } 289 Vector modelStore = currModel.getInternalASModels(); 290 for (int i = 0; i < modelStore.size(); i++) { 291 initGrammarPool((ASModelImpl)modelStore.elementAt(i), grammarPool); 292 } 293 } 294 } | Popular Tags |