KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringTokenizer JavaDoc;
20
21 /** <p>Implementation of <code>xs:derivationset</code>.
22  * Follows this specification:
23  * <pre>
24  * &lt;xs:simpleType name="simpleDerivationSet"&gt;
25  * &lt;xs:annotation&gt;
26  * &lt;xs:documentation&gt;
27  * #all or (possibly empty) subset of {restriction, union, list}
28  * &lt;/xs:documentation&gt;
29  * &lt;xs:documentation&gt;
30  * A utility type, not for public use
31  * &lt;/xs:documentation&gt;
32  * &lt;/xs:annotation&gt;
33  * &lt;xs:union&gt;
34  * &lt;xs:simpleType&gt;
35  * &lt;xs:restriction base="xs:token"&gt;
36  * &lt;xs:enumeration value="#all"/&gt;
37  * &lt;/xs:restriction&gt;
38  * &lt;/xs:simpleType&gt;
39  * &lt;xs:simpleType&gt;
40  * &lt;xs:restriction base="xs:derivationControl"&gt;
41  * &lt;xs:enumeration value="list"/&gt;
42  * &lt;xs:enumeration value="union"/&gt;
43  * &lt;xs:enumeration value="restriction"/&gt;
44  * &lt;/xs:restriction&gt;
45  * &lt;/xs:simpleType&gt;
46  * &lt;/xs:union&gt;
47  * &lt;/xs:simpleType&gt;
48  * </pre></p>
49  *
50  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
51  */

52 public class XsSimpleDerivationSet {
53   final boolean listAllowed, unionAllowed, restrictionAllowed;
54
55   /** <p>Returns whether derivation of lists is allowed.</p>
56    */

57   public boolean isListAllowed() {
58     return listAllowed;
59   }
60
61   /** <p>Returns whether derivation of unions is allowed.</p>
62    */

63   public boolean isUnionAllowed() {
64     return unionAllowed;
65   }
66
67   /** <p>Returns whether derivation of restrictions is allowed.</p>
68    */

69   public boolean isRestrictionAllowed() {
70     return restrictionAllowed;
71   }
72
73   /** <p>Returns a <code>DerivationSet</code> matching the given
74    * value.</p>
75    */

76   public static XsSimpleDerivationSet valueOf(String JavaDoc pValue) {
77     return new XsSimpleDerivationSet(pValue);
78   }
79
80   /** <p>Creates a new DerivationSet with the given value.</p>
81    */

82   public XsSimpleDerivationSet(String JavaDoc pValue) {
83     if ("#all".equals(pValue)) {
84       listAllowed = true;
85       unionAllowed = true;
86       restrictionAllowed = true;
87     } else {
88       boolean acceptList = false;
89       boolean acceptUnion = false;
90       boolean acceptRestriction = false;
91       for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pValue, " "); st.hasMoreTokens(); ) {
92         String JavaDoc s = st.nextToken();
93         if ("list".equals(s)) {
94           acceptList = true;
95         } else if ("union".equals(s)) {
96           acceptUnion = true;
97         } else if ("restriction".equals(s)) {
98           acceptRestriction = true;
99         } else {
100           throw new IllegalArgumentException JavaDoc("Invalid derivation set value: " + pValue + "; the token " + s + " did not resolve to either of 'extension' or 'restriction'");
101         }
102       }
103       listAllowed = acceptList;
104       unionAllowed = acceptUnion;
105       restrictionAllowed = acceptRestriction;
106     }
107   }
108
109   public String JavaDoc toString() {
110     final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
111     if (listAllowed) {
112       sb.append("list");
113     }
114     if (unionAllowed) {
115       if (sb.length() > 0) {
116         sb.append(" ");
117       }
118       sb.append("union");
119     }
120     if (restrictionAllowed) {
121       if (sb.length() > 0) {
122         sb.append(" ");
123       }
124       sb.append("restriction");
125     }
126     return sb.toString();
127   }
128
129   public boolean equals(Object JavaDoc o) {
130     if (o == null || !(XsSimpleDerivationSet.class.equals(o.getClass()))) {
131       return false;
132     }
133     XsSimpleDerivationSet ds = (XsSimpleDerivationSet) o;
134     return ds.listAllowed == listAllowed &&
135             ds.unionAllowed == unionAllowed &&
136             ds.restrictionAllowed == restrictionAllowed;
137   }
138
139   public int hashCode() {
140     int result = 0;
141     if (listAllowed) { result += 1; }
142     if (unionAllowed) { result += 2; }
143     if (restrictionAllowed) { result += 4; }
144     return result;
145   }
146 }
147
Popular Tags