1 16 17 package org.apache.xerces.impl.xs; 18 19 import org.apache.xerces.impl.xs.XMLSchemaLoader; 20 import org.apache.xerces.impl.xs.util.XSGrammarPool; 21 import org.apache.xerces.xni.grammars.Grammar; 22 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 23 import org.apache.xerces.xni.grammars.XSGrammar; 24 import org.apache.xerces.xni.parser.XMLInputSource; 25 import org.apache.xerces.xs.LSInputList; 26 import org.apache.xerces.xs.StringList; 27 import org.apache.xerces.xs.XSConstants; 28 import org.apache.xerces.xs.XSLoader; 29 import org.apache.xerces.xs.XSModel; 30 import org.apache.xerces.xs.XSNamedMap; 31 import org.apache.xerces.xs.XSObjectList; 32 import org.apache.xerces.xs.XSTypeDefinition; 33 import org.w3c.dom.DOMConfiguration ; 34 import org.w3c.dom.DOMException ; 35 import org.w3c.dom.DOMStringList ; 36 import org.w3c.dom.ls.LSInput ; 37 38 47 public final class XSLoaderImpl implements XSLoader, DOMConfiguration { 48 49 53 private final XSGrammarPool fGrammarPool = new XSGrammarMerger(); 54 55 56 private final XMLSchemaLoader fSchemaLoader = new XMLSchemaLoader(); 57 58 61 public XSLoaderImpl() { 62 fSchemaLoader.setProperty(XMLSchemaLoader.XMLGRAMMAR_POOL, fGrammarPool); 63 } 64 65 89 public DOMConfiguration getConfig() { 90 return this; 91 } 92 93 100 public XSModel loadURIList(StringList uriList) { 101 int length = uriList.getLength(); 102 if (length == 0) { 103 return null; 104 } 105 try { 106 fGrammarPool.clear(); 107 for (int i = 0; i < length; ++i) { 108 fSchemaLoader.loadGrammar(new XMLInputSource(null, uriList.item(i), null)); 109 } 110 return fGrammarPool.toXSModel(); 111 } 112 catch (Exception e) { 113 fSchemaLoader.reportDOMFatalError(e); 114 return null; 115 } 116 } 117 118 125 public XSModel loadInputList(LSInputList is) { 126 final int length = is.getLength(); 127 if (length == 0) { 128 return null; 129 } 130 try { 131 fGrammarPool.clear(); 132 for (int i = 0; i < length; ++i) { 133 fSchemaLoader.loadGrammar(fSchemaLoader.dom2xmlInputSource(is.item(i))); 134 } 135 return fGrammarPool.toXSModel(); 136 } 137 catch (Exception e) { 138 fSchemaLoader.reportDOMFatalError(e); 139 return null; 140 } 141 } 142 143 150 public XSModel loadURI(String uri) { 151 try { 152 fGrammarPool.clear(); 153 return ((XSGrammar) fSchemaLoader.loadGrammar(new XMLInputSource(null, uri, null))).toXSModel(); 154 } 155 catch (Exception e){ 156 fSchemaLoader.reportDOMFatalError(e); 157 return null; 158 } 159 } 160 161 168 public XSModel load(LSInput is) { 169 try { 170 fGrammarPool.clear(); 171 return ((XSGrammar) fSchemaLoader.loadGrammar(fSchemaLoader.dom2xmlInputSource(is))).toXSModel(); 172 } 173 catch (Exception e) { 174 fSchemaLoader.reportDOMFatalError(e); 175 return null; 176 } 177 } 178 179 182 public void setParameter(String name, Object value) throws DOMException { 183 fSchemaLoader.setParameter(name, value); 184 } 185 186 189 public Object getParameter(String name) throws DOMException { 190 return fSchemaLoader.getParameter(name); 191 } 192 193 196 public boolean canSetParameter(String name, Object value) { 197 return fSchemaLoader.canSetParameter(name, value); 198 } 199 200 203 public DOMStringList getParameterNames() { 204 return fSchemaLoader.getParameterNames(); 205 } 206 207 212 private static final class XSGrammarMerger extends XSGrammarPool { 213 214 public XSGrammarMerger () {} 215 216 public void putGrammar(Grammar grammar) { 217 SchemaGrammar cachedGrammar = 218 toSchemaGrammar(super.getGrammar(grammar.getGrammarDescription())); 219 if (cachedGrammar != null) { 220 SchemaGrammar newGrammar = toSchemaGrammar(grammar); 221 if (newGrammar != null) { 222 mergeSchemaGrammars(cachedGrammar, newGrammar); 223 } 224 } 225 else { 226 super.putGrammar(grammar); 227 } 228 } 229 230 private SchemaGrammar toSchemaGrammar (Grammar grammar) { 231 return (grammar instanceof SchemaGrammar) ? (SchemaGrammar) grammar : null; 232 } 233 234 private void mergeSchemaGrammars(SchemaGrammar cachedGrammar, SchemaGrammar newGrammar) { 235 236 237 XSNamedMap map = newGrammar.getComponents(XSConstants.ELEMENT_DECLARATION); 238 int length = map.getLength(); 239 for (int i = 0; i < length; ++i) { 240 XSElementDecl decl = (XSElementDecl) map.item(i); 241 if (cachedGrammar.getGlobalElementDecl(decl.getName()) == null) { 242 cachedGrammar.addGlobalElementDecl(decl); 243 } 244 } 245 246 247 map = newGrammar.getComponents(XSConstants.ATTRIBUTE_DECLARATION); 248 length = map.getLength(); 249 for (int i = 0; i < length; ++i) { 250 XSAttributeDecl decl = (XSAttributeDecl) map.item(i); 251 if (cachedGrammar.getGlobalAttributeDecl(decl.getName()) == null) { 252 cachedGrammar.addGlobalAttributeDecl(decl); 253 } 254 } 255 256 257 map = newGrammar.getComponents(XSConstants.TYPE_DEFINITION); 258 length = map.getLength(); 259 for (int i = 0; i < length; ++i) { 260 XSTypeDefinition decl = (XSTypeDefinition) map.item(i); 261 if (cachedGrammar.getGlobalTypeDecl(decl.getName()) == null) { 262 cachedGrammar.addGlobalTypeDecl(decl); 263 } 264 } 265 266 267 map = newGrammar.getComponents(XSConstants.ATTRIBUTE_GROUP); 268 length = map.getLength(); 269 for (int i = 0; i < length; ++i) { 270 XSAttributeGroupDecl decl = (XSAttributeGroupDecl) map.item(i); 271 if (cachedGrammar.getGlobalAttributeGroupDecl(decl.getName()) == null) { 272 cachedGrammar.addGlobalAttributeGroupDecl(decl); 273 } 274 } 275 276 277 map = newGrammar.getComponents(XSConstants.MODEL_GROUP); 278 length = map.getLength(); 279 for (int i = 0; i < length; ++i) { 280 XSGroupDecl decl = (XSGroupDecl) map.item(i); 281 if (cachedGrammar.getGlobalGroupDecl(decl.getName()) == null) { 282 cachedGrammar.addGlobalGroupDecl(decl); 283 } 284 } 285 286 287 map = newGrammar.getComponents(XSConstants.NOTATION_DECLARATION); 288 length = map.getLength(); 289 for (int i = 0; i < length; ++i) { 290 XSNotationDecl decl = (XSNotationDecl) map.item(i); 291 if (cachedGrammar.getGlobalNotationDecl(decl.getName()) == null) { 292 cachedGrammar.addGlobalNotationDecl(decl); 293 } 294 } 295 296 301 XSObjectList annotations = newGrammar.getAnnotations(); 302 length = annotations.getLength(); 303 for (int i = 0; i < length; ++i) { 304 cachedGrammar.addAnnotation((XSAnnotationImpl) annotations.item(i)); 305 } 306 307 } 308 309 public boolean containsGrammar(XMLGrammarDescription desc) { 310 return false; 311 } 312 313 public Grammar getGrammar(XMLGrammarDescription desc) { 314 return null; 315 } 316 317 public Grammar retrieveGrammar(XMLGrammarDescription desc) { 318 return null; 319 } 320 321 public Grammar [] retrieveInitialGrammarSet (String grammarType) { 322 return new Grammar[0]; 323 } 324 } 325 } 326 | Popular Tags |