| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.value; |
| 9 | |
| 10 | import net.sf.saxon.expr.sort.AtomicMatchKey; |
| 11 | import net.sf.saxon.expr.sort.ComparisonException; |
| 12 | import net.sf.saxon.lib.StringCollator; |
| 13 | import net.sf.saxon.om.SequenceTool; |
| 14 | import net.sf.saxon.trans.Err; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.type.AtomicType; |
| 17 | import net.sf.saxon.type.BuiltInAtomicType; |
| 18 | import net.sf.saxon.type.ConversionResult; |
| 19 | import net.sf.saxon.type.ValidationFailure; |
| 20 | |
| 21 | /** |
| 22 | * A boolean XPath value |
| 23 | */ |
| 24 | |
| 25 | public final class BooleanValue extends AtomicValue implements Comparable, AtomicMatchKey { |
| 26 | private boolean value; |
| 27 | |
| 28 | /** |
| 29 | * The boolean value TRUE |
| 30 | */ |
| 31 | public static final BooleanValue TRUE = new BooleanValue(true); |
| 32 | /** |
| 33 | * The boolean value FALSE |
| 34 | */ |
| 35 | public static final BooleanValue FALSE = new BooleanValue(false); |
| 36 | |
| 37 | /** |
| 38 | * Private Constructor: create a boolean value. Only two instances of this class are |
| 39 | * ever created, one to represent true and one to represent false. |
| 40 | * |
| 41 | * @param value the initial value, true or false |
| 42 | */ |
| 43 | |
| 44 | private BooleanValue(boolean value) { |
| 45 | this.value = value; |
| 46 | typeLabel = BuiltInAtomicType.BOOLEAN; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Factory method: get a BooleanValue |
| 51 | * |
| 52 | * @param value true or false, to determine which boolean value is |
| 53 | * required |
| 54 | * @return the BooleanValue requested |
| 55 | */ |
| 56 | |
| 57 | public static BooleanValue get(boolean value) { |
| 58 | return value ? TRUE : FALSE; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Create a new Boolean value with a user-supplied type label. |
| 63 | * It is the caller's responsibility to ensure that the value is valid for the subtype |
| 64 | * |
| 65 | * @param value the boolean value |
| 66 | * @param typeLabel the type label, xs:boolean or a subtype |
| 67 | */ |
| 68 | |
| 69 | public BooleanValue(boolean value, AtomicType typeLabel) { |
| 70 | this.value = value; |
| 71 | this.typeLabel = typeLabel; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Create a copy of this atomic value (usually so that the type label can be changed). |
| 76 | * The type label of the copy will be reset to the primitive type. |
| 77 | * |
| 78 | * @param typeLabel the atomic type label to be added to the copied value |
| 79 | */ |
| 80 | |
| 81 | public AtomicValue copyAsSubType(AtomicType typeLabel) { |
| 82 | BooleanValue v = new BooleanValue(value); |
| 83 | v.typeLabel = typeLabel; |
| 84 | return v; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Convert a string to a boolean value, using the XML Schema rules (including |
| 89 | * whitespace trimming) |
| 90 | * |
| 91 | * @param s the input string |
| 92 | * @return the relevant BooleanValue if validation succeeds; or a ValidationFailure if not. |
| 93 | */ |
| 94 | |
| 95 | public static ConversionResult fromString(CharSequence s) { |
| 96 | // implementation designed to avoid creating new objects |
| 97 | s = Whitespace.trimWhitespace(s); |
| 98 | int len = s.length(); |
| 99 | if (len == 1) { |
| 100 | char c = s.charAt(0); |
| 101 | if (c == '1') { |
| 102 | return TRUE; |
| 103 | } else if (c == '0') { |
| 104 | return FALSE; |
| 105 | } |
| 106 | } else if (len == 4) { |
| 107 | if (s.charAt(0) == 't' && s.charAt(1) == 'r' && s.charAt(2) == 'u' && s.charAt(3) == 'e') { |
| 108 | return TRUE; |
| 109 | } |
| 110 | } else if (len == 5) { |
| 111 | if (s.charAt(0) == 'f' && s.charAt(1) == 'a' && s.charAt(2) == 'l' && s.charAt(3) == 's' && s.charAt(4) == 'e') { |
| 112 | return FALSE; |
| 113 | } |
| 114 | } |
| 115 | ValidationFailure err = new ValidationFailure( |
| 116 | "The string " + Err.wrap(s, Err.VALUE) + " cannot be cast to a boolean"); |
| 117 | err.setErrorCode("FORG0001"); |
| 118 | return err; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the value |
| 123 | * |
| 124 | * @return true or false, the actual boolean value of this BooleanValue |
| 125 | */ |
| 126 | |
| 127 | public boolean getBooleanValue() { |
| 128 | return value; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the effective boolean value of this expression |
| 133 | * |
| 134 | * @return the boolean value |
| 135 | */ |
| 136 | public boolean effectiveBooleanValue() { |
| 137 | return value; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Determine the primitive type of the value. This delivers the same answer as |
| 142 | * getItemType().getPrimitiveItemType(). The primitive types are |
| 143 | * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration, |
| 144 | * and xs:untypedAtomic. For external objects, the result is AnyAtomicType. |
| 145 | */ |
| 146 | |
| 147 | public BuiltInAtomicType getPrimitiveType() { |
| 148 | return BuiltInAtomicType.BOOLEAN; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Convert to string |
| 153 | * |
| 154 | * @return "true" or "false" |
| 155 | */ |
| 156 | |
| 157 | public String getPrimitiveStringValue() { |
| 158 | return value ? "true" : "false"; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get a Comparable value that implements the XML Schema ordering comparison semantics for this value. |
| 163 | * The default implementation returns "this". This is overridden for particular atomic types. |
| 164 | * <p>In the case of data types that are partially ordered, the returned Comparable extends the standard |
| 165 | * semantics of the compareTo() method by returning the value {@link net.sf.saxon.om.SequenceTool#INDETERMINATE_ORDERING} when there |
| 166 | * is no defined order relationship between two given values.</p> |
| 167 | * |
| 168 | * @return a Comparable that follows XML Schema comparison rules |
| 169 | */ |
| 170 | |
| 171 | public Comparable getSchemaComparable() { |
| 172 | return new BooleanComparable(); |
| 173 | } |
| 174 | |
| 175 | private class BooleanComparable implements Comparable { |
| 176 | |
| 177 | public boolean asBoolean() { |
| 178 | return BooleanValue.this.getBooleanValue(); |
| 179 | } |
| 180 | |
| 181 | public int compareTo(/*@NotNull*/ Object o) { |
| 182 | return equals(o) ? 0 : SequenceTool.INDETERMINATE_ORDERING; |
| 183 | } |
| 184 | |
| 185 | public boolean equals(Object o) { |
| 186 | return o instanceof BooleanComparable && asBoolean() == ((BooleanComparable) o).asBoolean(); |
| 187 | } |
| 188 | |
| 189 | public int hashCode() { |
| 190 | return asBoolean() ? 9999999 : 8888888; |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Get a Comparable value that implements the XPath ordering comparison semantics for this value. |
| 197 | * Returns null if the value is not comparable according to XPath rules. The default implementation |
| 198 | * returns null. This is overridden for types that allow ordered comparisons in XPath: numeric, boolean, |
| 199 | * string, date, time, dateTime, yearMonthDuration, dayTimeDuration, and anyURI. |
| 200 | * |
| 201 | * |
| 202 | * @param ordered true if an ordered comparison is required. In this case the result is null if the |
| 203 | * type is unordered; in other cases the returned value will be a Comparable. |
| 204 | * @param collator the collation to be used when comparing strings |
| 205 | * @param implicitTimezone the XPath dynamic evaluation context, used in cases where the comparison is context |
| 206 | * sensitive |
| 207 | * @return an Object whose equals() and hashCode() methods implement the XPath comparison semantics |
| 208 | * with respect to this atomic value. If ordered is specified, the result will either be null if |
| 209 | * no ordering is defined, or will be a Comparable |
| 210 | */ |
| 211 | |
| 212 | public AtomicMatchKey getXPathComparable(boolean ordered, StringCollator collator, int implicitTimezone) { |
| 213 | return this; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Compare the value to another boolean value |
| 218 | * |
| 219 | * @param other The other boolean value |
| 220 | * @return -1 if this one is the lower, 0 if they are equal, +1 if this |
| 221 | * one is the higher. False is considered to be less than true. |
| 222 | * @throws ClassCastException if the other value is not a BooleanValue |
| 223 | * (the parameter is declared as Object to satisfy the Comparable |
| 224 | * interface) |
| 225 | */ |
| 226 | |
| 227 | public int compareTo(Object other) { |
| 228 | if (!(other instanceof BooleanValue)) { |
| 229 | XPathException e = new XPathException("Boolean values are not comparable to " + other.getClass(), "XPTY0004"); |
| 230 | throw new ComparisonException(e); |
| 231 | } |
| 232 | if (value == ((BooleanValue) other).value) { |
| 233 | return 0; |
| 234 | } |
| 235 | if (value) { |
| 236 | return +1; |
| 237 | } |
| 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Determine whether two boolean values are equal |
| 243 | * |
| 244 | * @param other the value to be compared to this value |
| 245 | * @return true if the other value is a boolean value and is equal to this |
| 246 | * value |
| 247 | * @throws ClassCastException if other value is not xs:boolean or derived therefrom |
| 248 | */ |
| 249 | public boolean equals(Object other) { |
| 250 | return other instanceof BooleanValue && value == ((BooleanValue) other).value; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Get a hash code for comparing two BooleanValues |
| 255 | * |
| 256 | * @return the hash code |
| 257 | */ |
| 258 | public int hashCode() { |
| 259 | return value ? 0 : 1; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Diagnostic display of this value as a string |
| 264 | * |
| 265 | * @return a string representation of this value: "true()" or "false()" |
| 266 | */ |
| 267 | public String toString() { |
| 268 | return getStringValue() + "()"; |
| 269 | } |
| 270 | } |
| 271 |