1 package net.sf.saxon.value; 2 import net.sf.saxon.Err; 3 import net.sf.saxon.ConversionContext; 4 import net.sf.saxon.expr.XPathContext; 5 import net.sf.saxon.trans.DynamicError; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.type.*; 8 9 12 13 public final class BooleanValue extends AtomicValue implements Comparable { 14 private boolean value; 15 16 19 public static final BooleanValue TRUE = new BooleanValue(true); 20 23 public static final BooleanValue FALSE = new BooleanValue(false); 24 25 30 31 private BooleanValue(boolean value) { 32 this.value = value; 33 } 34 35 42 43 public static BooleanValue get(boolean value) { 44 return (value ? TRUE : FALSE); 45 } 46 47 53 54 public static AtomicValue fromString(CharSequence s) { 55 s = trimWhitespace(s).toString(); 56 if (s.equals("true") || s.equals("1")) { 57 return TRUE; 58 } else if (s.equals("false") || s.equals("0")) { 59 return FALSE; 60 } else { 61 ValidationException err = new ValidationException( 62 "The string " + Err.wrap(s, Err.VALUE) + " cannot be cast to a boolean"); 63 err.setErrorCode("FORG0001"); 64 return new ValidationErrorValue(err); 65 } 66 } 67 68 72 73 public boolean getBooleanValue() { 74 return value; 75 } 76 77 84 public boolean effectiveBooleanValue(XPathContext context) { 85 return value; 86 } 87 88 94 95 public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) { 96 switch(requiredType.getPrimitiveType()) { 97 case Type.BOOLEAN: 98 case Type.ATOMIC: 99 case Type.ITEM: 100 return this; 101 case Type.NUMBER: 102 case Type.INTEGER: 103 return new IntegerValue(value ? 1 : 0); 104 case Type.DECIMAL: 105 case Type.FLOAT: 106 case Type.DOUBLE: 107 return new IntegerValue(value ? 1 : 0).convertPrimitive(requiredType, validate, conversion); 108 case Type.STRING: 109 return new StringValue(getStringValueCS()); 110 case Type.UNTYPED_ATOMIC: 111 return new UntypedAtomicValue(getStringValueCS()); 112 default: 113 ValidationException err = new ValidationException("Cannot convert boolean to " + 114 requiredType.getDisplayName()); 115 err.setErrorCode("FORG0001"); 117 return new ValidationErrorValue(err); 118 } 119 } 120 121 125 126 public String getStringValue() { 127 return (value ? "true" : "false"); 128 } 129 130 134 135 public ItemType getItemType() { 136 return Type.BOOLEAN_TYPE; 137 } 138 139 146 147 public Object convertToJava(Class target, XPathContext context) throws XPathException { 148 if (target==Object .class) { 149 return Boolean.valueOf(value); 150 } else if (target.isAssignableFrom(BooleanValue.class)) { 151 return this; 152 } else if (target==boolean.class) { 153 return Boolean.valueOf(value); 154 } else if (target==Boolean .class) { 155 return Boolean.valueOf(value); 156 } else if (target==String .class || target==CharSequence .class) { 157 return getStringValue(); 158 } else if (target==double.class) { 159 return new Double ((double)(value ? 1 : 0)); 160 } else if (target==Double .class) { 161 return new Double ((double)(value ? 1 : 0)); 162 } else if (target==float.class) { 163 return new Float ((float)(value ? 1 : 0)); 164 } else if (target==Float .class) { 165 return new Float ((float)(value ? 1 : 0)); 166 } else if (target==long.class) { 167 return new Long ((long)(value ? 1 : 0)); 168 } else if (target==Long .class) { 169 return new Long ((long)(value ? 1 : 0)); 170 } else if (target==int.class) { 171 return new Integer (value ? 1 : 0); 172 } else if (target==Integer .class) { 173 return new Integer (value ? 1 : 0); 174 } else if (target==short.class) { 175 return new Short ((short)(value ? 1 : 0)); 176 } else if (target==Short .class) { 177 return new Short ((short)(value ? 1 : 0)); 178 } else if (target==byte.class) { 179 return new Byte ((byte)(value ? 1 : 0)); 180 } else if (target==Byte .class) { 181 return new Byte ((byte)(value ? 1 : 0)); 182 } else if (target==char.class) { 183 return new Character (value ? '1' : '0'); 184 } else if (target==Character .class) { 185 return new Character (value ? '1' : '0'); 186 } else { 187 Object o = super.convertToJava(target, context); 188 if (o == null) { 189 DynamicError err = new DynamicError("Conversion of boolean to " + target.getName() + 190 " is not supported"); 191 err.setXPathContext(context); 192 err.setErrorCode("SAXON:0000"); 193 throw err; 194 } 195 return o; 196 } 197 } 198 199 209 210 public int compareTo(Object other) { 211 if (!(other instanceof BooleanValue)) { 212 throw new ClassCastException ("Boolean values are not comparable to " + other.getClass()); 213 } 214 if (this.value == ((BooleanValue)other).value) return 0; 215 if (this.value) return +1; 216 return -1; 217 } 218 219 226 public boolean equals(Object other) { 227 if (!(other instanceof BooleanValue)) { 228 throw new ClassCastException ("Boolean values are not comparable to " + other.getClass()); 229 } 230 return (this.value == ((BooleanValue)other).value); 231 } 232 233 238 public int hashCode() { 239 return (value ? 0 : 1); 240 } 241 242 246 public String toString() { 247 return getStringValue() + "()"; 248 } 249 } 250 251 269 | Popular Tags |