1 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.DecimalFormat ; 73 import java.text.NumberFormat ; 74 import java.text.DecimalFormatSymbols ; 75 import java.util.List ; 76 import java.util.Iterator ; 77 import java.util.Locale ; 78 79 197 public class StringFunction implements Function 198 { 199 200 private static DecimalFormat format = (DecimalFormat ) NumberFormat.getInstance(Locale.ENGLISH); 201 202 static { 203 DecimalFormatSymbols symbols = new DecimalFormatSymbols (Locale.ENGLISH); 204 symbols.setNaN("NaN"); 205 symbols.setInfinity("Infinity"); 206 format.setGroupingUsed(false); 207 format.setMaximumFractionDigits(32); 208 format.setDecimalFormatSymbols(symbols); 209 } 210 211 214 public StringFunction() {} 215 216 228 public Object call(Context context, 229 List args) throws FunctionCallException 230 { 231 int size = args.size(); 232 233 if ( size == 0 ) 234 { 235 return evaluate( context.getNodeSet(), 236 context.getNavigator() ); 237 } 238 else if ( size == 1 ) 239 { 240 return evaluate( args.get(0), 241 context.getNavigator() ); 242 } 243 244 throw new FunctionCallException( "string() takes at most argument." ); 245 } 246 247 255 public static String evaluate(Object obj, 256 Navigator nav) 257 { 258 try 259 { 260 if (obj == null) { 261 return ""; 262 } 263 264 if (nav != null && nav.isText(obj)) 267 { 268 return nav.getTextStringValue(obj); 269 } 270 271 if (obj instanceof List ) 272 { 273 List list = (List ) obj; 274 if (list.isEmpty()) 275 { 276 return ""; 277 } 278 obj = list.get(0); 280 } 281 282 if (nav != null && (nav.isElement(obj) || nav.isDocument(obj))) 283 { 284 Iterator descendantAxisIterator = nav.getDescendantAxisIterator(obj); 285 StringBuffer sb = new StringBuffer (); 286 while (descendantAxisIterator.hasNext()) 287 { 288 Object descendant = descendantAxisIterator.next(); 289 if (nav.isText(descendant)) 290 { 291 sb.append(nav.getTextStringValue(descendant)); 292 } 293 } 294 return sb.toString(); 295 } 296 else if (nav != null && nav.isAttribute(obj)) 297 { 298 return nav.getAttributeStringValue(obj); 299 } 300 else if (nav != null && nav.isText(obj)) 301 { 302 return nav.getTextStringValue(obj); 303 } 304 else if (nav != null && nav.isProcessingInstruction(obj)) 305 { 306 return nav.getProcessingInstructionData(obj); 307 } 308 else if (nav != null && nav.isComment(obj)) 309 { 310 return nav.getCommentStringValue(obj); 311 } 312 else if (nav != null && nav.isNamespace(obj)) 313 { 314 return nav.getNamespaceStringValue(obj); 315 } 316 else if (obj instanceof String ) 317 { 318 return (String ) obj; 319 } 320 else if (obj instanceof Boolean ) 321 { 322 return stringValue(((Boolean ) obj).booleanValue()); 323 } 324 else if (obj instanceof Number ) 325 { 326 return stringValue(((Number ) obj).doubleValue()); 327 } 328 return ""; 329 } 330 catch (UnsupportedAxisException e) 331 { 332 throw new JaxenRuntimeException(e); 333 } 334 335 } 336 337 private static String stringValue(double value) 338 { 339 340 if (value == 0) return "0"; 343 344 String result = null; 346 synchronized (format) { 347 result = format.format(value); 348 } 349 return result; 350 351 } 352 353 private static String stringValue(boolean value) 354 { 355 return value ? "true" : "false"; 356 } 357 358 } 359 | Popular Tags |