1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
35
36
37
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
69
70
71
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
103
104
105
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
134
135
136
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
166
167
168
169 @Deprecated
170 public InfoSetUtil() {
171
172 }
173 }