KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jxpath > ri > InfoSetUtil


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.jxpath.ri;
17
18 import org.apache.commons.jxpath.Pointer;
19 import org.apache.commons.jxpath.ri.model.NodePointer;
20 import org.apache.commons.jxpath.ri.model.VariablePointer;
21
22 /**
23  * Type conversions, XPath style.
24  *
25  * @author Dmitri Plotnikov
26  * @version $Revision: 1.11 $ $Date: 2004/07/16 22:49:33 $
27  */

28 public class InfoSetUtil {
29
30     private static final Double JavaDoc ZERO = new Double JavaDoc(0);
31     private static final Double JavaDoc ONE = new Double JavaDoc(1);
32     private static final Double JavaDoc NOT_A_NUMBER = new Double JavaDoc(Double.NaN);
33
34
35     /**
36      * Converts the supplied object to String
37      */

38     public static String JavaDoc stringValue(Object JavaDoc object) {
39         if (object instanceof String JavaDoc) {
40             return (String JavaDoc) object;
41         }
42         else if (object instanceof Number JavaDoc) {
43             double d = ((Number JavaDoc) object).doubleValue();
44             long l = ((Number JavaDoc) object).longValue();
45             if (d == l) {
46                 return String.valueOf(l);
47             }
48             return String.valueOf(d);
49         }
50         else if (object instanceof Boolean JavaDoc) {
51             return ((Boolean JavaDoc) object).booleanValue() ? "true" : "false";
52         }
53         else if (object == null) {
54             return "";
55         }
56         else if (object instanceof NodePointer) {
57             return stringValue(((NodePointer) object).getValue());
58         }
59         else if (object instanceof EvalContext) {
60             EvalContext ctx = (EvalContext) object;
61             Pointer ptr = ctx.getSingleNodePointer();
62             if (ptr != null) {
63                 return stringValue(ptr);
64             }
65             return "";
66         }
67         return String.valueOf(object);
68     }
69
70     /**
71      * Converts the supplied object to Number
72      */

73     public static Number JavaDoc number(Object JavaDoc object) {
74         if (object instanceof Number JavaDoc) {
75             return (Number JavaDoc) object;
76         }
77         else if (object instanceof Boolean JavaDoc) {
78             return ((Boolean JavaDoc) object).booleanValue() ? ONE : ZERO;
79         }
80         else if (object instanceof String JavaDoc) {
81             Double JavaDoc value;
82             try {
83                 value = new Double JavaDoc((String JavaDoc) object);
84             }
85             catch (NumberFormatException JavaDoc ex) {
86                 value = NOT_A_NUMBER;
87             }
88             return value;
89         }
90         else if (object instanceof EvalContext) {
91             EvalContext ctx = (EvalContext) object;
92             Pointer ptr = ctx.getSingleNodePointer();
93             if (ptr != null) {
94                 return number(ptr);
95             }
96             return NOT_A_NUMBER;
97         }
98         else if (object instanceof NodePointer) {
99             return number(((NodePointer) object).getValue());
100         }
101         return number(stringValue(object));
102     }
103
104     /**
105      * Converts the supplied object to double
106      */

107     public static double doubleValue(Object JavaDoc object) {
108         if (object instanceof Number JavaDoc) {
109             return ((Number JavaDoc) object).doubleValue();
110         }
111         else if (object instanceof Boolean JavaDoc) {
112             return ((Boolean JavaDoc) object).booleanValue() ? 0.0 : 1.0;
113         }
114         else if (object instanceof String JavaDoc) {
115             if (object.equals("")) {
116                 return 0.0;
117             }
118
119             double value;
120             try {
121                 value = Double.parseDouble((String JavaDoc) object);
122             }
123             catch (NumberFormatException JavaDoc ex) {
124                 value = Double.NaN;
125             }
126             return value;
127         }
128         else if (object instanceof NodePointer) {
129             return doubleValue(((NodePointer) object).getValue());
130         }
131         else if (object instanceof EvalContext) {
132             EvalContext ctx = (EvalContext) object;
133             Pointer ptr = ctx.getSingleNodePointer();
134             if (ptr != null) {
135                 return doubleValue(ptr);
136             }
137             return Double.NaN;
138         }
139         return doubleValue(stringValue(object));
140     }
141
142     /**
143      * Converts the supplied object to boolean
144      */

145     public static boolean booleanValue(Object JavaDoc object) {
146         if (object instanceof Number JavaDoc) {
147             double value = ((Number JavaDoc) object).doubleValue();
148             return value != 0 && value != -0 && !Double.isNaN(value);
149         }
150         else if (object instanceof Boolean JavaDoc) {
151             return ((Boolean JavaDoc) object).booleanValue();
152         }
153         else if (object instanceof EvalContext) {
154             EvalContext ctx = (EvalContext) object;
155             Pointer ptr = ctx.getSingleNodePointer();
156             if (ptr == null) {
157                 return false;
158             }
159             return booleanValue(ptr);
160         }
161         else if (object instanceof String JavaDoc) {
162             return ((String JavaDoc) object).length() != 0;
163         }
164         else if (object instanceof NodePointer) {
165             NodePointer pointer = (NodePointer) object;
166             if (pointer instanceof VariablePointer) {
167                 return booleanValue(pointer.getNode());
168             }
169             pointer = pointer.getValuePointer();
170             return pointer.isActual();
171         }
172         else if (object == null) {
173             return false;
174         }
175         return true;
176     }
177 }
Popular Tags