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.text.NumberFormat;
73 import java.util.List;
74 import java.util.Iterator;
75 import java.util.Locale;
76
77 /***
78 * <p><b>4.2</b> <code><i>string</i> string(<i>object</i>)</code>
79 *
80 * @author bob mcwhirter (bob @ werken.com)
81 */
82 public class StringFunction implements Function
83 {
84
85 private static NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH);
86
87 static {
88 format.setGroupingUsed(false);
89 format.setMaximumFractionDigits(32);
90 }
91
92 public Object call(Context context,
93 List args) throws FunctionCallException
94 {
95 int size = args.size();
96
97 if ( size == 0 )
98 {
99 return evaluate( context.getNodeSet(),
100 context.getNavigator() );
101 }
102 else if ( size == 1 )
103 {
104 return evaluate( args.get(0),
105 context.getNavigator() );
106 }
107
108 throw new FunctionCallException( "string() requires one argument." );
109 }
110
111 public static String evaluate(Object obj,
112 Navigator nav)
113 {
114 try
115 {
116 if (obj == null) {
117 return "";
118 }
119
120
121
122 if (nav != null && nav.isText(obj))
123 {
124 return nav.getTextStringValue(obj);
125 }
126
127 if (obj instanceof List)
128 {
129 List list = (List) obj;
130 if (list.isEmpty())
131 {
132 return "";
133 }
134
135 obj = list.get(0);
136 }
137
138 if (nav != null && (nav.isElement(obj) || nav.isDocument(obj)))
139 {
140 Iterator descendantAxisIterator = nav.getDescendantAxisIterator(obj);
141 StringBuffer sb = new StringBuffer();
142 while (descendantAxisIterator.hasNext())
143 {
144 Object descendant = descendantAxisIterator.next();
145 if (nav.isText(descendant))
146 {
147 sb.append(nav.getTextStringValue(descendant));
148 }
149 }
150 return sb.toString();
151 }
152 else if (nav != null && nav.isAttribute(obj))
153 {
154 return nav.getAttributeStringValue(obj);
155 }
156 else if (nav != null && nav.isText(obj))
157 {
158 return nav.getTextStringValue(obj);
159 }
160 else if (nav != null && nav.isProcessingInstruction(obj))
161 {
162 return nav.getProcessingInstructionData(obj);
163 }
164 else if (nav != null && nav.isComment(obj))
165 {
166 return nav.getCommentStringValue(obj);
167 }
168 else if (nav != null && nav.isNamespace(obj))
169 {
170 return nav.getNamespaceStringValue(obj);
171 }
172 else if (obj instanceof String)
173 {
174 return (String) obj;
175 }
176 else if (obj instanceof Boolean)
177 {
178 return stringValue(((Boolean) obj).booleanValue());
179 }
180 else if (obj instanceof Number)
181 {
182 return stringValue(((Number) obj).doubleValue());
183 }
184 return "";
185 }
186 catch (UnsupportedAxisException e)
187 {
188 throw new JaxenRuntimeException(e);
189 }
190
191 }
192
193 public static String stringValue(double value)
194 {
195 return format.format(value);
196 }
197
198 public static String stringValue(boolean bool)
199 {
200 return bool ? "true" : "false";
201 }
202
203 }