001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.xsom.impl;
021:
022: import com.sun.xml.xsom.SCD;
023: import com.sun.xml.xsom.XSAttGroupDecl;
024: import com.sun.xml.xsom.XSAttributeDecl;
025: import com.sun.xml.xsom.XSAttributeUse;
026: import com.sun.xml.xsom.XSComplexType;
027: import com.sun.xml.xsom.XSComponent;
028: import com.sun.xml.xsom.XSContentType;
029: import com.sun.xml.xsom.XSElementDecl;
030: import com.sun.xml.xsom.XSFacet;
031: import com.sun.xml.xsom.XSIdentityConstraint;
032: import com.sun.xml.xsom.XSListSimpleType;
033: import com.sun.xml.xsom.XSModelGroup;
034: import com.sun.xml.xsom.XSModelGroupDecl;
035: import com.sun.xml.xsom.XSNotation;
036: import com.sun.xml.xsom.XSParticle;
037: import com.sun.xml.xsom.XSRestrictionSimpleType;
038: import com.sun.xml.xsom.XSSchema;
039: import com.sun.xml.xsom.XSSchemaSet;
040: import com.sun.xml.xsom.XSSimpleType;
041: import com.sun.xml.xsom.XSType;
042: import com.sun.xml.xsom.XSUnionSimpleType;
043: import com.sun.xml.xsom.XSVariety;
044: import com.sun.xml.xsom.XSWildcard;
045: import com.sun.xml.xsom.impl.scd.Iterators;
046: import com.sun.xml.xsom.visitor.XSContentTypeFunction;
047: import com.sun.xml.xsom.visitor.XSContentTypeVisitor;
048: import com.sun.xml.xsom.visitor.XSFunction;
049: import com.sun.xml.xsom.visitor.XSSimpleTypeFunction;
050: import com.sun.xml.xsom.visitor.XSSimpleTypeVisitor;
051: import com.sun.xml.xsom.visitor.XSVisitor;
052: import org.xml.sax.Locator;
053:
054: import javax.xml.namespace.NamespaceContext;
055: import java.text.ParseException;
056: import java.util.Collection;
057: import java.util.Collections;
058: import java.util.HashMap;
059: import java.util.Iterator;
060: import java.util.List;
061: import java.util.Map;
062: import java.util.Vector;
063:
064: public class SchemaSetImpl implements XSSchemaSet {
065: private final Map<String, XSSchema> schemas = new HashMap<String, XSSchema>();
066: private final Vector<XSSchema> schemas2 = new Vector<XSSchema>();
067: private final List<XSSchema> readonlySchemaList = Collections
068: .unmodifiableList(schemas2);
069:
070: /**
071: * Gets a reference to the existing schema or creates a new one
072: * if none exists yet.
073: */
074: public SchemaImpl createSchema(String targetNamespace,
075: Locator location) {
076: SchemaImpl obj = (SchemaImpl) schemas.get(targetNamespace);
077: if (obj == null) {
078: obj = new SchemaImpl(this , location, targetNamespace);
079: schemas.put(targetNamespace, obj);
080: schemas2.add(obj);
081: }
082: return obj;
083: }
084:
085: public int getSchemaSize() {
086: return schemas.size();
087: }
088:
089: public XSSchema getSchema(String targetNamespace) {
090: return schemas.get(targetNamespace);
091: }
092:
093: public XSSchema getSchema(int idx) {
094: return schemas2.get(idx);
095: }
096:
097: public Iterator<XSSchema> iterateSchema() {
098: return schemas2.iterator();
099: }
100:
101: public final Collection<XSSchema> getSchemas() {
102: return readonlySchemaList;
103: }
104:
105: public XSType getType(String ns, String localName) {
106: XSSchema schema = getSchema(ns);
107: if (schema == null)
108: return null;
109:
110: return schema.getType(localName);
111: }
112:
113: public XSSimpleType getSimpleType(String ns, String localName) {
114: XSSchema schema = getSchema(ns);
115: if (schema == null)
116: return null;
117:
118: return schema.getSimpleType(localName);
119: }
120:
121: public XSElementDecl getElementDecl(String ns, String localName) {
122: XSSchema schema = getSchema(ns);
123: if (schema == null)
124: return null;
125:
126: return schema.getElementDecl(localName);
127: }
128:
129: public XSAttributeDecl getAttributeDecl(String ns, String localName) {
130: XSSchema schema = getSchema(ns);
131: if (schema == null)
132: return null;
133:
134: return schema.getAttributeDecl(localName);
135: }
136:
137: public XSModelGroupDecl getModelGroupDecl(String ns,
138: String localName) {
139: XSSchema schema = getSchema(ns);
140: if (schema == null)
141: return null;
142:
143: return schema.getModelGroupDecl(localName);
144: }
145:
146: public XSAttGroupDecl getAttGroupDecl(String ns, String localName) {
147: XSSchema schema = getSchema(ns);
148: if (schema == null)
149: return null;
150:
151: return schema.getAttGroupDecl(localName);
152: }
153:
154: public XSComplexType getComplexType(String ns, String localName) {
155: XSSchema schema = getSchema(ns);
156: if (schema == null)
157: return null;
158:
159: return schema.getComplexType(localName);
160: }
161:
162: public XSIdentityConstraint getIdentityConstraint(String ns,
163: String localName) {
164: XSSchema schema = getSchema(ns);
165: if (schema == null)
166: return null;
167:
168: return schema.getIdentityConstraint(localName);
169: }
170:
171: public Iterator<XSElementDecl> iterateElementDecls() {
172: return new Iterators.Map<XSElementDecl, XSSchema>(
173: iterateSchema()) {
174: protected Iterator<XSElementDecl> apply(XSSchema u) {
175: return u.iterateElementDecls();
176: }
177: };
178: }
179:
180: public Iterator<XSType> iterateTypes() {
181: return new Iterators.Map<XSType, XSSchema>(iterateSchema()) {
182: protected Iterator<XSType> apply(XSSchema u) {
183: return u.iterateTypes();
184: }
185: };
186: }
187:
188: public Iterator<XSAttributeDecl> iterateAttributeDecls() {
189: return new Iterators.Map<XSAttributeDecl, XSSchema>(
190: iterateSchema()) {
191: protected Iterator<XSAttributeDecl> apply(XSSchema u) {
192: return u.iterateAttributeDecls();
193: }
194: };
195: }
196:
197: public Iterator<XSAttGroupDecl> iterateAttGroupDecls() {
198: return new Iterators.Map<XSAttGroupDecl, XSSchema>(
199: iterateSchema()) {
200: protected Iterator<XSAttGroupDecl> apply(XSSchema u) {
201: return u.iterateAttGroupDecls();
202: }
203: };
204: }
205:
206: public Iterator<XSModelGroupDecl> iterateModelGroupDecls() {
207: return new Iterators.Map<XSModelGroupDecl, XSSchema>(
208: iterateSchema()) {
209: protected Iterator<XSModelGroupDecl> apply(XSSchema u) {
210: return u.iterateModelGroupDecls();
211: }
212: };
213: }
214:
215: public Iterator<XSSimpleType> iterateSimpleTypes() {
216: return new Iterators.Map<XSSimpleType, XSSchema>(
217: iterateSchema()) {
218: protected Iterator<XSSimpleType> apply(XSSchema u) {
219: return u.iterateSimpleTypes();
220: }
221: };
222: }
223:
224: public Iterator<XSComplexType> iterateComplexTypes() {
225: return new Iterators.Map<XSComplexType, XSSchema>(
226: iterateSchema()) {
227: protected Iterator<XSComplexType> apply(XSSchema u) {
228: return u.iterateComplexTypes();
229: }
230: };
231: }
232:
233: public Iterator<XSNotation> iterateNotations() {
234: return new Iterators.Map<XSNotation, XSSchema>(iterateSchema()) {
235: protected Iterator<XSNotation> apply(XSSchema u) {
236: return u.iterateNotations();
237: }
238: };
239: }
240:
241: public Iterator<XSIdentityConstraint> iterateIdentityConstraints() {
242: return new Iterators.Map<XSIdentityConstraint, XSSchema>(
243: iterateSchema()) {
244: protected Iterator<XSIdentityConstraint> apply(XSSchema u) {
245: return u.getIdentityConstraints().values().iterator();
246: }
247: };
248: }
249:
250: public Collection<XSComponent> select(String scd,
251: NamespaceContext nsContext) {
252: try {
253: return SCD.create(scd, nsContext).select(this );
254: } catch (ParseException e) {
255: throw new IllegalArgumentException(e);
256: }
257: }
258:
259: public XSComponent selectSingle(String scd,
260: NamespaceContext nsContext) {
261: try {
262: return SCD.create(scd, nsContext).selectSingle(this );
263: } catch (ParseException e) {
264: throw new IllegalArgumentException(e);
265: }
266: }
267:
268: public final EmptyImpl empty = new EmptyImpl();
269:
270: public XSContentType getEmpty() {
271: return empty;
272: }
273:
274: public XSSimpleType getAnySimpleType() {
275: return anySimpleType;
276: }
277:
278: public final AnySimpleType anySimpleType = new AnySimpleType();
279:
280: private class AnySimpleType extends DeclarationImpl implements
281: XSRestrictionSimpleType, Ref.SimpleType {
282:
283: AnySimpleType() {
284: super (null, null, null, null,
285: "http://www.w3.org/2001/XMLSchema",
286: "anySimpleType", false);
287: }
288:
289: public SchemaImpl getOwnerSchema() {
290: return createSchema("http://www.w3.org/2001/XMLSchema",
291: null);
292: }
293:
294: public XSSimpleType asSimpleType() {
295: return this ;
296: }
297:
298: public XSComplexType asComplexType() {
299: return null;
300: }
301:
302: public boolean isDerivedFrom(XSType t) {
303: return t == this || t == anyType;
304: }
305:
306: public boolean isSimpleType() {
307: return true;
308: }
309:
310: public boolean isComplexType() {
311: return false;
312: }
313:
314: public XSContentType asEmpty() {
315: return null;
316: }
317:
318: public XSParticle asParticle() {
319: return null;
320: }
321:
322: public XSType getBaseType() {
323: return anyType;
324: }
325:
326: public XSSimpleType getSimpleBaseType() {
327: return null;
328: }
329:
330: public int getDerivationMethod() {
331: return RESTRICTION;
332: }
333:
334: public Iterator<XSFacet> iterateDeclaredFacets() {
335: return Iterators.empty();
336: }
337:
338: public Collection<? extends XSFacet> getDeclaredFacets() {
339: return Collections.EMPTY_LIST;
340: }
341:
342: public void visit(XSSimpleTypeVisitor visitor) {
343: visitor.restrictionSimpleType(this );
344: }
345:
346: public void visit(XSContentTypeVisitor visitor) {
347: visitor.simpleType(this );
348: }
349:
350: public void visit(XSVisitor visitor) {
351: visitor.simpleType(this );
352: }
353:
354: public <T> T apply(XSSimpleTypeFunction<T> f) {
355: return f.restrictionSimpleType(this );
356: }
357:
358: public <T> T apply(XSContentTypeFunction<T> f) {
359: return f.simpleType(this );
360: }
361:
362: public <T> T apply(XSFunction<T> f) {
363: return f.simpleType(this );
364: }
365:
366: public XSVariety getVariety() {
367: return XSVariety.ATOMIC;
368: }
369:
370: public XSSimpleType getPrimitiveType() {
371: return this ;
372: }
373:
374: public boolean isPrimitive() {
375: return true;
376: }
377:
378: public XSListSimpleType getBaseListType() {
379: return null;
380: }
381:
382: public XSUnionSimpleType getBaseUnionType() {
383: return null;
384: }
385:
386: public XSFacet getFacet(String name) {
387: return null;
388: }
389:
390: public XSFacet getDeclaredFacet(String name) {
391: return null;
392: }
393:
394: public List<XSFacet> getDeclaredFacets(String name) {
395: return Collections.EMPTY_LIST;
396: }
397:
398: public boolean isRestriction() {
399: return true;
400: }
401:
402: public boolean isList() {
403: return false;
404: }
405:
406: public boolean isUnion() {
407: return false;
408: }
409:
410: public boolean isFinal(XSVariety v) {
411: return false;
412: }
413:
414: public XSRestrictionSimpleType asRestriction() {
415: return this ;
416: }
417:
418: public XSListSimpleType asList() {
419: return null;
420: }
421:
422: public XSUnionSimpleType asUnion() {
423: return null;
424: }
425:
426: public XSSimpleType getType() {
427: return this ;
428: } // Ref.SimpleType implementation
429:
430: public XSSimpleType getRedefinedBy() {
431: return null;
432: }
433:
434: public int getRedefinedCount() {
435: return 0;
436: }
437:
438: public XSType[] listSubstitutables() {
439: return Util.listSubstitutables(this );
440: }
441: }
442:
443: public XSComplexType getAnyType() {
444: return anyType;
445: }
446:
447: public final AnyType anyType = new AnyType();
448:
449: private class AnyType extends DeclarationImpl implements
450: XSComplexType, Ref.Type {
451: AnyType() {
452: super (null, null, null, null,
453: "http://www.w3.org/2001/XMLSchema", "anyType",
454: false);
455: }
456:
457: public SchemaImpl getOwnerSchema() {
458: return createSchema("http://www.w3.org/2001/XMLSchema",
459: null);
460: }
461:
462: public boolean isAbstract() {
463: return false;
464: }
465:
466: public XSWildcard getAttributeWildcard() {
467: return anyWildcard;
468: }
469:
470: public XSAttributeUse getAttributeUse(String nsURI,
471: String localName) {
472: return null;
473: }
474:
475: public Iterator<XSAttributeUse> iterateAttributeUses() {
476: return Iterators.empty();
477: }
478:
479: public XSAttributeUse getDeclaredAttributeUse(String nsURI,
480: String localName) {
481: return null;
482: }
483:
484: public Iterator<XSAttributeUse> iterateDeclaredAttributeUses() {
485: return Iterators.empty();
486: }
487:
488: public Iterator<XSAttGroupDecl> iterateAttGroups() {
489: return Iterators.empty();
490: }
491:
492: public Collection<XSAttributeUse> getAttributeUses() {
493: return Collections.EMPTY_LIST;
494: }
495:
496: public Collection<? extends XSAttributeUse> getDeclaredAttributeUses() {
497: return Collections.EMPTY_LIST;
498: }
499:
500: public Collection<? extends XSAttGroupDecl> getAttGroups() {
501: return Collections.EMPTY_LIST;
502: }
503:
504: public boolean isFinal(int i) {
505: return false;
506: }
507:
508: public boolean isSubstitutionProhibited(int i) {
509: return false;
510: }
511:
512: public boolean isMixed() {
513: return true;
514: }
515:
516: public XSContentType getContentType() {
517: return contentType;
518: }
519:
520: public XSContentType getExplicitContent() {
521: return null;
522: }
523:
524: public XSType getBaseType() {
525: return this ;
526: }
527:
528: public XSSimpleType asSimpleType() {
529: return null;
530: }
531:
532: public XSComplexType asComplexType() {
533: return this ;
534: }
535:
536: public boolean isDerivedFrom(XSType t) {
537: return t == this ;
538: }
539:
540: public boolean isSimpleType() {
541: return false;
542: }
543:
544: public boolean isComplexType() {
545: return true;
546: }
547:
548: public XSContentType asEmpty() {
549: return null;
550: }
551:
552: public int getDerivationMethod() {
553: return XSType.RESTRICTION;
554: }
555:
556: public XSElementDecl getScope() {
557: return null;
558: }
559:
560: public void visit(XSVisitor visitor) {
561: visitor.complexType(this );
562: }
563:
564: public <T> T apply(XSFunction<T> f) {
565: return f.complexType(this );
566: }
567:
568: public XSType getType() {
569: return this ;
570: } // Ref.Type implementation
571:
572: public XSComplexType getRedefinedBy() {
573: return null;
574: }
575:
576: public int getRedefinedCount() {
577: return 0;
578: }
579:
580: public XSType[] listSubstitutables() {
581: return Util.listSubstitutables(this );
582: }
583:
584: private final WildcardImpl anyWildcard = new WildcardImpl.Any(
585: null, null, null, null, XSWildcard.SKIP);
586: private final XSContentType contentType = new ParticleImpl(
587: null, null, new ModelGroupImpl(null, null, null, null,
588: XSModelGroup.SEQUENCE,
589: new ParticleImpl[] { new ParticleImpl(null,
590: null, anyWildcard, null,
591: XSParticle.UNBOUNDED, 0) }), null, 1, 1);
592: }
593: }
|