001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.xsom.impl;
026:
027: import com.sun.xml.internal.xsom.XSAttGroupDecl;
028: import com.sun.xml.internal.xsom.XSAttributeDecl;
029: import com.sun.xml.internal.xsom.XSAttributeUse;
030: import com.sun.xml.internal.xsom.XSComplexType;
031: import com.sun.xml.internal.xsom.XSContentType;
032: import com.sun.xml.internal.xsom.XSElementDecl;
033: import com.sun.xml.internal.xsom.XSSimpleType;
034: import com.sun.xml.internal.xsom.XSType;
035: import com.sun.xml.internal.xsom.XSWildcard;
036: import com.sun.xml.internal.xsom.impl.parser.DelayedRef;
037: import com.sun.xml.internal.xsom.impl.parser.SchemaDocumentImpl;
038: import com.sun.xml.internal.xsom.impl.util.ConcatIterator;
039: import com.sun.xml.internal.xsom.impl.util.FilterIterator;
040: import com.sun.xml.internal.xsom.visitor.XSFunction;
041: import com.sun.xml.internal.xsom.visitor.XSVisitor;
042: import org.xml.sax.Locator;
043:
044: import java.util.Iterator;
045:
046: public class ComplexTypeImpl extends AttributesHolder implements
047: XSComplexType, Ref.ComplexType {
048: public ComplexTypeImpl(SchemaDocumentImpl _parent,
049: AnnotationImpl _annon, Locator _loc,
050: ForeignAttributesImpl _fa, String _name,
051: boolean _anonymous,
052:
053: boolean _abstract, int _derivationMethod, Ref.Type _base,
054: int _final, int _block, boolean _mixed) {
055:
056: super (_parent, _annon, _loc, _fa, _name, _anonymous);
057:
058: if (_base == null)
059: throw new IllegalArgumentException();
060:
061: this ._abstract = _abstract;
062: this .derivationMethod = _derivationMethod;
063: this .baseType = _base;
064: this .finalValue = _final;
065: this .blockValue = _block;
066: this .mixed = _mixed;
067: }
068:
069: public XSComplexType asComplexType() {
070: return this ;
071: }
072:
073: public boolean isDerivedFrom(XSType t) {
074: XSType x = this ;
075: while (true) {
076: if (t == x)
077: return true;
078: XSType s = x.getBaseType();
079: if (s == x)
080: return false;
081: x = s;
082: }
083: }
084:
085: public XSSimpleType asSimpleType() {
086: return null;
087: }
088:
089: public final boolean isSimpleType() {
090: return false;
091: }
092:
093: public final boolean isComplexType() {
094: return true;
095: }
096:
097: private int derivationMethod;
098:
099: public int getDerivationMethod() {
100: return derivationMethod;
101: }
102:
103: private Ref.Type baseType;
104:
105: public XSType getBaseType() {
106: return baseType.getType();
107: }
108:
109: /**
110: * Called when this complex type redefines the specified complex type.
111: */
112: public void redefine(ComplexTypeImpl ct) {
113: if (baseType instanceof DelayedRef)
114: ((DelayedRef) baseType).redefine(ct);
115: else
116: this .baseType = ct;
117: ct.redefinedBy = this ;
118: redefiningCount = (short) (ct.redefiningCount + 1);
119: }
120:
121: /**
122: * Number of times this component redefines other components.
123: */
124: private short redefiningCount = 0;
125:
126: private ComplexTypeImpl redefinedBy = null;
127:
128: public XSComplexType getRedefinedBy() {
129: return redefinedBy;
130: }
131:
132: public int getRedefinedCount() {
133: int i = 0;
134: for (ComplexTypeImpl ct = this .redefinedBy; ct != null; ct = ct.redefinedBy)
135: i++;
136: return i;
137: }
138:
139: private XSElementDecl scope;
140:
141: public XSElementDecl getScope() {
142: return scope;
143: }
144:
145: public void setScope(XSElementDecl _scope) {
146: this .scope = _scope;
147: }
148:
149: private final boolean _abstract;
150:
151: public boolean isAbstract() {
152: return _abstract;
153: }
154:
155: private WildcardImpl localAttWildcard;
156:
157: /**
158: * Set the local attribute wildcard.
159: */
160: public void setWildcard(WildcardImpl wc) {
161: this .localAttWildcard = wc;
162: }
163:
164: public XSWildcard getAttributeWildcard() {
165: WildcardImpl complete = localAttWildcard;
166:
167: Iterator itr = iterateAttGroups();
168: while (itr.hasNext()) {
169: WildcardImpl w = (WildcardImpl) ((XSAttGroupDecl) itr
170: .next()).getAttributeWildcard();
171:
172: if (w == null)
173: continue;
174:
175: if (complete == null)
176: complete = w;
177: else
178: // TODO: the spec says it's intersection,
179: // but I think it has to be union.
180: complete = complete.union(ownerDocument, w);
181: }
182:
183: if (getDerivationMethod() == RESTRICTION)
184: return complete;
185:
186: WildcardImpl base = null;
187: XSType baseType = getBaseType();
188: if (baseType.asComplexType() != null)
189: base = (WildcardImpl) baseType.asComplexType()
190: .getAttributeWildcard();
191:
192: if (complete == null)
193: return base;
194: if (base == null)
195: return complete;
196:
197: return complete.union(ownerDocument, base);
198: }
199:
200: private final int finalValue;
201:
202: public boolean isFinal(int derivationMethod) {
203: return (finalValue & derivationMethod) != 0;
204: }
205:
206: private final int blockValue;
207:
208: public boolean isSubstitutionProhibited(int method) {
209: return (blockValue & method) != 0;
210: }
211:
212: private Ref.ContentType contentType;
213:
214: public void setContentType(Ref.ContentType v) {
215: contentType = v;
216: }
217:
218: public XSContentType getContentType() {
219: return contentType.getContentType();
220: }
221:
222: private XSContentType explicitContent;
223:
224: public void setExplicitContent(XSContentType v) {
225: this .explicitContent = v;
226: }
227:
228: public XSContentType getExplicitContent() {
229: return explicitContent;
230: }
231:
232: private final boolean mixed;
233:
234: public boolean isMixed() {
235: return mixed;
236: }
237:
238: public XSAttributeUse getAttributeUse(String nsURI, String localName) {
239: UName name = new UName(nsURI, localName);
240:
241: if (prohibitedAtts.contains(name))
242: return null;
243:
244: XSAttributeUse o = attributes.get(name);
245:
246: if (o == null) {
247: Iterator itr = iterateAttGroups();
248: while (itr.hasNext() && o == null)
249: o = ((XSAttGroupDecl) itr.next()).getAttributeUse(
250: nsURI, localName);
251: }
252:
253: if (o == null) {
254: XSType base = getBaseType();
255: if (base.asComplexType() != null)
256: o = base.asComplexType().getAttributeUse(nsURI,
257: localName);
258: }
259:
260: return o;
261: }
262:
263: public Iterator iterateAttributeUses() {
264:
265: XSComplexType baseType = getBaseType().asComplexType();
266:
267: if (baseType == null)
268: return super .iterateAttributeUses();
269:
270: return new ConcatIterator(new FilterIterator(baseType
271: .iterateAttributeUses()) {
272: protected boolean allows(Object o) {
273: XSAttributeDecl u = ((XSAttributeUse) o).getDecl();
274: UName n = new UName(u.getTargetNamespace(), u.getName());
275: return !prohibitedAtts.contains(n);
276: }
277: }, super .iterateAttributeUses());
278: }
279:
280: public XSType[] listSubstitutables() {
281: return Util.listSubstitutables(this );
282: }
283:
284: public void visit(XSVisitor visitor) {
285: visitor.complexType(this );
286: }
287:
288: public <T> T apply(XSFunction<T> function) {
289: return function.complexType(this );
290: }
291:
292: // Ref.ComplexType implementation
293: public XSComplexType getType() {
294: return this;
295: }
296: }
|