Source code: com/sun/xacml/attr/BooleanAttribute.java
1
2 /*
3 * @(#)BooleanAttribute.java
4 *
5 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistribution of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistribution in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18 * be used to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * This software is provided "AS IS," without a warranty of any kind. ALL
22 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
25 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use in
34 * the design, construction, operation or maintenance of any nuclear facility.
35 */
36
37 package com.sun.xacml.attr;
38
39 import com.sun.xacml.ParsingException;
40
41 import java.net.URI;
42
43 import org.w3c.dom.Node;
44
45
46 /**
47 * Representation of an xs:boolean value. This class supports parsing
48 * xs:boolean values. All objects of this class are immutable and
49 * all methods of the class are thread-safe.
50 *
51 * @since 1.0
52 * @author Marco Barreno
53 * @author Steve Hanna
54 */
55 public class BooleanAttribute extends AttributeValue
56 {
57
58 /**
59 * Official name of this type
60 */
61 public static final String identifier =
62 "http://www.w3.org/2001/XMLSchema#boolean";
63
64 /**
65 * URI version of name for this type
66 * <p>
67 * This field is initialized by a static initializer so that
68 * we can catch any exceptions thrown by URI(String) and
69 * transform them into a RuntimeException, since this should
70 * never happen but should be reported properly if it ever does.
71 */
72 private static URI identifierURI;
73
74 /**
75 * RuntimeException that wraps an Exception thrown during the
76 * creation of identifierURI, null if none.
77 */
78 private static RuntimeException earlyException;
79
80 /**
81 * Single instance of BooleanAttribute that represents true.
82 * Initialized by the static initializer below.
83 */
84 private static BooleanAttribute trueInstance;
85
86 /**
87 * Single instance of BooleanAttribute that represents false.
88 * Initialized by the static initializer below.
89 */
90 private static BooleanAttribute falseInstance;
91
92 /**
93 * Static initializer that initializes many static fields.
94 * <p>
95 * It is possible identifierURI
96 * class field so that we can catch any exceptions thrown
97 * by URI(String) and transform them into a RuntimeException.
98 * Such exceptions should never happen but should be reported
99 * properly if they ever do.
100 */
101 static {
102 try {
103 identifierURI = new URI(identifier);
104 trueInstance = new BooleanAttribute(true);
105 falseInstance = new BooleanAttribute(false);
106 } catch (Exception e) {
107 earlyException = new IllegalArgumentException();
108 earlyException.initCause(e);
109 }
110 };
111
112 /**
113 * The actual boolean value that this object represents.
114 */
115 private boolean value;
116
117 /**
118 * Creates a new <code>BooleanAttribute</code> that represents
119 * the boolean value supplied.
120 * <p>
121 * This constructor is private because it should not be used by
122 * anyone other than the static initializer in this class.
123 * Instead, please use one of the getInstance methods, which
124 * will ensure that only two BooleanAttribute objects are created,
125 * thus avoiding excess object creation.
126 */
127 private BooleanAttribute(boolean value) {
128 super(identifierURI);
129
130 this.value = value;
131 }
132
133 /**
134 * Returns a <code>BooleanAttribute</code> that represents
135 * the xs:boolean at a particular DOM node.
136 *
137 * @param root the <code>Node</code> that contains the desired value
138 * @return a <code>BooleanAttribute</code> representing the
139 * appropriate value (null if there is a parsing error)
140 */
141 public static BooleanAttribute getInstance(Node root)
142 throws ParsingException
143 {
144 return getInstance(root.getFirstChild().getNodeValue());
145 }
146
147 /**
148 * Returns a <code>BooleanAttribute</code> that represents
149 * the xs:boolean value indicated by the string provided.
150 *
151 * @param value a string representing the desired value
152 * @return a <code>BooleanAttribute</code> representing the
153 * appropriate value (null if there is a parsing error)
154 */
155 public static BooleanAttribute getInstance(String value)
156 throws ParsingException
157 {
158 // Shouldn't happen, but just in case...
159 if (earlyException != null)
160 throw earlyException;
161
162 if (value.equals("true"))
163 return trueInstance;
164 if (value.equals("false"))
165 return falseInstance;
166
167 throw new ParsingException("Boolean string must be true or false");
168 }
169
170 /**
171 * Returns a <code>BooleanAttribute</code> that represents
172 * the boolean value provided.
173 *
174 * @param value a boolean representing the desired value
175 * @return a <code>BooleanAttribute</code> representing the
176 * appropriate value
177 */
178 public static BooleanAttribute getInstance(boolean value) {
179
180 // Shouldn't happen, but just in case...
181 if (earlyException != null)
182 throw earlyException;
183
184 if (value)
185 return trueInstance;
186 else
187 return falseInstance;
188 }
189
190 /**
191 * Returns a <code>BooleanAttribute</code> that represents
192 * a true value.
193 *
194 * @return a <code>BooleanAttribute</code> representing a
195 * true value
196 */
197 public static BooleanAttribute getTrueInstance() {
198
199 // Shouldn't happen, but just in case...
200 if (earlyException != null)
201 throw earlyException;
202
203 return trueInstance;
204 }
205
206 /**
207 * Returns a <code>BooleanAttribute</code> that represents
208 * a false value.
209 *
210 * @return a <code>BooleanAttribute</code> representing a
211 * false value
212 */
213 public static BooleanAttribute getFalseInstance() {
214
215 // Shouldn't happen, but just in case...
216 if (earlyException != null)
217 throw earlyException;
218
219 return falseInstance;
220 }
221
222 /**
223 * Returns the <code>boolean</code> value represented by this object.
224 *
225 * @return the <code>boolean</code> value
226 */
227 public boolean getValue() {
228 return value;
229 }
230
231 /**
232 * Returns true if the input is an instance of this class and if its
233 * value equals the value contained in this class.
234 *
235 * @param o the object to compare
236 *
237 * @return true if this object and the input represent the same value
238 */
239 public boolean equals(Object o) {
240 if (! (o instanceof BooleanAttribute))
241 return false;
242
243 BooleanAttribute other = (BooleanAttribute)o;
244
245 return (value == other.value);
246 }
247
248 /**
249 * Returns the hashcode value used to index and compare this object with
250 * others of the same type. Typically this is the hashcode of the backing
251 * data object.
252 *
253 * @return the object's hashcode value
254 */
255 public int hashCode() {
256 // these numbers come from the javadoc for java.lang.Boolean...no,
257 // really, they do. I can't imagine what they were thinking...
258 return (value ? 1231 : 1237);
259 }
260
261 /**
262 *
263 */
264 public String encode() {
265 return (value ? "true" : "false");
266 }
267
268 }