1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 package org.jaxen.function;
64
65 import org.jaxen.Context;
66 import org.jaxen.Function;
67 import org.jaxen.FunctionCallException;
68 import org.jaxen.Navigator;
69 import org.jaxen.UnsupportedAxisException;
70 import org.jaxen.JaxenRuntimeException;
71
72 import java.util.List;
73 import java.util.Iterator;
74
75 /***
76 * <p><b>4.2</b> <code><i>string</i> string(<i>object</i>)</code>
77 *
78 * @author bob mcwhirter (bob @ werken.com)
79 */
80 public class StringFunction implements Function
81 {
82
83 public Object call(Context context,
84 List args) throws FunctionCallException
85 {
86 int size = args.size();
87
88 if ( size == 0 )
89 {
90 return evaluate( context.getNodeSet(),
91 context.getNavigator() );
92 }
93 else if ( size == 1 )
94 {
95 return evaluate( args.get(0),
96 context.getNavigator() );
97 }
98
99 throw new FunctionCallException( "string() requires one argument." );
100 }
101
102 public static String evaluate(Object obj,
103 Navigator nav)
104 {
105 try
106 {
107 String retval = "";
108 if (obj == null) {
109 return "";
110 }
111 if (obj instanceof List)
112 {
113 List list = (List) obj;
114 if (list.isEmpty())
115 {
116 return "";
117 }
118
119 obj = list.get(0);
120 }
121 if (nav != null && (nav.isElement(obj) || nav.isDocument(obj)))
122 {
123 Iterator descendantAxisIterator = nav.getDescendantAxisIterator(obj);
124 StringBuffer sb = new StringBuffer();
125 while (descendantAxisIterator.hasNext())
126 {
127 Object descendant = descendantAxisIterator.next();
128 if (nav.isText(descendant))
129 {
130 sb.append(nav.getTextStringValue(descendant));
131 }
132 }
133 retval = sb.toString();
134 }
135 else if (nav != null && nav.isAttribute(obj))
136 {
137 retval = nav.getAttributeStringValue(obj);
138 }
139 else if (nav != null && nav.isText(obj))
140 {
141 retval = nav.getTextStringValue(obj);
142 }
143 else if (nav != null && nav.isProcessingInstruction(obj))
144 {
145 retval = nav.getProcessingInstructionData(obj);
146 }
147 else if (nav != null && nav.isComment(obj))
148 {
149 retval = nav.getCommentStringValue(obj);
150 }
151 else if (nav != null && nav.isNamespace(obj))
152 {
153 retval = nav.getNamespaceStringValue(obj);
154 }
155 else if (obj instanceof String)
156 {
157 retval = (String) obj;
158 }
159 else if (obj instanceof Boolean)
160 {
161 retval = stringValue(((Boolean) obj).booleanValue());
162 }
163 else if (obj instanceof Number)
164 {
165 retval = stringValue(((Number) obj).doubleValue());
166 }
167 retval = retval == null ? "" : retval;
168 return retval;
169 }
170 catch (UnsupportedAxisException e)
171 {
172 throw new JaxenRuntimeException(e);
173 }
174
175 }
176
177 public static String stringValue(double value)
178 {
179 if (Double.isNaN(value))
180 {
181 return "NaN";
182 }
183 if (-0.0 == value || 0.0 == value)
184 {
185 return "0";
186 }
187 if (Double.isInfinite(value) && value < 0)
188 {
189 return "-Infinity";
190 }
191 if (Double.isInfinite(value) && value > 0)
192 {
193 return "Infinity";
194 }
195 if (((long) value) == value)
196 {
197 return Long.toString((long) value);
198 }
199 return Double.toString(value);
200 }
201
202 public static String stringValue(boolean bool)
203 {
204 return bool ? "true" : "false";
205 }
206
207 }