KickJava   Java API By Example, From Geeks To Geeks.

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


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="blockSet"&gt;
25  * &lt;xs:annotation&gt;
26  * &lt;xs:documentation&gt;
27  * A utility type, not for public use
28  * &lt;/xs:documentation&gt;
29  * &lt;xs:documentation&gt;
30  * #all or (possibly empty) subset of {substitution, extension,
31  * restriction}
32  * &lt;/xs:documentation&gt;
33  * &lt;/xs:annotation&gt;
34  * &lt;xs:union&gt;
35  * &lt;xs:simpleType&gt;
36  * &lt;xs:restriction base="xs:token"&gt;
37  * &lt;xs:enumeration value="#all"/&gt;
38  * &lt;/xs:restriction&gt;
39  * &lt;/xs:simpleType&gt;
40  * &lt;xs:simpleType&gt;
41  * &lt;xs:list&gt;
42  * &lt;xs:simpleType&gt;
43  * &lt;xs:restriction base="xs:derivationControl"&gt;
44  * &lt;xs:enumeration value="extension"/&gt;
45  * &lt;xs:enumeration value="restriction"/&gt;
46  * &lt;xs:enumeration value="substitution"/&gt;
47  * &lt;/xs:restriction&gt;
48  * &lt;/xs:simpleType&gt;
49  * &lt;/xs:list&gt;
50  * &lt;/xs:simpleType&gt;
51  * &lt;/xs:union&gt;
52  * &lt;/xs:simpleType&gt;
53  * </pre>
54  * </p>
55  *
56  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
57  */

58 public class XsBlockSet {
59   boolean extensionAllowed, restrictionAllowed, substitutionAllowed;
60
61   /** <p>Returns whether extension is allowed.</p>
62    */

63   public boolean isExtensionAllowed() {
64     return extensionAllowed;
65   }
66
67   /** <p>Sets whether extension is allowed.</p>
68    */

69   public void setExtensionAllowed(boolean pExtensionAllowed) {
70     extensionAllowed = pExtensionAllowed;
71   }
72
73   /** <p>Returns whether restriction is allowed.</p>
74    */

75   public boolean isRestrictionAllowed() {
76     return restrictionAllowed;
77   }
78
79   /** <p>Sets whether restriction is allowed.</p>
80    */

81   public void setRestrictionAllowed(boolean pRestrictionAllowed) {
82     restrictionAllowed = pRestrictionAllowed;
83   }
84
85   /** <p>Returns whether restriction is allowed.</p>
86    */

87   public boolean isSubstitutionAllowed() {
88     return substitutionAllowed;
89   }
90
91   /** <p>Sets whether restriction is allowed.</p>
92    */

93   public void setSubstitutionAllowed(boolean pSubstitutionAllowed) {
94     substitutionAllowed = pSubstitutionAllowed;
95   }
96
97   /** <p>Returns a <code>DerivationSet</code> matching the given
98    * value.</p>
99    */

100   public static XsBlockSet valueOf(String JavaDoc pValue) {
101     return new XsBlockSet(pValue);
102   }
103
104   /** <p>Creates a new DerivationSet with the given value.</p>
105    */

106   public XsBlockSet(String JavaDoc pValue) {
107     if ("#all".equals(pValue)) {
108       setExtensionAllowed(true);
109       setRestrictionAllowed(true);
110       setSubstitutionAllowed(true);
111     } else {
112       for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pValue, " "); st.hasMoreTokens(); ) {
113         String JavaDoc s = st.nextToken();
114         if ("extension".equals(s)) {
115           setExtensionAllowed(true);
116         } else if ("restriction".equals(s)) {
117           setRestrictionAllowed(true);
118         } else if ("substitution".equals(s)) {
119           setSubstitutionAllowed(true);
120         } else {
121           throw new IllegalArgumentException JavaDoc("Invalid block set value: " + pValue + "; the token " + s + " did not resolve to either of 'extension', 'restriction', or 'substitution'.");
122         }
123       }
124     }
125   }
126
127   public String JavaDoc toString() {
128     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
129     if (extensionAllowed) {
130       sb.append("extension");
131     }
132     if (restrictionAllowed) {
133       if (sb.length() > 0) {
134         sb.append(" ");
135       }
136       sb.append("restriction");
137     }
138     if (substitutionAllowed) {
139       if (sb.length() > 0) {
140         sb.append(" ");
141       }
142       sb.append("substitution");
143     }
144     return sb.toString();
145   }
146
147   public boolean equals(Object JavaDoc o) {
148     if (o == null || !(XsBlockSet.class.equals(o.getClass()))) {
149       return false;
150     }
151     XsBlockSet bs = (XsBlockSet) o;
152     return bs.extensionAllowed == extensionAllowed &&
153             bs.restrictionAllowed == restrictionAllowed &&
154             bs.substitutionAllowed == substitutionAllowed;
155   }
156
157   public int hashCode() {
158     int result = 0;
159     if (extensionAllowed) { result += 1; }
160     if (restrictionAllowed) { result += 2; }
161     if (substitutionAllowed) { result += 4; }
162     return result;
163   }
164 }
165
Popular Tags