FileDocCategorySizeDatePackage
XNumber.javaAPI DocJava SE 5 API9844Fri Aug 26 14:56:10 BST 2005com.sun.org.apache.xpath.internal.objects

XNumber

public class XNumber extends XObject
This class represents an XPath number, and is capable of converting the number to other types, such as a string.
xsl.usage
general

Fields Summary
double
m_val
Value of the XNumber object.
Constructors Summary
public XNumber(double d)
Construct a XNodeSet object.

param
d Value of the object

    super();

    m_val = d;
  
public XNumber(Number num)
Construct a XNodeSet object.

param
d Value of the object


    super();

    m_val = num.doubleValue();
    m_obj = num;
  
Methods Summary
public booleanbool()
Cast result object to a boolean.

return
false if the value is NaN or equal to 0.0

    return (Double.isNaN(m_val) || (m_val == 0.0)) ? false : true;
  
public voidcallVisitors(com.sun.org.apache.xpath.internal.ExpressionOwner owner, com.sun.org.apache.xpath.internal.XPathVisitor visitor)

see
XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)

  	visitor.visitNumberLiteral(owner, this);
  
public booleanequals(com.sun.org.apache.xpath.internal.objects.XObject obj2)
Tell if two objects are functionally equal.

param
obj2 Object to compare this to
return
true if the two objects are equal
throws
javax.xml.transform.TransformerException


    // In order to handle the 'all' semantics of 
    // nodeset comparisons, we always call the 
    // nodeset function.
    int t = obj2.getType();
    try
    {
	    if (t == XObject.CLASS_NODESET)
	      return obj2.equals(this);
	    else if(t == XObject.CLASS_BOOLEAN)
	      return obj2.bool() == bool();
		else
	       return m_val == obj2.num();
    }
    catch(javax.xml.transform.TransformerException te)
    {
      throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te);
    }
  
public intgetType()
Tell that this is a CLASS_NUMBER.

return
node type CLASS_NUMBER

    return CLASS_NUMBER;
  
public java.lang.StringgetTypeString()
Given a request type, return the equivalent string. For diagnostic purposes.

return
type string "#NUMBER"

    return "#NUMBER";
  
public booleanisStableNumber()
Tell if this expression returns a stable number that will not change during iterations within the expression. This is used to determine if a proximity position predicate can indicate that no more searching has to occur.

return
true if the expression represents a stable number.

    return true;
  
public doublenum()
Cast result object to a number.

return
the value of the XNumber object

    return m_val;
  
public doublenum(com.sun.org.apache.xpath.internal.XPathContext xctxt)
Evaluate expression to a number.

return
0.0
throws
javax.xml.transform.TransformerException


    return m_val;
  
public java.lang.Objectobject()
Return a java object that's closest to the representation that should be handed to an extension.

return
The value of this XNumber as a Double object

    if(null == m_obj)
      m_obj = new Double(m_val);
    return m_obj;
  
public java.lang.Stringstr()
Cast result object to a string.

return
"NaN" if the number is NaN, Infinity or -Infinity if the number is infinite or the string value of the number.


    if (Double.isNaN(m_val))
    {
      return "NaN";
    }
    else if (Double.isInfinite(m_val))
    {
      if (m_val > 0)
        return "Infinity";
      else
        return "-Infinity";
    }

    double num = m_val;
    String s = Double.toString(num);
    int len = s.length();

    if (s.charAt(len - 2) == '." && s.charAt(len - 1) == '0")
    {
      s = s.substring(0, len - 2);

      if (s.equals("-0"))
        return "0";

      return s;
    }

    int e = s.indexOf('E");

    if (e < 0)
    {
      if (s.charAt(len - 1) == '0")
        return s.substring(0, len - 1);
      else
        return s;
    }

    int exp = Integer.parseInt(s.substring(e + 1));
    String sign;

    if (s.charAt(0) == '-")
    {
      sign = "-";
      s = s.substring(1);

      --e;
    }
    else
      sign = "";

    int nDigits = e - 2;

    if (exp >= nDigits)
      return sign + s.substring(0, 1) + s.substring(2, e)
             + zeros(exp - nDigits);

    // Eliminate trailing 0's - bugzilla 14241
    while (s.charAt(e-1) == '0")
      e--;
         
    if (exp > 0)
      return sign + s.substring(0, 1) + s.substring(2, 2 + exp) + "."
             + s.substring(2 + exp, e);

    return sign + "0." + zeros(-1 - exp) + s.substring(0, 1)
           + s.substring(2, e);
  
private static java.lang.Stringzeros(int n)
Return a string of '0' of the given length

param
n Length of the string to be returned
return
a string of '0' with the given length

    if (n < 1)
      return "";

    char[] buf = new char[n];

    for (int i = 0; i < n; i++)
    {
      buf[i] = '0";
    }

    return new String(buf);