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  
18  package org.apache.commons.jxpath.ri;
19  
20  import org.apache.commons.jxpath.Pointer;
21  import org.apache.commons.jxpath.ri.model.NodePointer;
22  import org.apache.commons.jxpath.ri.model.VariablePointer;
23  
24  /**
25   * Type conversions, XPath style.
26   */
27  public class InfoSetUtil {
28  
29      private static final Double ZERO = Double.valueOf(0);
30      private static final Double ONE = Double.valueOf(1);
31      private static final Double NOT_A_NUMBER = Double.valueOf(Double.NaN);
32  
33      /**
34       * Converts the supplied object to boolean.
35       *
36       * @param object to convert
37       * @return boolean
38       */
39      public static boolean booleanValue(final Object object) {
40          if (object instanceof Number) {
41              final double value = ((Number) object).doubleValue();
42              final int negZero = -0;
43              return value != 0 && value != negZero && !Double.isNaN(value);
44          }
45          if (object instanceof Boolean) {
46              return ((Boolean) object).booleanValue();
47          }
48          if (object instanceof EvalContext) {
49              final EvalContext ctx = (EvalContext) object;
50              final Pointer ptr = ctx.getSingleNodePointer();
51              return ptr != null && booleanValue(ptr);
52          }
53          if (object instanceof String) {
54              return ((String) object).length() != 0;
55          }
56          if (object instanceof NodePointer) {
57              NodePointer pointer = (NodePointer) object;
58              if (pointer instanceof VariablePointer) {
59                  return booleanValue(pointer.getNode());
60              }
61              pointer = pointer.getValuePointer();
62              return pointer.isActual();
63          }
64          return object != null;
65      }
66  
67      /**
68       * Converts the supplied object to double.
69       *
70       * @param object to convert
71       * @return double
72       */
73      public static double doubleValue(final Object object) {
74          if (object instanceof Number) {
75              return ((Number) object).doubleValue();
76          }
77          if (object instanceof Boolean) {
78              return ((Boolean) object).booleanValue() ? 0.0 : 1.0;
79          }
80          if (object instanceof String) {
81              if (object.equals("")) {
82                  return 0.0;
83              }
84              try {
85                  return Double.parseDouble((String) object);
86              } catch (final NumberFormatException ex) {
87                  return Double.NaN;
88              }
89          }
90          if (object instanceof NodePointer) {
91              return doubleValue(((NodePointer) object).getValue());
92          }
93          if (object instanceof EvalContext) {
94              final EvalContext ctx = (EvalContext) object;
95              final Pointer ptr = ctx.getSingleNodePointer();
96              return ptr == null ? Double.NaN : doubleValue(ptr);
97          }
98          return doubleValue(stringValue(object));
99      }
100 
101     /**
102      * Converts the supplied object to Number.
103      *
104      * @param object to convert
105      * @return Number result
106      */
107     public static Number number(final Object object) {
108         if (object instanceof Number) {
109             return (Number) object;
110         }
111         if (object instanceof Boolean) {
112             return ((Boolean) object).booleanValue() ? ONE : ZERO;
113         }
114         if (object instanceof String) {
115             try {
116                 return Double.valueOf((String) object);
117             } catch (final NumberFormatException ex) {
118                 return NOT_A_NUMBER;
119             }
120         }
121         if (object instanceof EvalContext) {
122             final EvalContext ctx = (EvalContext) object;
123             final Pointer ptr = ctx.getSingleNodePointer();
124             return ptr == null ? NOT_A_NUMBER : number(ptr);
125         }
126         if (object instanceof NodePointer) {
127             return number(((NodePointer) object).getValue());
128         }
129         return number(stringValue(object));
130     }
131 
132     /**
133      * Converts the supplied object to String.
134      *
135      * @param object to convert
136      * @return String value
137      */
138     public static String stringValue(final Object object) {
139         if (object instanceof String) {
140             return (String) object;
141         }
142         if (object instanceof Number) {
143             final double d = ((Number) object).doubleValue();
144             final long l = ((Number) object).longValue();
145             return d == l ? String.valueOf(l) : String.valueOf(d);
146         }
147         if (object instanceof Boolean) {
148             return ((Boolean) object).booleanValue() ? "true" : "false";
149         }
150         if (object == null) {
151             return "";
152         }
153         if (object instanceof NodePointer) {
154             return stringValue(((NodePointer) object).getValue());
155         }
156         if (object instanceof EvalContext) {
157             final EvalContext ctx = (EvalContext) object;
158             final Pointer ptr = ctx.getSingleNodePointer();
159             return ptr == null ? "" : stringValue(ptr);
160         }
161         return String.valueOf(object);
162     }
163 
164     /**
165      * Constructs a new instance.
166      *
167      * @deprecated Will be private in the next major version.
168      */
169     @Deprecated
170     public InfoSetUtil() {
171         // empty
172     }
173 }