001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri;
017:
018: import org.apache.commons.jxpath.Pointer;
019: import org.apache.commons.jxpath.ri.model.NodePointer;
020: import org.apache.commons.jxpath.ri.model.VariablePointer;
021:
022: /**
023: * Type conversions, XPath style.
024: *
025: * @author Dmitri Plotnikov
026: * @version $Revision: 1.11 $ $Date: 2004/07/16 22:49:33 $
027: */
028: public class InfoSetUtil {
029:
030: private static final Double ZERO = new Double(0);
031: private static final Double ONE = new Double(1);
032: private static final Double NOT_A_NUMBER = new Double(Double.NaN);
033:
034: /**
035: * Converts the supplied object to String
036: */
037: public static String stringValue(Object object) {
038: if (object instanceof String) {
039: return (String) object;
040: } else if (object instanceof Number) {
041: double d = ((Number) object).doubleValue();
042: long l = ((Number) object).longValue();
043: if (d == l) {
044: return String.valueOf(l);
045: }
046: return String.valueOf(d);
047: } else if (object instanceof Boolean) {
048: return ((Boolean) object).booleanValue() ? "true" : "false";
049: } else if (object == null) {
050: return "";
051: } else if (object instanceof NodePointer) {
052: return stringValue(((NodePointer) object).getValue());
053: } else if (object instanceof EvalContext) {
054: EvalContext ctx = (EvalContext) object;
055: Pointer ptr = ctx.getSingleNodePointer();
056: if (ptr != null) {
057: return stringValue(ptr);
058: }
059: return "";
060: }
061: return String.valueOf(object);
062: }
063:
064: /**
065: * Converts the supplied object to Number
066: */
067: public static Number number(Object object) {
068: if (object instanceof Number) {
069: return (Number) object;
070: } else if (object instanceof Boolean) {
071: return ((Boolean) object).booleanValue() ? ONE : ZERO;
072: } else if (object instanceof String) {
073: Double value;
074: try {
075: value = new Double((String) object);
076: } catch (NumberFormatException ex) {
077: value = NOT_A_NUMBER;
078: }
079: return value;
080: } else if (object instanceof EvalContext) {
081: EvalContext ctx = (EvalContext) object;
082: Pointer ptr = ctx.getSingleNodePointer();
083: if (ptr != null) {
084: return number(ptr);
085: }
086: return NOT_A_NUMBER;
087: } else if (object instanceof NodePointer) {
088: return number(((NodePointer) object).getValue());
089: }
090: return number(stringValue(object));
091: }
092:
093: /**
094: * Converts the supplied object to double
095: */
096: public static double doubleValue(Object object) {
097: if (object instanceof Number) {
098: return ((Number) object).doubleValue();
099: } else if (object instanceof Boolean) {
100: return ((Boolean) object).booleanValue() ? 0.0 : 1.0;
101: } else if (object instanceof String) {
102: if (object.equals("")) {
103: return 0.0;
104: }
105:
106: double value;
107: try {
108: value = Double.parseDouble((String) object);
109: } catch (NumberFormatException ex) {
110: value = Double.NaN;
111: }
112: return value;
113: } else if (object instanceof NodePointer) {
114: return doubleValue(((NodePointer) object).getValue());
115: } else if (object instanceof EvalContext) {
116: EvalContext ctx = (EvalContext) object;
117: Pointer ptr = ctx.getSingleNodePointer();
118: if (ptr != null) {
119: return doubleValue(ptr);
120: }
121: return Double.NaN;
122: }
123: return doubleValue(stringValue(object));
124: }
125:
126: /**
127: * Converts the supplied object to boolean
128: */
129: public static boolean booleanValue(Object object) {
130: if (object instanceof Number) {
131: double value = ((Number) object).doubleValue();
132: return value != 0 && value != -0 && !Double.isNaN(value);
133: } else if (object instanceof Boolean) {
134: return ((Boolean) object).booleanValue();
135: } else if (object instanceof EvalContext) {
136: EvalContext ctx = (EvalContext) object;
137: Pointer ptr = ctx.getSingleNodePointer();
138: if (ptr == null) {
139: return false;
140: }
141: return booleanValue(ptr);
142: } else if (object instanceof String) {
143: return ((String) object).length() != 0;
144: } else if (object instanceof NodePointer) {
145: NodePointer pointer = (NodePointer) object;
146: if (pointer instanceof VariablePointer) {
147: return booleanValue(pointer.getNode());
148: }
149: pointer = pointer.getValuePointer();
150: return pointer.isActual();
151: } else if (object == null) {
152: return false;
153: }
154: return true;
155: }
156: }
|