1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 4 5 8 9 public final class StringValue extends Value { 10 private String value; 12 16 17 public StringValue(String value) { 18 this.value = (value==null ? "" : value); 19 } 20 21 24 25 public String asString() { 26 return value; 27 } 28 29 32 33 public double asNumber() { 34 return Value.stringToNumber(value); 35 } 36 37 41 42 public boolean asBoolean() { 43 return (value.length()>0); 44 } 45 46 50 51 public int getDataType() { 52 return Value.STRING; 53 } 54 55 59 60 public int getLength() { 61 return getLength(value); 62 } 63 64 69 70 public static int getLength(String s) { 71 int n = 0; 72 for (int i=0; i<s.length(); i++) { 73 int c = (int)s.charAt(i); 74 if (c<55296 || c>56319) n++; } 76 return n; 77 } 78 79 82 83 public static int[] expand(String s) { 84 int[] array = new int[getLength(s)]; 85 int o=0; 86 for (int i=0; i<s.length(); i++) { 87 int charval; 88 int c = s.charAt(i); 89 if (c>=55296 && c<=56319) { 90 charval = ((c - 55296) * 1024) + ((int)s.charAt(i+1) - 56320) + 65536; 92 i++; 93 } else { 94 charval = c; 95 } 96 array[o++] = charval; 97 } 98 return array; 99 } 100 101 104 105 public boolean equals(StringValue other) { 106 return this.value.equals(other.value); 107 } 108 109 113 114 public int conversionPreference(Class required) { 115 116 if (required==Object .class) return 50; 117 if (required.isAssignableFrom(StringValue.class)) return 0; 118 119 if (required==boolean.class) return 6; 120 if (required==Boolean .class) return 7; 121 if (required==byte.class) return 4; 122 if (required==Byte .class) return 5; 123 if (required==char.class) return 2; 124 if (required==Character .class) return 3; 125 if (required==double.class) return 4; 126 if (required==Double .class) return 5; 127 if (required==float.class) return 4; 128 if (required==Float .class) return 5; 129 if (required==int.class) return 4; 130 if (required==Integer .class) return 5; 131 if (required==long.class) return 4; 132 if (required==Long .class) return 5; 133 if (required==short.class) return 4; 134 if (required==Short .class) return 5; 135 if (required==String .class) return 0; 136 return Integer.MAX_VALUE; 137 } 138 139 140 143 144 public Object convertToJava(Class target) throws XPathException { 145 if (target==Object .class) { 146 return value; 147 } else if (target.isAssignableFrom(StringValue.class)) { 148 return this; 149 } else if (target==boolean.class) { 150 return new Boolean (asBoolean()); 151 } else if (target==Boolean .class) { 152 return new Boolean (asBoolean()); 153 } else if (target==String .class) { 154 return value; 155 } else if (target==double.class) { 156 return new Double (asNumber()); 157 } else if (target==Double .class) { 158 return new Double (asNumber()); 159 } else if (target==float.class) { 160 return new Float (asNumber()); 161 } else if (target==Float .class) { 162 return new Float (asNumber()); 163 } else if (target==long.class) { 164 return new Long ((long)asNumber()); 165 } else if (target==Long .class) { 166 return new Long ((long)asNumber()); 167 } else if (target==int.class) { 168 return new Integer ((int)asNumber()); 169 } else if (target==Integer .class) { 170 return new Integer ((int)asNumber()); 171 } else if (target==short.class) { 172 return new Short ((short)asNumber()); 173 } else if (target==Short .class) { 174 return new Short ((short)asNumber()); 175 } else if (target==byte.class) { 176 return new Byte ((byte)asNumber()); 177 } else if (target==Byte .class) { 178 return new Byte ((byte)asNumber()); 179 } else if (target==char.class || target==Character .class) { 180 if (value.length()==1) { 181 return new Character (value.charAt(0)); 182 } else { 183 throw new XPathException("Cannot convert string to Java char unless length is 1"); 184 } 185 } else { 186 throw new XPathException("Conversion of string to " + target.getName() + 187 " is not supported"); 188 } 189 } 190 191 194 195 public void display(int level) { 196 System.err.println(indent(level) + "string (\"" + value + "\")" ); 197 } 198 199 } 200 201 220 | Popular Tags |