1 17 package org.apache.ws.jaxme.xs.impl; 18 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 import java.util.Map ; 26 import java.util.NoSuchElementException ; 27 28 import org.apache.ws.jaxme.xs.XSAnnotation; 29 import org.apache.ws.jaxme.xs.XSAttribute; 30 import org.apache.ws.jaxme.xs.XSAttributeGroup; 31 import org.apache.ws.jaxme.xs.XSElement; 32 import org.apache.ws.jaxme.xs.XSGroup; 33 import org.apache.ws.jaxme.xs.XSIdentityConstraint; 34 import org.apache.ws.jaxme.xs.XSKeyRef; 35 import org.apache.ws.jaxme.xs.XSNotation; 36 import org.apache.ws.jaxme.xs.XSObject; 37 import org.apache.ws.jaxme.xs.XSObjectFactory; 38 import org.apache.ws.jaxme.xs.XSSchema; 39 import org.apache.ws.jaxme.xs.XSType; 40 import org.apache.ws.jaxme.xs.parser.XSContext; 41 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException; 42 import org.apache.ws.jaxme.xs.types.*; 43 import org.apache.ws.jaxme.xs.xml.XsAnyURI; 44 import org.apache.ws.jaxme.xs.xml.XsESchema; 45 import org.apache.ws.jaxme.xs.xml.XsQName; 46 import org.xml.sax.Attributes ; 47 import org.xml.sax.Locator ; 48 import org.xml.sax.SAXException ; 49 50 51 56 public class XSSchemaImpl implements XSSchema { 57 private static final XSType[] BUILTIN_TYPES = new XSType[]{ 58 XSAnySimpleType.getInstance(), 59 XSAnyURI.getInstance(), 60 XSBase64Binary.getInstance(), 61 XSBoolean.getInstance(), 62 XSByte.getInstance(), 63 XSDate.getInstance(), 64 XSDateTime.getInstance(), 65 XSDecimal.getInstance(), 66 XSDouble.getInstance(), 67 XSDuration.getInstance(), 68 XSEntities.getInstance(), 69 XSEntity.getInstance(), 70 XSFloat.getInstance(), 71 XSGDay.getInstance(), 72 XSGMonth.getInstance(), 73 XSGMonthDay.getInstance(), 74 XSGYear.getInstance(), 75 XSGYearMonth.getInstance(), 76 XSHexBinary.getInstance(), 77 XSID.getInstance(), 78 XSIDREF.getInstance(), 79 XSIDREFs.getInstance(), 80 XSInt.getInstance(), 81 XSInteger.getInstance(), 82 XSLanguage.getInstance(), 83 XSLong.getInstance(), 84 XSName.getInstance(), 85 XSNCName.getInstance(), 86 XSNegativeInteger.getInstance(), 87 XSNMToken.getInstance(), 88 XSNMTokens.getInstance(), 89 XSNonNegativeInteger.getInstance(), 90 XSNonPositiveInteger.getInstance(), 91 XSNormalizedString.getInstance(), 92 org.apache.ws.jaxme.xs.types.XSNotation.getInstance(), 93 XSPositiveInteger.getInstance(), 94 XSQName.getInstance(), 95 XSShort.getInstance(), 96 XSString.getInstance(), 97 XSTime.getInstance(), 98 XSToken.getInstance(), 99 XSUnsignedByte.getInstance(), 100 XSUnsignedInt.getInstance(), 101 XSUnsignedLong.getInstance(), 102 XSUnsignedShort.getInstance(), 103 XSAnyType.getInstance() 104 }; 105 106 private final XSContext context; 107 private final XsESchema syntaxSchema; 108 private final List childs = new ArrayList (1); 109 private final Map types = new HashMap (1); 110 private final Map builtinTypes = new HashMap (1); 111 private final Map groups = new HashMap (1); 112 private final Map attributeGroups = new HashMap (1); 113 private final Map attributes = new HashMap (1); 114 private final Map elements = new HashMap (1); 115 private final Attributes openAttrs; 116 117 private final Map identityConstraintsMap = new HashMap (1); 118 private final Map keyRefsMap = new HashMap (1); 119 120 private final Map immutableIdentityConstraintsMap 121 = Collections.unmodifiableMap( identityConstraintsMap ); 122 private final Map immutableKeyRefsMap = Collections.unmodifiableMap( 123 keyRefsMap 124 ); 125 126 private boolean isValidated; 127 128 134 public XSSchemaImpl(XSContext pContext, XsESchema pSchema) { 135 context = pContext; 136 syntaxSchema = pSchema; 137 for (int i = 0; i < BUILTIN_TYPES.length; i++) { 138 builtinTypes.put(BUILTIN_TYPES[i].getName(), BUILTIN_TYPES[i]); 139 } 140 openAttrs = pSchema.getOpenAttributes(); 141 } 142 143 public XSContext getContext() { return context; } 144 public boolean isTopLevelObject() { return true; } 145 public XSObject getParentObject() { return null; } 146 public XSObjectFactory getXSObjectFactory() { return context.getXSObjectFactory(); } 147 public Locator getLocator() { return syntaxSchema.getLocator(); } 148 public XSSchema getXSSchema() { return this; } 149 protected XsESchema getXsESchema() { return syntaxSchema; } 150 151 153 protected void addChild(Object pChild) { 154 childs.add(pChild); 155 } 156 157 161 protected void replace(Object pOldChild, Object pNewChild) { 162 for (ListIterator iter = childs.listIterator(); iter.hasNext(); ) { 163 Object o = iter.next(); 164 if (o.equals(pOldChild)) { 165 iter.set(pNewChild); 166 return; 167 } 168 } 169 throw new NoSuchElementException (); 170 } 171 172 public XSAnnotation[] getAnnotations() { 173 List result = new ArrayList (); 174 for (Iterator iter = childs.iterator(); iter.hasNext(); ) { 175 Object o = iter.next(); 176 if (o instanceof XSAnnotation) { 177 result.add(o); 178 } 179 } 180 return (XSAnnotation[]) result.toArray(new XSAnnotation[result.size()]); 181 } 182 183 public XSType[] getTypes() { 184 List result = new ArrayList (); 185 for (Iterator iter = childs.iterator(); iter.hasNext(); ) { 186 Object o = iter.next(); 187 if (o instanceof XSType) { 188 result.add(o); 189 } 190 } 191 return (XSType[]) result.toArray(new XSType[result.size()]); 192 } 193 194 public XSType[] getBuiltinTypes() { 195 return BUILTIN_TYPES; 196 } 197 198 public XSType getType(XsQName pName) { 199 XSType result = (XSType) types.get(pName); 200 if (result == null) { 201 result = (XSType) builtinTypes.get(pName); 202 } 203 return result; 204 } 205 206 public XSGroup[] getGroups() { 207 List result = new ArrayList (); 208 for (Iterator iter = childs.iterator(); iter.hasNext(); ) { 209 Object o = iter.next(); 210 if (o instanceof XSGroup) { 211 result.add(o); 212 } 213 } 214 return (XSGroup[]) result.toArray(new XSGroup[result.size()]); 215 } 216 217 public XSGroup getGroup(XsQName pName) { 218 return (XSGroup) groups.get(pName); 219 } 220 221 public XSAttributeGroup[] getAttributeGroups() { 222 List result = new ArrayList (); 223 for (Iterator iter = childs.iterator(); iter.hasNext(); ) { 224 Object o = iter.next(); 225 if (o instanceof XSAttributeGroup) { 226 result.add(o); 227 } 228 } 229 return (XSAttributeGroup[]) result.toArray(new XSAttributeGroup[result.size()]); 230 } 231 232 public XSAttributeGroup getAttributeGroup(XsQName pName) { 233 return (XSAttributeGroup) attributeGroups.get(pName); 234 } 235 236 public XSElement[] getElements() { 237 List result = new ArrayList (); 238 for (Iterator iter = childs.iterator(); iter.hasNext(); ) { 239 Object o = iter.next(); 240 if (o instanceof XSElement) { 241 result.add(o); 242 } 243 } 244 return (XSElement[]) result.toArray(new XSElement[result.size()]); 245 } 246 247 public XSElement getElement(XsQName pName) { 248 return (XSElement) elements.get(pName); 249 } 250 251 public Map getIdentityConstraints() { 252 return immutableIdentityConstraintsMap; 253 } 254 255 public Map getKeyRefs() { 256 return immutableKeyRefsMap; 257 } 258 259 public void add( XSIdentityConstraint ic ) throws SAXException { 260 String name = ic.getName(); 261 262 if ( name == null ) { 263 throw new LocSAXException( 264 "An identity constraint must have a 'name' attribute.", 265 ic.getLocator() 266 ); 267 } else if ( identityConstraintsMap.put( name, ic ) != null ) { 268 throw new LocSAXException( 269 "No two identity constraints may share the same name.", 270 ic.getLocator() 271 ); 272 } 273 274 identityConstraintsMap.put( name, ic ); 275 } 276 277 public void add( XSKeyRef rf ) throws SAXException { 278 String name = rf.getName(); 279 280 if ( name == null ) { 281 throw new LocSAXException( 282 "A key ref must have a 'name' attribute.", 283 rf.getLocator() 284 ); 285 } else if ( keyRefsMap.put( name, rf ) != null ) { 286 throw new LocSAXException( 287 "No two key refs may share the same name.", 288 rf.getLocator() 289 ); 290 } 291 292 keyRefsMap.put( name, rf ); 293 } 294 295 296 297 public XSAttribute[] getAttributes() { 298 List result = new ArrayList (); 299 for (Iterator iter = childs.iterator(); iter.hasNext(); ) { 300 Object o = iter.next(); 301 if (o instanceof XSAttribute) { 302 result.add(o); 303 } 304 } 305 return (XSAttribute[]) result.toArray(new XSAttribute[result.size()]); 306 } 307 308 public XSAttribute getAttribute(XsQName pQName) { 309 return (XSAttribute) attributes.get(pQName); 310 } 311 312 public void add(XSAnnotation pAnnotation) { 313 addChild(pAnnotation); 314 } 315 316 public void add(XSType pType) throws SAXException { 317 XsQName name = pType.getName(); 318 if (name == null) { 319 throw new LocSAXException("A global type must have a 'name' attribute.", pType.getLocator()); 320 } 321 if (types.containsKey(name)) { 322 throw new LocSAXException("A global type " + name + " is already defined.", pType.getLocator()); 323 } 324 types.put(name, pType); 325 pType.setGlobal(true); 326 addChild(pType); 327 } 328 329 public void redefine(XSType pType) throws SAXException { 330 XsQName name = pType.getName(); 331 if (name == null) { 332 throw new LocSAXException("A global type must have a 'name' attribute.", pType.getLocator()); 333 } 334 Object oldType = types.get(name); 335 if (oldType == null) { 336 throw new LocSAXException("The global type " + name + " cannot be redefined, as it doesn't yet exist.", 337 pType.getLocator()); 338 } 339 types.put(name, pType); 340 pType.setGlobal(true); 341 replace(oldType, pType); 342 } 343 344 public void add(XSGroup pGroup) throws SAXException { 345 XsQName name = pGroup.getName(); 346 if (name == null) { 347 throw new LocSAXException("A global group must have a 'name' attribute.", pGroup.getLocator()); 348 } 349 if (groups.containsKey(name)) { 350 throw new LocSAXException("A global group " + name + " is already defined.", pGroup.getLocator()); 351 } 352 groups.put(name, pGroup); 353 addChild(pGroup); 354 pGroup.setGlobal(true); 355 } 356 357 public void redefine(XSGroup pGroup) throws SAXException { 358 XsQName name = pGroup.getName(); 359 if (name == null) { 360 throw new LocSAXException("A global group must have a 'name' attribute.", pGroup.getLocator()); 361 } 362 Object oldGroup = groups.get(name); 363 if (oldGroup == null) { 364 throw new LocSAXException("The global group " + name + " cannot be redefined, as it doesn't yet exist.", 365 pGroup.getLocator()); 366 } 367 groups.put(name, pGroup); 368 replace(oldGroup, pGroup); 369 } 370 371 public void add(XSAttributeGroup pGroup) throws SAXException { 372 XsQName name = pGroup.getName(); 373 if (name == null) { 374 throw new LocSAXException("A global attribute group must have a 'name' attribute.", pGroup.getLocator()); 375 } 376 if (attributeGroups.containsKey(name)) { 377 throw new LocSAXException("A global attribute group " + name + " is already defined.", pGroup.getLocator()); 378 } 379 attributeGroups.put(name, pGroup); 380 addChild(pGroup); 381 } 382 383 public void redefine(XSAttributeGroup pGroup) throws SAXException { 384 XsQName name = pGroup.getName(); 385 if (name == null) { 386 throw new LocSAXException("A global attribute group must have a 'name' attribute.", pGroup.getLocator()); 387 } 388 Object oldGroup = attributeGroups.get(name); 389 if (!attributeGroups.containsKey(name)) { 390 throw new LocSAXException("The global attribute group " + name + " cannot be redefined, as it doesn't yet exist.", 391 pGroup.getLocator()); 392 } 393 attributeGroups.put(name, pGroup); 394 replace(oldGroup, pGroup); 395 } 396 397 public void add(XSAttribute pAttribute) throws SAXException { 398 XsQName name = pAttribute.getName(); 399 if (name == null) { 400 throw new LocSAXException("A global attribute must have a 'name' attribute.", pAttribute.getLocator()); 401 } 402 if (attributes.containsKey(name)) { 403 throw new LocSAXException("A global attribute " + name + " is already defined.", pAttribute.getLocator()); 404 } 405 attributes.put(name, pAttribute); 406 addChild(pAttribute); 407 } 408 409 public void add(XSElement pElement) throws SAXException { 410 XsQName name = pElement.getName(); 411 if (name == null) { 412 throw new LocSAXException("A global element must have a 'name' attribute.", pElement.getLocator()); 413 } 414 if (elements.containsKey(name)) { 415 throw new LocSAXException("A global element " + name + " is already defined.", pElement.getLocator()); 416 } 417 elements.put(name, pElement); 418 addChild(pElement); 419 } 420 421 public void add(XSNotation pNotation) { 422 addChild(pNotation); 423 } 424 425 public Object [] getChilds() { 426 return childs.toArray(); 427 } 428 429 protected void validate(Object pChild) throws SAXException { 430 if (pChild instanceof XSObject) { 431 ((XSObject) pChild).validate(); 432 } else { 433 throw new IllegalStateException ("Unable to validate the child " + pChild + 434 ", perhaps you should overwrite the method " + 435 getClass().getName() + ".validate(Object)."); 436 } 437 } 438 439 protected boolean isValidated() { 440 return isValidated; 441 } 442 443 public void validate() throws SAXException { 444 if (isValidated()) { 445 return; 446 } else { 447 isValidated = true; 448 } 449 450 Object [] myChilds = getChilds(); 451 for (int i = 0; i < myChilds.length; i++) { 452 validate(myChilds[i]); 453 } 454 } 455 456 public Attributes getOpenAttributes() { 457 return openAttrs; 458 } 459 460 public XsAnyURI getTargetNamespace() { 461 return syntaxSchema.getTargetNamespace(); 462 } 463 } 464 | Popular Tags |