1 16 17 package org.apache.xerces.impl.xs; 18 19 import java.util.Vector ; 20 21 import org.apache.xerces.xs.StringList; 22 import org.apache.xerces.xs.XSAttributeDeclaration; 23 import org.apache.xerces.xs.XSAttributeGroupDefinition; 24 import org.apache.xerces.xs.XSConstants; 25 import org.apache.xerces.xs.XSElementDeclaration; 26 import org.apache.xerces.xs.XSModel; 27 import org.apache.xerces.xs.XSModelGroupDefinition; 28 import org.apache.xerces.xs.XSNamedMap; 29 import org.apache.xerces.xs.XSNamespaceItemList; 30 import org.apache.xerces.xs.XSNotationDeclaration; 31 import org.apache.xerces.xs.XSObjectList; 32 import org.apache.xerces.xs.XSTypeDefinition; 33 import org.apache.xerces.impl.xs.util.NSItemListImpl; 34 import org.apache.xerces.impl.xs.util.StringListImpl; 35 import org.apache.xerces.impl.xs.util.XSNamedMap4Types; 36 import org.apache.xerces.impl.xs.util.XSNamedMapImpl; 37 import org.apache.xerces.impl.xs.util.XSObjectListImpl; 38 import org.apache.xerces.util.SymbolHash; 39 import org.apache.xerces.util.XMLSymbols; 40 41 51 public class XSModelImpl implements XSModel { 52 53 private static final short MAX_COMP_IDX = XSTypeDefinition.SIMPLE_TYPE; 55 private static final boolean[] GLOBAL_COMP = {false, true, true, true, false, true, true, false, false, false, false, true, false, false, false, true, true }; 73 74 private int fGrammarCount; 76 private String [] fNamespaces; 78 private SchemaGrammar[] fGrammarList; 80 private SymbolHash fGrammarMap; 82 private SymbolHash fSubGroupMap; 84 85 private XSNamedMap[] fGlobalComponents; 87 private XSNamedMap[][] fNSComponents; 89 90 private XSObjectListImpl fAnnotations = null; 92 93 private boolean fHasIDC = false; 95 96 102 public XSModelImpl(SchemaGrammar[] grammars) { 103 int len = grammars.length; 105 fNamespaces = new String [Math.max(len+1, 5)]; 106 fGrammarList = new SchemaGrammar[Math.max(len+1, 5)]; 107 boolean hasS4S = false; 108 for (int i = 0; i < len; i++) { 109 fNamespaces[i] = grammars[i].getTargetNamespace(); 110 fGrammarList[i] = grammars[i]; 111 if (fNamespaces[i] == SchemaSymbols.URI_SCHEMAFORSCHEMA) 112 hasS4S = true; 113 } 114 if (!hasS4S) { 116 fNamespaces[len] = SchemaSymbols.URI_SCHEMAFORSCHEMA; 117 fGrammarList[len++] = SchemaGrammar.SG_SchemaNS; 118 } 119 120 SchemaGrammar sg1, sg2; 121 Vector gs; 122 int i, j, k; 123 for (i = 0; i < len; i++) { 125 sg1 = fGrammarList[i]; 127 gs = sg1.getImportedGrammars(); 128 for (j = gs == null ? -1 : gs.size() - 1; j >= 0; j--) { 130 sg2 = (SchemaGrammar)gs.elementAt(j); 131 for (k = 0; k < len; k++) { 133 if (sg2 == fGrammarList[k]) 134 break; 135 } 136 if (k == len) { 138 if (len == fGrammarList.length) { 140 String [] newSA = new String [len*2]; 141 System.arraycopy(fNamespaces, 0, newSA, 0, len); 142 fNamespaces = newSA; 143 SchemaGrammar[] newGA = new SchemaGrammar[len*2]; 144 System.arraycopy(fGrammarList, 0, newGA, 0, len); 145 fGrammarList = newGA; 146 } 147 fNamespaces[len] = sg2.getTargetNamespace(); 148 fGrammarList[len] = sg2; 149 len++; 150 } 151 } 152 } 153 154 fGrammarMap = new SymbolHash(len*2); 156 for (i = 0; i < len; i++) { 157 fGrammarMap.put(null2EmptyString(fNamespaces[i]), fGrammarList[i]); 158 if (fGrammarList[i].hasIDConstraints()) 160 fHasIDC = true; 161 } 162 163 fGrammarCount = len; 164 fGlobalComponents = new XSNamedMap[MAX_COMP_IDX+1]; 165 fNSComponents = new XSNamedMap[len][MAX_COMP_IDX+1]; 166 167 buildSubGroups(); 169 } 170 171 private void buildSubGroups() { 172 SubstitutionGroupHandler sgHandler = new SubstitutionGroupHandler(null); 173 for (int i = 0 ; i < fGrammarCount; i++) { 174 sgHandler.addSubstitutionGroup(fGrammarList[i].getSubstitutionGroups()); 175 } 176 177 XSNamedMap elements = getComponents(XSConstants.ELEMENT_DECLARATION); 178 int len = elements.getLength(); 179 fSubGroupMap = new SymbolHash(len*2); 180 XSElementDecl head; 181 XSElementDeclaration[] subGroup; 182 for (int i = 0; i < len; i++) { 183 head = (XSElementDecl)elements.item(i); 184 subGroup = sgHandler.getSubstitutionGroup(head); 185 fSubGroupMap.put(head, subGroup.length > 0 ? 186 new XSObjectListImpl(subGroup, subGroup.length) : XSObjectListImpl.EMPTY_LIST); 187 } 188 } 189 190 196 public StringList getNamespaces() { 197 return new StringListImpl(fNamespaces, fGrammarCount); 199 } 200 201 202 public XSNamespaceItemList getNamespaceItems() { 203 204 return new NSItemListImpl(fGrammarList, fGrammarCount); 206 } 207 208 221 public synchronized XSNamedMap getComponents(short objectType) { 222 if (objectType <= 0 || objectType > MAX_COMP_IDX || 223 !GLOBAL_COMP[objectType]) { 224 return XSNamedMapImpl.EMPTY_MAP; 225 } 226 227 SymbolHash[] tables = new SymbolHash[fGrammarCount]; 228 if (fGlobalComponents[objectType] == null) { 230 for (int i = 0; i < fGrammarCount; i++) { 231 switch (objectType) { 232 case XSConstants.TYPE_DEFINITION: 233 case XSTypeDefinition.COMPLEX_TYPE: 234 case XSTypeDefinition.SIMPLE_TYPE: 235 tables[i] = fGrammarList[i].fGlobalTypeDecls; 236 break; 237 case XSConstants.ATTRIBUTE_DECLARATION: 238 tables[i] = fGrammarList[i].fGlobalAttrDecls; 239 break; 240 case XSConstants.ELEMENT_DECLARATION: 241 tables[i] = fGrammarList[i].fGlobalElemDecls; 242 break; 243 case XSConstants.ATTRIBUTE_GROUP: 244 tables[i] = fGrammarList[i].fGlobalAttrGrpDecls; 245 break; 246 case XSConstants.MODEL_GROUP_DEFINITION: 247 tables[i] = fGrammarList[i].fGlobalGroupDecls; 248 break; 249 case XSConstants.NOTATION_DECLARATION: 250 tables[i] = fGrammarList[i].fGlobalNotationDecls; 251 break; 252 } 253 } 254 if (objectType == XSTypeDefinition.COMPLEX_TYPE || 257 objectType == XSTypeDefinition.SIMPLE_TYPE) { 258 fGlobalComponents[objectType] = new XSNamedMap4Types(fNamespaces, tables, fGrammarCount, objectType); 259 } 260 else { 261 fGlobalComponents[objectType] = new XSNamedMapImpl(fNamespaces, tables, fGrammarCount); 262 } 263 } 264 265 return fGlobalComponents[objectType]; 266 } 267 268 280 public synchronized XSNamedMap getComponentsByNamespace(short objectType, 281 String namespace) { 282 if (objectType <= 0 || objectType > MAX_COMP_IDX || 283 !GLOBAL_COMP[objectType]) { 284 return XSNamedMapImpl.EMPTY_MAP; 285 } 286 287 int i = 0; 289 if (namespace != null) { 290 for (; i < fGrammarCount; ++i) { 291 if (namespace.equals(fNamespaces[i])) 292 break; 293 } 294 } 295 else { 296 for (; i < fGrammarCount; ++i) { 297 if (fNamespaces[i] == null) 298 break; 299 } 300 } 301 if (i == fGrammarCount) 302 return XSNamedMapImpl.EMPTY_MAP; 303 304 if (fNSComponents[i][objectType] == null) { 306 SymbolHash table = null; 307 switch (objectType) { 308 case XSConstants.TYPE_DEFINITION: 309 case XSTypeDefinition.COMPLEX_TYPE: 310 case XSTypeDefinition.SIMPLE_TYPE: 311 table = fGrammarList[i].fGlobalTypeDecls; 312 break; 313 case XSConstants.ATTRIBUTE_DECLARATION: 314 table = fGrammarList[i].fGlobalAttrDecls; 315 break; 316 case XSConstants.ELEMENT_DECLARATION: 317 table = fGrammarList[i].fGlobalElemDecls; 318 break; 319 case XSConstants.ATTRIBUTE_GROUP: 320 table = fGrammarList[i].fGlobalAttrGrpDecls; 321 break; 322 case XSConstants.MODEL_GROUP_DEFINITION: 323 table = fGrammarList[i].fGlobalGroupDecls; 324 break; 325 case XSConstants.NOTATION_DECLARATION: 326 table = fGrammarList[i].fGlobalNotationDecls; 327 break; 328 } 329 330 if (objectType == XSTypeDefinition.COMPLEX_TYPE || 333 objectType == XSTypeDefinition.SIMPLE_TYPE) { 334 fNSComponents[i][objectType] = new XSNamedMap4Types(namespace, table, objectType); 335 } 336 else { 337 fNSComponents[i][objectType] = new XSNamedMapImpl(namespace, table); 338 } 339 } 340 341 return fNSComponents[i][objectType]; 342 } 343 344 352 public XSTypeDefinition getTypeDefinition(String name, 353 String namespace) { 354 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 355 if (sg == null) 356 return null; 357 return (XSTypeDefinition)sg.fGlobalTypeDecls.get(name); 358 } 359 360 367 public XSAttributeDeclaration getAttributeDeclaration(String name, 368 String namespace) { 369 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 370 if (sg == null) 371 return null; 372 return (XSAttributeDeclaration)sg.fGlobalAttrDecls.get(name); 373 } 374 375 382 public XSElementDeclaration getElementDeclaration(String name, 383 String namespace) { 384 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 385 if (sg == null) 386 return null; 387 return (XSElementDeclaration)sg.fGlobalElemDecls.get(name); 388 } 389 390 397 public XSAttributeGroupDefinition getAttributeGroup(String name, 398 String namespace) { 399 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 400 if (sg == null) 401 return null; 402 return (XSAttributeGroupDefinition)sg.fGlobalAttrGrpDecls.get(name); 403 } 404 405 413 public XSModelGroupDefinition getModelGroupDefinition(String name, 414 String namespace) { 415 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 416 if (sg == null) 417 return null; 418 return (XSModelGroupDefinition)sg.fGlobalGroupDecls.get(name); 419 } 420 421 422 425 public XSNotationDeclaration getNotationDeclaration(String name, 426 String namespace) { 427 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 428 if (sg == null) 429 return null; 430 return (XSNotationDeclaration)sg.fGlobalNotationDecls.get(name); 431 } 432 433 436 public synchronized XSObjectList getAnnotations() { 437 if(fAnnotations != null) 438 return fAnnotations; 439 440 int totalAnnotations = 0; 442 for (int i = 0; i < fGrammarCount; i++) { 443 totalAnnotations += fGrammarList[i].fNumAnnotations; 444 } 445 XSAnnotationImpl [] annotations = new XSAnnotationImpl [totalAnnotations]; 446 int currPos = 0; 447 for (int i = 0; i < fGrammarCount; i++) { 448 SchemaGrammar currGrammar = fGrammarList[i]; 449 if (currGrammar.fNumAnnotations > 0) { 450 System.arraycopy(currGrammar.fAnnotations, 0, annotations, currPos, currGrammar.fNumAnnotations); 451 currPos += currGrammar.fNumAnnotations; 452 } 453 } 454 fAnnotations = new XSObjectListImpl(annotations, annotations.length); 455 return fAnnotations; 456 } 457 458 private static final String null2EmptyString(String str) { 459 return str == null ? XMLSymbols.EMPTY_STRING : str; 460 } 461 462 468 public boolean hasIDConstraints() { 469 return fHasIDC; 470 } 471 472 476 public XSObjectList getSubstitutionGroup(XSElementDeclaration head) { 477 return (XSObjectList)fSubGroupMap.get(head); 478 } 479 480 } | Popular Tags |