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