KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > StringValue


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3
4
5 /**
6 * A string value
7 */

8
9 public final class StringValue extends Value {
10     private String JavaDoc value; // may be zero-length, will never be null
11

12     /**
13     * Constructor
14     * @param value the String value. Null is taken as equivalent to "".
15     */

16
17     public StringValue(String JavaDoc value) {
18         this.value = (value==null ? "" : value);
19     }
20
21     /**
22     * Get the string value as a String
23     */

24     
25     public String JavaDoc asString() {
26         return value;
27     }
28
29     /**
30     * Convert the string value to a number
31     */

32
33     public double asNumber() {
34         return Value.stringToNumber(value);
35     }
36
37     /**
38     * Convert the string value to a boolean
39     * @return false if the string value is zero-length, true otherwise
40     */

41
42     public boolean asBoolean() {
43         return (value.length()>0);
44     }
45
46     /**
47     * Return the type of the expression (if known)
48     * @return Value.STRING (always)
49     */

50
51     public int getDataType() {
52         return Value.STRING;
53     }
54
55     /**
56     * Get the length of this string, as defined in XPath. This is not the same as the Java length,
57     * as a Unicode surrogate pair counts as a single character
58     */

59
60     public int getLength() {
61         return getLength(value);
62     }
63
64     /**
65     * Get the length of a string, as defined in XPath. This is not the same as the Java length,
66     * as a Unicode surrogate pair counts as a single character.
67     * @param s The string whose length is required
68     */

69
70     public static int getLength(String JavaDoc 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++; // don't count high surrogates, i.e. D800 to DBFF
75
}
76         return n;
77     }
78
79     /**
80     * Expand a string containing surrogate pairs into an array of 32-bit characters
81     */

82
83     public static int[] expand(String JavaDoc 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                 // we'll trust the data to be sound
91
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     /**
102     * Determine if two StringValues are equal
103     */

104
105     public boolean equals(StringValue other) {
106         return this.value.equals(other.value);
107     }
108
109     /**
110     * Get conversion preference for this value to a Java class. A low result
111     * indicates higher preference.
112     */

113        
114     public int conversionPreference(Class JavaDoc required) {
115
116         if (required==Object JavaDoc.class) return 50;
117         if (required.isAssignableFrom(StringValue.class)) return 0;
118
119         if (required==boolean.class) return 6;
120         if (required==Boolean JavaDoc.class) return 7;
121         if (required==byte.class) return 4;
122         if (required==Byte JavaDoc.class) return 5;
123         if (required==char.class) return 2;
124         if (required==Character JavaDoc.class) return 3;
125         if (required==double.class) return 4;
126         if (required==Double JavaDoc.class) return 5;
127         if (required==float.class) return 4;
128         if (required==Float JavaDoc.class) return 5;
129         if (required==int.class) return 4;
130         if (required==Integer JavaDoc.class) return 5;
131         if (required==long.class) return 4;
132         if (required==Long JavaDoc.class) return 5;
133         if (required==short.class) return 4;
134         if (required==Short JavaDoc.class) return 5;
135         if (required==String JavaDoc.class) return 0;
136         return Integer.MAX_VALUE;
137     }
138
139
140     /**
141     * Convert to Java object (for passing to external functions)
142     */

143     
144     public Object JavaDoc convertToJava(Class JavaDoc target) throws XPathException {
145         if (target==Object JavaDoc.class) {
146             return value;
147         } else if (target.isAssignableFrom(StringValue.class)) {
148             return this;
149         } else if (target==boolean.class) {
150             return new Boolean JavaDoc(asBoolean());
151         } else if (target==Boolean JavaDoc.class) {
152             return new Boolean JavaDoc(asBoolean());
153         } else if (target==String JavaDoc.class) {
154             return value;
155         } else if (target==double.class) {
156             return new Double JavaDoc(asNumber());
157         } else if (target==Double JavaDoc.class) {
158             return new Double JavaDoc(asNumber());
159         } else if (target==float.class) {
160             return new Float JavaDoc(asNumber());
161         } else if (target==Float JavaDoc.class) {
162             return new Float JavaDoc(asNumber());
163         } else if (target==long.class) {
164             return new Long JavaDoc((long)asNumber());
165         } else if (target==Long JavaDoc.class) {
166             return new Long JavaDoc((long)asNumber());
167         } else if (target==int.class) {
168             return new Integer JavaDoc((int)asNumber());
169         } else if (target==Integer JavaDoc.class) {
170             return new Integer JavaDoc((int)asNumber());
171         } else if (target==short.class) {
172             return new Short JavaDoc((short)asNumber());
173         } else if (target==Short JavaDoc.class) {
174             return new Short JavaDoc((short)asNumber());
175         } else if (target==byte.class) {
176             return new Byte JavaDoc((byte)asNumber());
177         } else if (target==Byte JavaDoc.class) {
178             return new Byte JavaDoc((byte)asNumber());
179         } else if (target==char.class || target==Character JavaDoc.class) {
180             if (value.length()==1) {
181                 return new Character JavaDoc(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     /**
192     * Diagnostic print of expression structure
193     */

194     
195     public void display(int level) {
196         System.err.println(indent(level) + "string (\"" + value + "\")" );
197     }
198
199 }
200
201 //
202
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
203
// you may not use this file except in compliance with the License. You may obtain a copy of the
204
// License at http://www.mozilla.org/MPL/
205
//
206
// Software distributed under the License is distributed on an "AS IS" basis,
207
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
208
// See the License for the specific language governing rights and limitations under the License.
209
//
210
// The Original Code is: all this file.
211
//
212
// The Initial Developer of the Original Code is
213
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
214
//
215
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
216
//
217
// Contributor(s): none.
218
//
219

220
Popular Tags