KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > xml > XsNamespaceList


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.xs.xml;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.Comparator JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25
26 /** <p>Implementation of the <code>xs:namespaceList</code> type,
27  * specified like this:
28  * <pre>
29  * &lt;xs:simpleType name="namespaceList"&gt;
30  * &lt;xs:annotation&gt;
31  * &lt;xs:documentation&gt;
32  * A utility type, not for public use
33  * &lt;/xs:documentation&gt;
34  * &lt;/xs:annotation&gt;
35  * &lt;xs:union&gt;
36  * &lt;xs:simpleType&gt;
37  * &lt;xs:restriction base="xs:token"&gt;
38  * &lt;xs:enumeration value="##any"/&gt;
39  * &lt;xs:enumeration value="##other"/&gt;
40  * &lt;/xs:restriction&gt;
41  * &lt;/xs:simpleType&gt;
42  * &lt;xs:simpleType&gt;
43  * &lt;xs:list&gt;
44  * &lt;xs:simpleType&gt;
45  * &lt;xs:union memberTypes="xs:anyURI"&gt;
46  * &lt;xs:simpleType&gt;
47  * &lt;xs:restriction base="xs:token"&gt;
48  * &lt;xs:enumeration value="##targetNamespace"/&gt;
49  * &lt;xs:enumeration value="##local"/&gt;
50  * &lt;/xs:restriction&gt;
51  * &lt;/xs:simpleType&gt;
52  * &lt;/xs:union&gt;
53  * &lt;/xs:simpleType&gt;
54  * &lt;/xs:list&gt;
55  * &lt;/xs:simpleType&gt;
56  * &lt;/xs:union&gt;
57  * &lt;/xs:simpleType&gt;
58  * </pre></p>
59  *
60  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
61  */

62 public abstract class XsNamespaceList {
63     /** <p>The namespace list matching "##any".</p>
64      */

65     public static final XsNamespaceList ANY = new XsNamespaceList(){
66         /** @return true
67          */

68         public boolean isAny() { return true; }
69         /** @return false
70          */

71         public boolean isOther() { return false; }
72         /** @return null
73          */

74         public XsAnyURI[] getUris() { return null; }
75         /** @return The string "##any"
76          */

77         public String JavaDoc toString() { return "##any"; }
78     };
79
80     /** <p>A namespace list matching "##other" with the given
81      * target namespace <code>pTargetNamespace</code>.</p>
82      */

83     public static class Other extends XsNamespaceList {
84         private final XsAnyURI[] uris;
85         protected Other(XsAnyURI pTargetNamespace) {
86             if (pTargetNamespace == null) {
87                 pTargetNamespace = new XsAnyURI("");
88             }
89             uris = new XsAnyURI[]{pTargetNamespace};
90         }
91         /** @return false
92          */

93         public boolean isAny() { return false; }
94         /** @return true
95          */

96         public boolean isOther() { return true; }
97         /** @return An array with a single element (the target namespace or "")
98          */

99         public XsAnyURI[] getUris() { return uris; }
100         /** @return The string "##other".
101          */

102         public String JavaDoc toString() { return "##other"; }
103         /** <p>Returns getUris()[0].hashCode().</p>
104          */

105         public int hashCode() { return getUris()[0].hashCode(); }
106         /** <p>Returns, whether this is an instance of
107          * {@link Other} with the same target namespace.</p>
108          */

109         public boolean equals(Object JavaDoc pOther) {
110             return pOther != null
111                 && (pOther instanceof Other)
112                 && getUris()[0].equals(((Other) pOther).getUris()[0]);
113         }
114     }
115
116     public static class Basic extends XsNamespaceList {
117         private final XsAnyURI targetNamespace;
118         private final XsAnyURI[] uris;
119         private final String JavaDoc toStr;
120         protected Basic(String JavaDoc pValue, XsAnyURI pTargetNamespace) {
121             targetNamespace = pTargetNamespace;
122             toStr = pValue;
123             Set JavaDoc set = new HashSet JavaDoc();
124             if (pTargetNamespace == null) {
125                 pTargetNamespace = new XsAnyURI("");
126             }
127             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pValue); st.hasMoreTokens(); ) {
128                 String JavaDoc s = st.nextToken();
129                 if ("##targetNamespace".equals(s)) {
130                     set.add(pTargetNamespace);
131                 } else if ("##local".equals(pTargetNamespace)) {
132                     set.add(new XsAnyURI(""));
133                 } else {
134                     set.add(new XsAnyURI(s));
135                 }
136             }
137             uris = (XsAnyURI[]) set.toArray(new XsAnyURI[set.size()]);
138         }
139         /** @return false
140          */

141         public boolean isAny() { return false; }
142         /** @return false
143          */

144         public boolean isOther() { return false; }
145         /** @return An array with the URI's specified in the
146          * 'namespace' attribute.
147          */

148         public XsAnyURI[] getUris() { return uris; }
149         /** @return The unmodified value of the 'namespace' attribute.
150          */

151         public String JavaDoc toString() { return toStr; }
152         /** @return An hash code suitable for applying
153          * {@link Arrays#equals(Object[], Object[])} on
154          * the result of {@link #getUris()}.</p>
155          */

156         public int hashCode() {
157             XsAnyURI[] uris = getUris();
158             int hash = uris.length;
159             for (int i = 0; i < uris.length; i++) {
160                 hash += uris[i].hashCode();
161             }
162             return hash;
163         }
164         /** <p>Implemented with {@link Arrays#equals(Object[], Object[])}
165          * and the result of {@link #getUris()}.</p>
166          */

167         public boolean equals(Object JavaDoc pOther) {
168             return pOther != null
169                 && pOther instanceof Basic
170                 && Arrays.equals(getUris(), ((Basic) pOther).getUris());
171         }
172     }
173
174     /** <p>Returns a namespace list, matching the 'namespace' attribute
175      * given by <code>pValue</code>. The given target namespace is used,
176      * if required.</p>
177      */

178     public static XsNamespaceList valueOf(String JavaDoc pValue, XsAnyURI pTargetNamespace) {
179         if ("##any".equals(pValue)) {
180             return ANY;
181         } else if ("##other".equals(pValue)) {
182             return new Other(pTargetNamespace);
183         } else {
184             return new Basic(pValue, pTargetNamespace);
185         }
186     }
187
188   /** <p>Returns whether the namespace list matches <code>##any</code>.
189    * If this is the case, then {@link #isOther()} returns false
190    * and {@link #getUris()} returns null.</p>
191    */

192   public abstract boolean isAny();
193
194   /** <p>Returns whether the namespace list matches <code>##other</code>.
195    * If the result is true, then {@link #getUris()} may be used to
196    * obtain an array with a single element, the target namespace.</p>
197    */

198   public abstract boolean isOther();
199
200   /** <p>Returns the array of URI's specified in the namespace list.
201    * If {@link #isAny()} returns true, then the result is null.
202    * If {@link #isOther()} returns true, then the result is an
203    * array with a single element: The target namespace or "" for
204    * an absent namespace.</p>
205    */

206   public abstract XsAnyURI[] getUris();
207 }
208
Popular Tags