1 /*
2 * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
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.opensaml.xml.schema;
18
19 import org.opensaml.xml.util.DatatypeHelper;
20
21 /**
22 * A class representing a boolean attribute. This class tracks the usage of the literals {true, false, 1, 0} to ensure
23 * proper roundtripping when unmarshalling/marshalling.
24 */
25 public class XSBooleanValue {
26
27 /** Whether to use the numeric representation of the lexical one. */
28 private boolean numeric;
29
30 /** Value of this boolean. */
31 private Boolean value;
32
33 /**
34 * Constructor. Uses lexical representation and sets value to null.
35 */
36 public XSBooleanValue() {
37 numeric = false;
38 value = null;
39 }
40
41 /**
42 * Constructor.
43 *
44 * @param newValue the value
45 * @param numericRepresentation whether to use a numeric or lexical representation
46 */
47 public XSBooleanValue(Boolean newValue, boolean numericRepresentation) {
48 numeric = numericRepresentation;
49 value = newValue;
50 }
51
52 /**
53 * Gets the boolean value.
54 *
55 * @return the boolean value
56 */
57 public Boolean getValue() {
58 return value;
59 }
60
61 /**
62 * Sets the boolean value.
63 *
64 * @param newValue the boolean value
65 */
66 public void setValue(Boolean newValue) {
67 value = newValue;
68 }
69
70 /**
71 * Gets whether to use the numeric or lexical representation.
72 *
73 * @return whether to use the numeric or lexical representation
74 */
75 public boolean isNumericRepresentation() {
76 return numeric;
77 }
78
79 /**
80 * Sets whether to use the numeric or lexical representation.
81 *
82 * @param numericRepresentation whether to use the numeric or lexical representation
83 */
84 public void setNumericRepresentation(boolean numericRepresentation) {
85 this.numeric = numericRepresentation;
86 }
87
88 /** {@inheritDoc} */
89 public int hashCode(){
90 int hash;
91 if(numeric){
92 if(value == null){
93 hash = 0;
94 }else if(value.booleanValue()){
95 hash = 1;
96 }else {
97 hash = 3;
98 }
99 }else{
100 if(value == null){
101 hash = 4;
102 }else if(value.booleanValue()){
103 hash = 5;
104 }else {
105 hash = 6;
106 }
107 }
108
109 return hash;
110 }
111
112 /** {@inheritDoc} */
113 public boolean equals(Object obj){
114 if(obj == this){
115 return true;
116 }
117
118 if(obj instanceof XSBooleanValue){
119 return hashCode() == obj.hashCode();
120 }
121
122 return false;
123 }
124
125 /** {@inheritDoc} */
126 public String toString() {
127 return toString(value, numeric);
128 }
129
130 /**
131 * Converts a boolean value into a string. If using the numeric representations and the value is true then "1" is
132 * returned or "0" if the value is false. If lexical representation is used "true" and "false" will be returned. If
133 * the given value is null, then "false" is returned.
134 *
135 * @param value the boolean value
136 * @param numericRepresentation whether to use numeric of lexical representation
137 *
138 * @return the textual representation
139 */
140 public static String toString(Boolean value, boolean numericRepresentation) {
141 if (value == null) {
142 return "false";
143 }
144
145 if (numericRepresentation) {
146 if (value.booleanValue()) {
147 return "1";
148 } else {
149 return "0";
150 }
151 } else {
152 return value.toString();
153 }
154 }
155
156 /**
157 * Parses a string meant to represent a boolean. If the string is "1" or "0" the returned object will use a numeric
158 * representation and have a value of TRUE or FALSE, respectively. If the string is "true" the returned object will
159 * use a lexical representation and have a value of TRUE. If the string is anything else the returned object will
160 * use a lexical representation and have a value of FALSE.
161 *
162 * @param booleanString the string to parse
163 *
164 * @return the boolean value
165 */
166 public static XSBooleanValue valueOf(String booleanString) {
167 String trimmedBooleanString = DatatypeHelper.safeTrimOrNullString(booleanString);
168 if (trimmedBooleanString != null) {
169 if (trimmedBooleanString.equals("1")) {
170 return new XSBooleanValue(Boolean.TRUE, true);
171 } else if (trimmedBooleanString.equals("0")) {
172 return new XSBooleanValue(Boolean.FALSE, true);
173 } else if (trimmedBooleanString.equals("true")) {
174 return new XSBooleanValue(Boolean.TRUE, false);
175 }
176 }
177
178 return new XSBooleanValue(Boolean.FALSE, false);
179 }
180 }