1 16 19 package com.sun.org.apache.xpath.internal.objects; 20 21 import com.sun.org.apache.xpath.internal.ExpressionOwner; 22 import com.sun.org.apache.xpath.internal.XPathContext; 23 import com.sun.org.apache.xpath.internal.XPathVisitor; 24 25 30 public class XNumber extends XObject 31 { 32 33 35 double m_val; 36 37 42 public XNumber(double d) 43 { 44 super(); 45 46 m_val = d; 47 } 48 49 54 public XNumber(Number num) 55 { 56 57 super(); 58 59 m_val = num.doubleValue(); 60 m_obj = num; 61 } 62 63 68 public int getType() 69 { 70 return CLASS_NUMBER; 71 } 72 73 79 public String getTypeString() 80 { 81 return "#NUMBER"; 82 } 83 84 89 public double num() 90 { 91 return m_val; 92 } 93 94 101 public double num(XPathContext xctxt) 102 throws javax.xml.transform.TransformerException 103 { 104 105 return m_val; 106 } 107 108 113 public boolean bool() 114 { 115 return (Double.isNaN(m_val) || (m_val == 0.0)) ? false : true; 116 } 117 118 268 274 public String str() 275 { 276 277 if (Double.isNaN(m_val)) 278 { 279 return "NaN"; 280 } 281 else if (Double.isInfinite(m_val)) 282 { 283 if (m_val > 0) 284 return "Infinity"; 285 else 286 return "-Infinity"; 287 } 288 289 double num = m_val; 290 String s = Double.toString(num); 291 int len = s.length(); 292 293 if (s.charAt(len - 2) == '.' && s.charAt(len - 1) == '0') 294 { 295 s = s.substring(0, len - 2); 296 297 if (s.equals("-0")) 298 return "0"; 299 300 return s; 301 } 302 303 int e = s.indexOf('E'); 304 305 if (e < 0) 306 { 307 if (s.charAt(len - 1) == '0') 308 return s.substring(0, len - 1); 309 else 310 return s; 311 } 312 313 int exp = Integer.parseInt(s.substring(e + 1)); 314 String sign; 315 316 if (s.charAt(0) == '-') 317 { 318 sign = "-"; 319 s = s.substring(1); 320 321 --e; 322 } 323 else 324 sign = ""; 325 326 int nDigits = e - 2; 327 328 if (exp >= nDigits) 329 return sign + s.substring(0, 1) + s.substring(2, e) 330 + zeros(exp - nDigits); 331 332 while (s.charAt(e-1) == '0') 334 e--; 335 336 if (exp > 0) 337 return sign + s.substring(0, 1) + s.substring(2, 2 + exp) + "." 338 + s.substring(2 + exp, e); 339 340 return sign + "0." + zeros(-1 - exp) + s.substring(0, 1) 341 + s.substring(2, e); 342 } 343 344 345 353 static private String zeros(int n) 354 { 355 if (n < 1) 356 return ""; 357 358 char[] buf = new char[n]; 359 360 for (int i = 0; i < n; i++) 361 { 362 buf[i] = '0'; 363 } 364 365 return new String (buf); 366 } 367 368 374 public Object object() 375 { 376 if(null == m_obj) 377 m_obj = new Double (m_val); 378 return m_obj; 379 } 380 381 390 public boolean equals(XObject obj2) 391 { 392 393 int t = obj2.getType(); 397 try 398 { 399 if (t == XObject.CLASS_NODESET) 400 return obj2.equals(this); 401 else if(t == XObject.CLASS_BOOLEAN) 402 return obj2.bool() == bool(); 403 else 404 return m_val == obj2.num(); 405 } 406 catch(javax.xml.transform.TransformerException te) 407 { 408 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te); 409 } 410 } 411 412 420 public boolean isStableNumber() 421 { 422 return true; 423 } 424 425 428 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 429 { 430 visitor.visitNumberLiteral(owner, this); 431 } 432 433 434 } 435 | Popular Tags |