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.XSElementDecl;
023: import com.sun.xml.xsom.XSModelGroup;
024: import com.sun.xml.xsom.XSModelGroupDecl;
025: import com.sun.xml.xsom.XSTerm;
026: import com.sun.xml.xsom.XSWildcard;
027: import com.sun.xml.xsom.impl.parser.SchemaDocumentImpl;
028: import com.sun.xml.xsom.visitor.XSFunction;
029: import com.sun.xml.xsom.visitor.XSTermFunction;
030: import com.sun.xml.xsom.visitor.XSTermFunctionWithParam;
031: import com.sun.xml.xsom.visitor.XSTermVisitor;
032: import com.sun.xml.xsom.visitor.XSVisitor;
033: import com.sun.xml.xsom.visitor.XSWildcardFunction;
034: import com.sun.xml.xsom.visitor.XSWildcardVisitor;
035: import org.xml.sax.Locator;
036:
037: import java.util.Collection;
038: import java.util.Collections;
039: import java.util.HashSet;
040: import java.util.Iterator;
041: import java.util.Set;
042:
043: public abstract class WildcardImpl extends ComponentImpl implements
044: XSWildcard, Ref.Term {
045: protected WildcardImpl(SchemaDocumentImpl owner,
046: AnnotationImpl _annon, Locator _loc,
047: ForeignAttributesImpl _fa, int _mode) {
048: super (owner, _annon, _loc, _fa);
049: this .mode = _mode;
050: }
051:
052: private final int mode;
053:
054: public int getMode() {
055: return mode;
056: }
057:
058: // compute the union
059: public WildcardImpl union(SchemaDocumentImpl owner, WildcardImpl rhs) {
060: if (this instanceof Any || rhs instanceof Any)
061: return new Any(owner, null, null, null, mode);
062:
063: if (this instanceof Finite && rhs instanceof Finite) {
064: Set<String> values = new HashSet<String>();
065: values.addAll(((Finite) this ).names);
066: values.addAll(((Finite) rhs).names);
067: return new Finite(owner, null, null, null, values, mode);
068: }
069:
070: if (this instanceof Other && rhs instanceof Other) {
071: if (((Other) this ).otherNamespace
072: .equals(((Other) rhs).otherNamespace))
073: return new Other(owner, null, null, null,
074: ((Other) this ).otherNamespace, mode);
075: else
076: // this somewhat strange rule is indeed in the spec
077: return new Other(owner, null, null, null, "", mode);
078: }
079:
080: Other o;
081: Finite f;
082:
083: if (this instanceof Other) {
084: o = (Other) this ;
085: f = (Finite) rhs;
086: } else {
087: o = (Other) rhs;
088: f = (Finite) this ;
089: }
090:
091: if (f.names.contains(o.otherNamespace))
092: return new Any(owner, null, null, null, mode);
093: else
094: return new Other(owner, null, null, null, o.otherNamespace,
095: mode);
096: }
097:
098: public final static class Any extends WildcardImpl implements
099: XSWildcard.Any {
100: public Any(SchemaDocumentImpl owner, AnnotationImpl _annon,
101: Locator _loc, ForeignAttributesImpl _fa, int _mode) {
102: super (owner, _annon, _loc, _fa, _mode);
103: }
104:
105: public boolean acceptsNamespace(String namespaceURI) {
106: return true;
107: }
108:
109: public void visit(XSWildcardVisitor visitor) {
110: visitor.any(this );
111: }
112:
113: public Object apply(XSWildcardFunction function) {
114: return function.any(this );
115: }
116: }
117:
118: public final static class Other extends WildcardImpl implements
119: XSWildcard.Other {
120: public Other(SchemaDocumentImpl owner, AnnotationImpl _annon,
121: Locator _loc, ForeignAttributesImpl _fa,
122: String otherNamespace, int _mode) {
123: super (owner, _annon, _loc, _fa, _mode);
124: this .otherNamespace = otherNamespace;
125: }
126:
127: private final String otherNamespace;
128:
129: public String getOtherNamespace() {
130: return otherNamespace;
131: }
132:
133: public boolean acceptsNamespace(String namespaceURI) {
134: return !namespaceURI.equals(otherNamespace);
135: }
136:
137: public void visit(XSWildcardVisitor visitor) {
138: visitor.other(this );
139: }
140:
141: public Object apply(XSWildcardFunction function) {
142: return function.other(this );
143: }
144: }
145:
146: public final static class Finite extends WildcardImpl implements
147: XSWildcard.Union {
148: public Finite(SchemaDocumentImpl owner, AnnotationImpl _annon,
149: Locator _loc, ForeignAttributesImpl _fa,
150: Set<String> ns, int _mode) {
151: super (owner, _annon, _loc, _fa, _mode);
152: names = ns;
153: namesView = Collections.unmodifiableSet(names);
154: }
155:
156: private final Set<String> names;
157: private final Set<String> namesView;
158:
159: public Iterator<String> iterateNamespaces() {
160: return names.iterator();
161: }
162:
163: public Collection<String> getNamespaces() {
164: return namesView;
165: }
166:
167: public boolean acceptsNamespace(String namespaceURI) {
168: return names.contains(namespaceURI);
169: }
170:
171: public void visit(XSWildcardVisitor visitor) {
172: visitor.union(this );
173: }
174:
175: public Object apply(XSWildcardFunction function) {
176: return function.union(this );
177: }
178: }
179:
180: public final void visit(XSVisitor visitor) {
181: visitor.wildcard(this );
182: }
183:
184: public final void visit(XSTermVisitor visitor) {
185: visitor.wildcard(this );
186: }
187:
188: public Object apply(XSTermFunction function) {
189: return function.wildcard(this );
190: }
191:
192: public <T, P> T apply(XSTermFunctionWithParam<T, P> function,
193: P param) {
194: return function.wildcard(this , param);
195: }
196:
197: public Object apply(XSFunction function) {
198: return function.wildcard(this );
199: }
200:
201: public boolean isWildcard() {
202: return true;
203: }
204:
205: public boolean isModelGroupDecl() {
206: return false;
207: }
208:
209: public boolean isModelGroup() {
210: return false;
211: }
212:
213: public boolean isElementDecl() {
214: return false;
215: }
216:
217: public XSWildcard asWildcard() {
218: return this ;
219: }
220:
221: public XSModelGroupDecl asModelGroupDecl() {
222: return null;
223: }
224:
225: public XSModelGroup asModelGroup() {
226: return null;
227: }
228:
229: public XSElementDecl asElementDecl() {
230: return null;
231: }
232:
233: // Ref.Term implementation
234: public XSTerm getTerm() {
235: return this;
236: }
237: }
|