|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.xsom.impl; |
|
27 |
|
28 import com.sun.xml.internal.xsom.XSElementDecl; |
|
29 import com.sun.xml.internal.xsom.XSModelGroup; |
|
30 import com.sun.xml.internal.xsom.XSModelGroupDecl; |
|
31 import com.sun.xml.internal.xsom.XSTerm; |
|
32 import com.sun.xml.internal.xsom.XSWildcard; |
|
33 import com.sun.xml.internal.xsom.impl.parser.SchemaDocumentImpl; |
|
34 import com.sun.xml.internal.xsom.visitor.XSFunction; |
|
35 import com.sun.xml.internal.xsom.visitor.XSTermFunction; |
|
36 import com.sun.xml.internal.xsom.visitor.XSTermFunctionWithParam; |
|
37 import com.sun.xml.internal.xsom.visitor.XSTermVisitor; |
|
38 import com.sun.xml.internal.xsom.visitor.XSVisitor; |
|
39 import com.sun.xml.internal.xsom.visitor.XSWildcardFunction; |
|
40 import com.sun.xml.internal.xsom.visitor.XSWildcardVisitor; |
|
41 import org.xml.sax.Locator; |
|
42 |
|
43 import java.util.Collection; |
|
44 import java.util.Collections; |
|
45 import java.util.HashSet; |
|
46 import java.util.Iterator; |
|
47 import java.util.Set; |
|
48 |
|
49 public abstract class WildcardImpl extends ComponentImpl implements XSWildcard, Ref.Term |
|
50 { |
|
51 protected WildcardImpl( SchemaDocumentImpl owner, AnnotationImpl _annon, Locator _loc, ForeignAttributesImpl _fa, int _mode ) { |
|
52 super(owner,_annon,_loc,_fa); |
|
53 this.mode = _mode; |
|
54 } |
|
55 |
|
56 private final int mode; |
|
57 public int getMode() { return mode; } |
|
58 |
|
59 // compute the union |
|
60 public WildcardImpl union( SchemaDocumentImpl owner, WildcardImpl rhs ) { |
|
61 if(this instanceof Any || rhs instanceof Any) |
|
62 return new Any(owner,null,null,null,mode); |
|
63 |
|
64 if(this instanceof Finite && rhs instanceof Finite) { |
|
65 Set<String> values = new HashSet<String>(); |
|
66 values.addAll( ((Finite)this).names ); |
|
67 values.addAll( ((Finite)rhs ).names ); |
|
68 return new Finite(owner,null,null,null,values,mode); |
|
69 } |
|
70 |
|
71 if(this instanceof Other && rhs instanceof Other) { |
|
72 if( ((Other)this).otherNamespace.equals( |
|
73 ((Other)rhs).otherNamespace) ) |
|
74 return new Other(owner,null,null,null, ((Other)this).otherNamespace, mode ); |
|
75 else |
|
76 // this somewhat strange rule is indeed in the spec |
|
77 return new Other(owner,null,null,null, "", mode ); |
|
78 } |
|
79 |
|
80 Other o; |
|
81 Finite f; |
|
82 |
|
83 if( this instanceof Other ) { |
|
84 o=(Other)this; f=(Finite)rhs; |
|
85 } else { |
|
86 o=(Other)rhs; f=(Finite)this; |
|
87 } |
|
88 |
|
89 if(f.names.contains(o.otherNamespace)) |
|
90 return new Any(owner,null,null,null,mode); |
|
91 else |
|
92 return new Other(owner,null,null,null,o.otherNamespace,mode); |
|
93 } |
|
94 |
|
95 |
|
96 |
|
97 public final static class Any extends WildcardImpl implements XSWildcard.Any { |
|
98 public Any( SchemaDocumentImpl owner, AnnotationImpl _annon, Locator _loc, ForeignAttributesImpl _fa, int _mode ) { |
|
99 super(owner,_annon,_loc,_fa,_mode); |
|
100 } |
|
101 |
|
102 public boolean acceptsNamespace( String namespaceURI ) { |
|
103 return true; |
|
104 } |
|
105 public void visit( XSWildcardVisitor visitor ) { |
|
106 visitor.any(this); |
|
107 } |
|
108 public Object apply( XSWildcardFunction function ) { |
|
109 return function.any(this); |
|
110 } |
|
111 } |
|
112 |
|
113 public final static class Other extends WildcardImpl implements XSWildcard.Other { |
|
114 public Other( SchemaDocumentImpl owner, AnnotationImpl _annon, Locator _loc, ForeignAttributesImpl _fa, |
|
115 String otherNamespace, int _mode ) { |
|
116 super(owner,_annon,_loc,_fa,_mode); |
|
117 this.otherNamespace = otherNamespace; |
|
118 } |
|
119 |
|
120 private final String otherNamespace; |
|
121 |
|
122 public String getOtherNamespace() { return otherNamespace; } |
|
123 |
|
124 public boolean acceptsNamespace( String namespaceURI ) { |
|
125 return !namespaceURI.equals(otherNamespace); |
|
126 } |
|
127 |
|
128 public void visit( XSWildcardVisitor visitor ) { |
|
129 visitor.other(this); |
|
130 } |
|
131 public Object apply( XSWildcardFunction function ) { |
|
132 return function.other(this); |
|
133 } |
|
134 } |
|
135 |
|
136 public final static class Finite extends WildcardImpl implements XSWildcard.Union { |
|
137 public Finite( SchemaDocumentImpl owner, AnnotationImpl _annon, Locator _loc, ForeignAttributesImpl _fa, |
|
138 Set<String> ns, int _mode ) { |
|
139 super(owner,_annon,_loc,_fa,_mode); |
|
140 names = ns; |
|
141 namesView = Collections.unmodifiableSet(names); |
|
142 } |
|
143 |
|
144 private final Set<String> names; |
|
145 private final Set<String> namesView; |
|
146 |
|
147 public Iterator<String> iterateNamespaces() { |
|
148 return names.iterator(); |
|
149 } |
|
150 |
|
151 public Collection<String> getNamespaces() { |
|
152 return namesView; |
|
153 } |
|
154 |
|
155 public boolean acceptsNamespace( String namespaceURI ) { |
|
156 return names.contains(namespaceURI); |
|
157 } |
|
158 |
|
159 public void visit( XSWildcardVisitor visitor ) { |
|
160 visitor.union(this); |
|
161 } |
|
162 public Object apply( XSWildcardFunction function ) { |
|
163 return function.union(this); |
|
164 } |
|
165 } |
|
166 |
|
167 public final void visit( XSVisitor visitor ) { |
|
168 visitor.wildcard(this); |
|
169 } |
|
170 public final void visit( XSTermVisitor visitor ) { |
|
171 visitor.wildcard(this); |
|
172 } |
|
173 public Object apply( XSTermFunction function ) { |
|
174 return function.wildcard(this); |
|
175 } |
|
176 |
|
177 public <T,P> T apply(XSTermFunctionWithParam<T, P> function, P param) { |
|
178 return function.wildcard(this,param); |
|
179 } |
|
180 |
|
181 public Object apply( XSFunction function ) { |
|
182 return function.wildcard(this); |
|
183 } |
|
184 |
|
185 |
|
186 public boolean isWildcard() { return true; } |
|
187 public boolean isModelGroupDecl() { return false; } |
|
188 public boolean isModelGroup() { return false; } |
|
189 public boolean isElementDecl() { return false; } |
|
190 |
|
191 public XSWildcard asWildcard() { return this; } |
|
192 public XSModelGroupDecl asModelGroupDecl() { return null; } |
|
193 public XSModelGroup asModelGroup() { return null; } |
|
194 public XSElementDecl asElementDecl() { return null; } |
|
195 |
|
196 |
|
197 // Ref.Term implementation |
|
198 public XSTerm getTerm() { return this; } |
|
199 } |