1 57 58 package com.sun.org.apache.xerces.internal.impl.xs; 59 60 import java.util.Vector ; 61 62 import com.sun.org.apache.xerces.internal.xs.StringList; 63 import com.sun.org.apache.xerces.internal.xs.XSAttributeDeclaration; 64 import com.sun.org.apache.xerces.internal.xs.XSAttributeGroupDefinition; 65 import com.sun.org.apache.xerces.internal.xs.XSConstants; 66 import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration; 67 import com.sun.org.apache.xerces.internal.xs.XSModel; 68 import com.sun.org.apache.xerces.internal.xs.XSModelGroupDefinition; 69 import com.sun.org.apache.xerces.internal.xs.XSNamedMap; 70 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItemList; 71 import com.sun.org.apache.xerces.internal.xs.XSNotationDeclaration; 72 import com.sun.org.apache.xerces.internal.xs.XSObjectList; 73 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 74 import com.sun.org.apache.xerces.internal.impl.xs.util.NSItemListImpl; 75 import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl; 76 import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMap4Types; 77 import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMapImpl; 78 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl; 79 import com.sun.org.apache.xerces.internal.util.SymbolHash; 80 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 81 82 90 98 106 public class XSModelImpl implements XSModel { 107 108 private static final short MAX_COMP_IDX = XSTypeDefinition.SIMPLE_TYPE; 110 private static final boolean[] GLOBAL_COMP = {false, true, true, true, false, true, true, false, false, false, false, true, false, false, false, true, true }; 128 129 private int fGrammarCount; 131 private String [] fNamespaces; 133 private SchemaGrammar[] fGrammarList; 135 private SymbolHash fGrammarMap; 137 private SymbolHash fSubGroupMap; 139 140 private XSNamedMap[] fGlobalComponents; 142 private XSNamedMap[][] fNSComponents; 144 145 private XSObjectListImpl fAnnotations = null; 147 148 private boolean fHasIDC = false; 150 151 157 public XSModelImpl(SchemaGrammar[] grammars) { 158 int len = grammars.length; 160 fNamespaces = new String [Math.max(len+1, 5)]; 161 fGrammarList = new SchemaGrammar[Math.max(len+1, 5)]; 162 boolean hasS4S = false; 163 for (int i = 0; i < len; i++) { 164 fNamespaces[i] = grammars[i].getTargetNamespace(); 165 fGrammarList[i] = grammars[i]; 166 if (fNamespaces[i] == SchemaSymbols.URI_SCHEMAFORSCHEMA) 167 hasS4S = true; 168 } 169 if (!hasS4S) { 171 fNamespaces[len] = SchemaSymbols.URI_SCHEMAFORSCHEMA; 172 fGrammarList[len++] = SchemaGrammar.SG_SchemaNS; 173 } 174 175 SchemaGrammar sg1, sg2; 176 Vector gs; 177 int i, j, k; 178 for (i = 0; i < len; i++) { 180 sg1 = fGrammarList[i]; 182 gs = sg1.getImportedGrammars(); 183 for (j = gs == null ? -1 : gs.size() - 1; j >= 0; j--) { 185 sg2 = (SchemaGrammar)gs.elementAt(j); 186 for (k = 0; k < len; k++) { 188 if (sg2 == fGrammarList[k]) 189 break; 190 } 191 if (k == len) { 193 if (len == fGrammarList.length) { 195 String [] newSA = new String [len*2]; 196 System.arraycopy(fNamespaces, 0, newSA, 0, len); 197 fNamespaces = newSA; 198 SchemaGrammar[] newGA = new SchemaGrammar[len*2]; 199 System.arraycopy(fGrammarList, 0, newGA, 0, len); 200 fGrammarList = newGA; 201 } 202 fNamespaces[len] = sg2.getTargetNamespace(); 203 fGrammarList[len] = sg2; 204 len++; 205 } 206 } 207 } 208 209 fGrammarMap = new SymbolHash(len*2); 211 for (i = 0; i < len; i++) { 212 fGrammarMap.put(null2EmptyString(fNamespaces[i]), fGrammarList[i]); 213 if (fGrammarList[i].hasIDConstraints()) 215 fHasIDC = true; 216 } 217 218 fGrammarCount = len; 219 fGlobalComponents = new XSNamedMap[MAX_COMP_IDX+1]; 220 fNSComponents = new XSNamedMap[len][MAX_COMP_IDX+1]; 221 222 buildSubGroups(); 224 } 225 226 private void buildSubGroups() { 227 SubstitutionGroupHandler sgHandler = new SubstitutionGroupHandler(null); 228 for (int i = 0 ; i < fGrammarCount; i++) { 229 sgHandler.addSubstitutionGroup(fGrammarList[i].getSubstitutionGroups()); 230 } 231 232 XSNamedMap elements = getComponents(XSConstants.ELEMENT_DECLARATION); 233 int len = elements.getLength(); 234 fSubGroupMap = new SymbolHash(len*2); 235 XSElementDecl head; 236 XSElementDeclaration[] subGroup; 237 for (int i = 0; i < len; i++) { 238 head = (XSElementDecl)elements.item(i); 239 subGroup = sgHandler.getSubstitutionGroup(head); 240 fSubGroupMap.put(head, new XSObjectListImpl(subGroup, subGroup.length)); 241 } 242 } 243 244 250 public StringList getNamespaces() { 251 return new StringListImpl(fNamespaces, fGrammarCount); 253 } 254 255 256 public XSNamespaceItemList getNamespaceItems() { 257 258 return new NSItemListImpl(fGrammarList, fGrammarCount); 260 } 261 262 270 public synchronized XSNamedMap getComponents(short objectType) { 271 if (objectType <= 0 || objectType > MAX_COMP_IDX || 272 !GLOBAL_COMP[objectType]) { 273 return null; 274 } 275 276 SymbolHash[] tables = new SymbolHash[fGrammarCount]; 277 if (fGlobalComponents[objectType] == null) { 279 for (int i = 0; i < fGrammarCount; i++) { 280 switch (objectType) { 281 case XSConstants.TYPE_DEFINITION: 282 case XSTypeDefinition.COMPLEX_TYPE: 283 case XSTypeDefinition.SIMPLE_TYPE: 284 tables[i] = fGrammarList[i].fGlobalTypeDecls; 285 break; 286 case XSConstants.ATTRIBUTE_DECLARATION: 287 tables[i] = fGrammarList[i].fGlobalAttrDecls; 288 break; 289 case XSConstants.ELEMENT_DECLARATION: 290 tables[i] = fGrammarList[i].fGlobalElemDecls; 291 break; 292 case XSConstants.ATTRIBUTE_GROUP: 293 tables[i] = fGrammarList[i].fGlobalAttrGrpDecls; 294 break; 295 case XSConstants.MODEL_GROUP_DEFINITION: 296 tables[i] = fGrammarList[i].fGlobalGroupDecls; 297 break; 298 case XSConstants.NOTATION_DECLARATION: 299 tables[i] = fGrammarList[i].fGlobalNotationDecls; 300 break; 301 } 302 } 303 if (objectType == XSTypeDefinition.COMPLEX_TYPE || 306 objectType == XSTypeDefinition.SIMPLE_TYPE) { 307 fGlobalComponents[objectType] = new XSNamedMap4Types(fNamespaces, tables, fGrammarCount, objectType); 308 } 309 else { 310 fGlobalComponents[objectType] = new XSNamedMapImpl(fNamespaces, tables, fGrammarCount); 311 } 312 } 313 314 return fGlobalComponents[objectType]; 315 } 316 317 329 public synchronized XSNamedMap getComponentsByNamespace(short objectType, 330 String namespace) { 331 if (objectType <= 0 || objectType > MAX_COMP_IDX || 332 !GLOBAL_COMP[objectType]) { 333 return null; 334 } 335 336 int i = 0; 338 for (; i < fGrammarCount; i++) { 339 if (fNamespaces[i] == namespace) 340 break; 341 } 342 if (i == fGrammarCount) 343 return null; 344 345 if (fNSComponents[i][objectType] == null) { 347 SymbolHash table = null; 348 switch (objectType) { 349 case XSConstants.TYPE_DEFINITION: 350 case XSTypeDefinition.COMPLEX_TYPE: 351 case XSTypeDefinition.SIMPLE_TYPE: 352 table = fGrammarList[i].fGlobalTypeDecls; 353 break; 354 case XSConstants.ATTRIBUTE_DECLARATION: 355 table = fGrammarList[i].fGlobalAttrDecls; 356 break; 357 case XSConstants.ELEMENT_DECLARATION: 358 table = fGrammarList[i].fGlobalElemDecls; 359 break; 360 case XSConstants.ATTRIBUTE_GROUP: 361 table = fGrammarList[i].fGlobalAttrGrpDecls; 362 break; 363 case XSConstants.MODEL_GROUP_DEFINITION: 364 table = fGrammarList[i].fGlobalGroupDecls; 365 break; 366 case XSConstants.NOTATION_DECLARATION: 367 table = fGrammarList[i].fGlobalNotationDecls; 368 break; 369 } 370 371 if (objectType == XSTypeDefinition.COMPLEX_TYPE || 374 objectType == XSTypeDefinition.SIMPLE_TYPE) { 375 fNSComponents[i][objectType] = new XSNamedMap4Types(namespace, table, objectType); 376 } 377 else { 378 fNSComponents[i][objectType] = new XSNamedMapImpl(namespace, table); 379 } 380 } 381 382 return fNSComponents[i][objectType]; 383 } 384 385 393 public XSTypeDefinition getTypeDefinition(String name, 394 String namespace) { 395 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 396 if (sg == null) 397 return null; 398 return (XSTypeDefinition)sg.fGlobalTypeDecls.get(name); 399 } 400 401 408 public XSAttributeDeclaration getAttributeDeclaration(String name, 409 String namespace) { 410 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 411 if (sg == null) 412 return null; 413 return (XSAttributeDeclaration)sg.fGlobalAttrDecls.get(name); 414 } 415 416 423 public XSElementDeclaration getElementDeclaration(String name, 424 String namespace) { 425 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 426 if (sg == null) 427 return null; 428 return (XSElementDeclaration)sg.fGlobalElemDecls.get(name); 429 } 430 431 438 public XSAttributeGroupDefinition getAttributeGroup(String name, 439 String namespace) { 440 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 441 if (sg == null) 442 return null; 443 return (XSAttributeGroupDefinition)sg.fGlobalAttrGrpDecls.get(name); 444 } 445 446 454 public XSModelGroupDefinition getModelGroupDefinition(String name, 455 String namespace) { 456 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 457 if (sg == null) 458 return null; 459 return (XSModelGroupDefinition)sg.fGlobalGroupDecls.get(name); 460 } 461 462 463 466 public XSNotationDeclaration getNotationDeclaration(String name, 467 String namespace) { 468 SchemaGrammar sg = (SchemaGrammar)fGrammarMap.get(null2EmptyString(namespace)); 469 if (sg == null) 470 return null; 471 return (XSNotationDeclaration)sg.fGlobalNotationDecls.get(name); 472 } 473 474 477 public synchronized XSObjectList getAnnotations() { 478 if(fAnnotations != null) 479 return fAnnotations; 480 481 int totalAnnotations = 0; 483 for (int i = 0; i < fGrammarCount; i++) { 484 totalAnnotations += fGrammarList[i].fNumAnnotations; 485 } 486 XSAnnotationImpl [] annotations = new XSAnnotationImpl [totalAnnotations]; 487 int currPos = 0; 488 for (int i = 0; i < fGrammarCount; i++) { 489 SchemaGrammar currGrammar = fGrammarList[i]; 490 System.arraycopy(currGrammar.fAnnotations, 0, annotations, currPos, currGrammar.fNumAnnotations); 491 currPos += currGrammar.fNumAnnotations; 492 } 493 fAnnotations = new XSObjectListImpl(annotations, annotations.length); 494 return fAnnotations; 495 } 496 497 private static final String null2EmptyString(String str) { 498 return str == null ? XMLSymbols.EMPTY_STRING : str; 499 } 500 501 507 public boolean hasIDConstraints() { 508 return fHasIDC; 509 } 510 511 515 public XSObjectList getSubstitutionGroup(XSElementDeclaration head) { 516 return (XSObjectList)fSubGroupMap.get(head); 517 } 518 519 } | Popular Tags |