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