1 22 23 package org.xquark.schema; 24 25 import java.util.*; 26 27 43 public class Schema implements SchemaConstants, SchemaVisitable, SchemaScope { 44 private static final String RCSRevision = "$Revision: 1.2 $"; 45 private static final String RCSName = "$Name: $"; 46 47 public static final int DATATYPE = 0; 48 public static final int ELEMENT_DECLARATION = 1; 49 public static final int ATTRIBUTE_DECLARATION = 2; 50 public static final int MODEL_GROUP_DEFINITION = 3; 51 public static final int ATTRIBUTE_GROUP_DEFINITION = 4; 52 public static final int NOTATION_DECLARATION = 5; 53 54 private HashMap dataTypes = new HashMap(); 55 private HashMap modelGroupDefinitions = new HashMap(); 56 private HashMap attributeGroupDefinitions = new HashMap(); 57 private HashMap elementDeclarations = new HashMap(); 58 private HashMap attributeDeclarations = new HashMap(); 59 private HashMap notationDeclarations = new HashMap(); 60 61 private String targetNamespace; 62 private String schemaLocation; 63 private SchemaManager manager; 64 private boolean loaded = false; 65 private HashSet includedLocations = new HashSet(); 66 private HashSet importedNamespaces = new HashSet(); 67 private LinkedList defaults = new LinkedList(); 68 private HashMap prefixMap = null; 69 private String prefix; 70 private InitializationVisitor initializer = null; 71 72 73 private HashMap identityConstraints = null; 76 77 85 public Schema(String uri, String location, SchemaManager manager) { 86 targetNamespace = uri; 87 schemaLocation = location; 88 this.manager = manager; 89 pushDefaults(); 90 addImportedNamespace(XMLSCHEMA_URI); 91 initializer = new InitializationVisitor(this); 92 } 93 94 public void accept(SchemaVisitor visitor) throws SchemaException { 96 visitor.visit(this); 97 } 98 99 100 public void initialize() throws SchemaException { 101 102 java.util.Iterator it = getTypes().iterator(); 103 while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer); 104 105 it = getElementDeclarations().iterator(); 106 while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer); 107 108 it = getAttributeDeclarations().iterator(); 109 while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer); 110 111 it = getAttributeGroupDefinitions().iterator(); 112 while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer); 113 } 114 115 120 public String getNamespace() { 121 return targetNamespace; 122 } 123 124 129 public String toString() { 130 return targetNamespace; 131 } 132 133 138 public boolean equals(Schema schema) { 139 return ( targetNamespace.equals(schema.getNamespace()) ); 140 } 141 142 147 public String getLocation() { 148 return schemaLocation; 149 } 150 151 156 public Collection getSchemaDocuments() { 157 ArrayList result = new ArrayList(); 158 if (schemaLocation != null) result.add(schemaLocation); 159 result.addAll(includedLocations); 160 return result; 161 } 162 163 166 public Set getImportedNamespaces() { 167 return Collections.unmodifiableSet(importedNamespaces); 168 } 169 170 175 public SchemaManager getManager() { 176 return manager; 177 } 178 179 186 public Type getType(String name) { 187 return (Type)getSchemaComponent(name, dataTypes); 188 } 189 190 197 public ElementDeclaration getElementDeclaration(String name) { 198 return (ElementDeclaration)getSchemaComponent(name, elementDeclarations); 199 } 200 201 208 public AttributeDeclaration getAttributeDeclaration(String name) { 209 return (AttributeDeclaration)getSchemaComponent(name, attributeDeclarations); 210 } 211 212 219 public ModelGroupDefinition getModelGroupDefinition(String name) { 220 return (ModelGroupDefinition)getSchemaComponent(name, modelGroupDefinitions); 221 } 222 223 230 public AttributeGroupDefinition getAttributeGroupDefinition(String name) { 231 return (AttributeGroupDefinition)getSchemaComponent(name, attributeGroupDefinitions); 232 } 233 234 241 public NotationDeclaration getNotationDeclaration(String name) { 242 return (NotationDeclaration)getSchemaComponent(name, notationDeclarations); 243 } 244 245 public SchemaComponent getSchemaComponent(String name, int type) { 246 switch (type) { 247 case DATATYPE: return getType(name); 248 case ELEMENT_DECLARATION: return getElementDeclaration(name); 249 case ATTRIBUTE_DECLARATION: return getAttributeDeclaration(name); 250 case MODEL_GROUP_DEFINITION: return getModelGroupDefinition(name); 251 case ATTRIBUTE_GROUP_DEFINITION: return getAttributeGroupDefinition(name); 252 case NOTATION_DECLARATION: return getNotationDeclaration(name); 253 default: return null; 254 } 255 } 256 257 public boolean isGlobalScope() { 258 return true; 259 } 260 261 public ElementDeclaration getElementDeclaration(String namespace, String name) { 262 if (!targetNamespace.equals(namespace)) return null; 263 return getElementDeclaration(name); 264 } 265 266 public AttributeDeclaration getAttributeDeclaration(String namespace, String name) { 267 if (!targetNamespace.equals(namespace)) return null; 268 return getAttributeDeclaration(name); 269 } 270 271 276 public Collection getTypes() { 277 return dataTypes.values(); 278 } 279 280 285 public Collection getElementDeclarations() { 286 return elementDeclarations.values(); 287 } 288 289 294 public Collection getAttributeDeclarations() { 295 return attributeDeclarations.values(); 296 } 297 298 303 public Collection getModelGroupDefinitions() { 304 return modelGroupDefinitions.values(); 305 } 306 307 312 public Collection getAttributeGroupDefinitions() { 313 return attributeGroupDefinitions.values(); 314 } 315 320 public Collection getNotationDeclarations() { 321 return notationDeclarations.values(); 322 } 323 324 public HashMap getNotationDeclarationMap() { 325 return notationDeclarations; 326 } 327 328 private SchemaComponent getSchemaComponent(String name, HashMap map) { 329 SchemaComponent result = (SchemaComponent)map.get(name); 330 return result; 331 } 332 333 boolean isLoaded() { 334 return loaded; 335 } 336 337 void setLoaded(boolean loaded) { 338 this.loaded = loaded; 339 } 340 341 public boolean isIncluded(String location) { 342 return includedLocations.contains(location); 343 } 344 345 public void addIncludedLocation(String location) { 346 includedLocations.add(location); 347 } 348 349 public boolean isImported(String namespace) { 350 return importedNamespaces.contains(namespace); 351 } 352 353 public void addImportedNamespace(String namespace) { 354 importedNamespaces.add(namespace); 355 } 356 357 public void register(Type entity) throws SchemaException { 358 register(entity, dataTypes); 359 } 360 361 public void register(ElementDeclaration entity) throws SchemaException { 362 register(entity, elementDeclarations); 363 } 364 365 public void register(AttributeDeclaration entity) throws SchemaException { 366 register(entity, attributeDeclarations); 367 } 368 369 public void register(ModelGroupDefinition entity) throws SchemaException { 370 register(entity, modelGroupDefinitions); 371 } 372 373 public void register(AttributeGroupDefinition entity) throws SchemaException { 374 register(entity, attributeGroupDefinitions); 375 } 376 377 public void register(NotationDeclaration entity) throws SchemaException { 378 register(entity, notationDeclarations); 379 } 380 381 private void register(SchemaComponent comp, HashMap map) throws SchemaException { 382 String name = comp.getName(); 383 SchemaComponent existing = getSchemaComponent(name, map); 384 if (existing != null && !existing.equals(comp)) 385 throw new SchemaException("sch-props-correct.2", comp); 387 map.put(name, comp); 388 } 389 390 public void removeRegisteredComponent(Type entity) { 391 dataTypes.remove(entity.getName()); 392 } 393 394 public void removeRegisteredComponent(ElementDeclaration entity) { 395 elementDeclarations.remove(entity.getName()); 396 } 397 398 public void removeRegisteredComponent(AttributeDeclaration entity) { 399 attributeDeclarations.remove(entity.getName()); 400 } 401 402 public void removeRegisteredComponent(ModelGroupDefinition entity) { 403 modelGroupDefinitions.remove(entity.getName()); 404 } 405 406 public void removeRegisteredComponent(AttributeGroupDefinition entity) { 407 attributeGroupDefinitions.remove(entity.getName()); 408 } 409 410 public void removeRegisteredComponent(NotationDeclaration entity) { 411 notationDeclarations.remove(entity.getName()); 412 } 413 414 public void pushDefaults() { 415 defaults.addFirst(new HashMap()); 416 } 417 418 public void popDefaults() { 419 defaults.removeFirst(); 420 } 421 422 public void setDefault(String key, String val) { 423 ((HashMap)defaults.getFirst()).put(key, val); 424 } 425 426 public String getDefault(String key) { 427 return (String )((HashMap)defaults.getFirst()).get(key); 428 } 429 430 public void setPrefixMap(HashMap map) { 431 prefixMap = map; 432 433 Iterator it = ((Set)prefixMap.entrySet()). iterator(); 435 while (it.hasNext()) { 436 Map.Entry entry = (Map.Entry)it.next(); 437 if ( getNamespace() == null ) { 438 prefix = ""; 439 break; 440 } 441 if ( getNamespace().equals(entry.getValue()) ) { 442 prefix = (String )entry.getKey(); 443 break; 444 } 445 } 446 } 447 448 public HashMap getPrefixMap() { 449 return prefixMap; 450 } 451 452 public String getPrefix() { 453 return prefix; 454 } 455 456 public void register(IdentityConstraint entity) throws SchemaException { 458 if ( identityConstraints == null ) identityConstraints = new HashMap(); 459 register(entity, identityConstraints); 460 } 461 462 public Collection getIdentityConstraints() { 463 if ( identityConstraints == null ) return null; 464 return identityConstraints.values(); 465 } 466 467 public IdentityConstraint getIdentityConstraint(String name) { 469 if ( identityConstraints == null ) return null; 470 if ( name.indexOf(':') != -1 ) { 471 name = name.substring(name.indexOf(':') + 1); 472 } 473 IdentityConstraint result = (IdentityConstraint)identityConstraints.get(name); 474 return result; 475 } 476 477 } 478 | Popular Tags |