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. /*
  17. * $Id: ExsltBase.java,v 1.5 2004/02/11 17:56:36 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.lib;
  20. import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy;
  21. import org.w3c.dom.Node;
  22. import org.w3c.dom.NodeList;
  23. /**
  24. * The base class for some EXSLT extension classes.
  25. * It contains common utility methods to be used by the sub-classes.
  26. */
  27. public abstract class ExsltBase
  28. {
  29. /**
  30. * Return the string value of a Node
  31. *
  32. * @param n The Node.
  33. * @return The string value of the Node
  34. */
  35. protected static String toString(Node n)
  36. {
  37. if (n instanceof DTMNodeProxy)
  38. return ((DTMNodeProxy)n).getStringValue();
  39. else
  40. {
  41. String value = n.getNodeValue();
  42. if (value == null)
  43. {
  44. NodeList nodelist = n.getChildNodes();
  45. StringBuffer buf = new StringBuffer();
  46. for (int i = 0; i < nodelist.getLength(); i++)
  47. {
  48. Node childNode = nodelist.item(i);
  49. buf.append(toString(childNode));
  50. }
  51. return buf.toString();
  52. }
  53. else
  54. return value;
  55. }
  56. }
  57. /**
  58. * Convert the string value of a Node to a number.
  59. * Return NaN if the string is not a valid number.
  60. *
  61. * @param n The Node.
  62. * @return The number value of the Node
  63. */
  64. protected static double toNumber(Node n)
  65. {
  66. double d = 0.0;
  67. String str = toString(n);
  68. try
  69. {
  70. d = Double.valueOf(str).doubleValue();
  71. }
  72. catch (NumberFormatException e)
  73. {
  74. d= Double.NaN;
  75. }
  76. return d;
  77. }
  78. }