View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath.ri;
18  
19  import org.apache.commons.jxpath.Pointer;
20  import org.apache.commons.jxpath.ri.model.NodePointer;
21  import org.apache.commons.jxpath.ri.model.VariablePointer;
22  
23  /**
24   * Type conversions, XPath style.
25   *
26   * @author Dmitri Plotnikov
27   * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
28   */
29  public class InfoSetUtil {
30  
31      private static final Double ZERO = new Double(0);
32      private static final Double ONE = new Double(1);
33      private static final Double NOT_A_NUMBER = new Double(Double.NaN);
34  
35      /**
36       * Converts the supplied object to String.
37       * @param object to convert
38       * @return String value
39       */
40      public static String stringValue(Object object) {
41          if (object instanceof String) {
42              return (String) object;
43          }
44          if (object instanceof Number) {
45              double d = ((Number) object).doubleValue();
46              long l = ((Number) object).longValue();
47              return d == l ? String.valueOf(l) : String.valueOf(d);
48          }
49          if (object instanceof Boolean) {
50              return ((Boolean) object).booleanValue() ? "true" : "false";
51          }
52          if (object == null) {
53              return "";
54          }
55          if (object instanceof NodePointer) {
56              return stringValue(((NodePointer) object).getValue());
57          }
58          if (object instanceof EvalContext) {
59              EvalContext ctx = (EvalContext) object;
60              Pointer ptr = ctx.getSingleNodePointer();
61              return ptr == null ? "" : stringValue(ptr);
62          }
63          return String.valueOf(object);
64      }
65  
66      /**
67       * Converts the supplied object to Number.
68       * @param object to convert
69       * @return Number result
70       */
71      public static Number number(Object object) {
72          if (object instanceof Number) {
73              return (Number) object;
74          }
75          if (object instanceof Boolean) {
76              return ((Boolean) object).booleanValue() ? ONE : ZERO;
77          }
78          if (object instanceof String) {
79              try {
80                  return new Double((String) object);
81              }
82              catch (NumberFormatException ex) {
83                  return NOT_A_NUMBER;
84              }
85          }
86          if (object instanceof EvalContext) {
87              EvalContext ctx = (EvalContext) object;
88              Pointer ptr = ctx.getSingleNodePointer();
89              return ptr == null ? NOT_A_NUMBER : number(ptr);
90          }
91          if (object instanceof NodePointer) {
92              return number(((NodePointer) object).getValue());
93          }
94          return number(stringValue(object));
95      }
96  
97      /**
98       * Converts the supplied object to double.
99       * @param object to convert
100      * @return double
101      */
102     public static double doubleValue(Object object) {
103         if (object instanceof Number) {
104             return ((Number) object).doubleValue();
105         }
106         if (object instanceof Boolean) {
107             return ((Boolean) object).booleanValue() ? 0.0 : 1.0;
108         }
109         if (object instanceof String) {
110             if (object.equals("")) {
111                 return 0.0;
112             }
113             try {
114                 return Double.parseDouble((String) object);
115             }
116             catch (NumberFormatException ex) {
117                 return Double.NaN;
118             }
119         }
120         if (object instanceof NodePointer) {
121             return doubleValue(((NodePointer) object).getValue());
122         }
123         if (object instanceof EvalContext) {
124             EvalContext ctx = (EvalContext) object;
125             Pointer ptr = ctx.getSingleNodePointer();
126             return ptr == null ? Double.NaN : doubleValue(ptr);
127         }
128         return doubleValue(stringValue(object));
129     }
130 
131     /**
132      * Converts the supplied object to boolean.
133      * @param object to convert
134      * @return boolean
135      */
136     public static boolean booleanValue(Object object) {
137         if (object instanceof Number) {
138             double value = ((Number) object).doubleValue();
139             final int negZero = -0;
140             return value != 0 && value != negZero && !Double.isNaN(value);
141         }
142         if (object instanceof Boolean) {
143             return ((Boolean) object).booleanValue();
144         }
145         if (object instanceof EvalContext) {
146             EvalContext ctx = (EvalContext) object;
147             Pointer ptr = ctx.getSingleNodePointer();
148             return ptr == null ? false : booleanValue(ptr);
149         }
150         if (object instanceof String) {
151             return ((String) object).length() != 0;
152         }
153         if (object instanceof NodePointer) {
154             NodePointer pointer = (NodePointer) object;
155             if (pointer instanceof VariablePointer) {
156                 return booleanValue(pointer.getNode());
157             }
158             pointer = pointer.getValuePointer();
159             return pointer.isActual();
160         }
161         return object != null;
162     }
163 }