1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25
26
27
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
37
38
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
68
69
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
99
100
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
133
134
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 }