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: ExsltCommon.java,v 1.10 2004/02/11 17:56:36 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.lib;
  20. import com.sun.org.apache.xalan.internal.extensions.ExpressionContext;
  21. import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  22. import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeIterator;
  23. import com.sun.org.apache.xpath.internal.NodeSet;
  24. /**
  25. * This class contains EXSLT common extension functions.
  26. * It is accessed by specifying a namespace URI as follows:
  27. * <pre>
  28. * xmlns:exslt="http://exslt.org/common"
  29. * </pre>
  30. *
  31. * The documentation for each function has been copied from the relevant
  32. * EXSLT Implementer page.
  33. *
  34. * @see <a href="http://www.exslt.org/">EXSLT</a>
  35. * @xsl.usage general
  36. */
  37. public class ExsltCommon
  38. {
  39. /**
  40. * The exsl:object-type function returns a string giving the type of the object passed
  41. * as the argument. The possible object types are: 'string', 'number', 'boolean',
  42. * 'node-set', 'RTF', or 'external'.
  43. *
  44. * Most XSLT object types can be coerced to each other without error. However, there are
  45. * certain coercions that raise errors, most importantly treating anything other than a
  46. * node set as a node set. Authors of utilities such as named templates or user-defined
  47. * extension functions may wish to give some flexibility in the parameter and argument values
  48. * that are accepted by the utility; the exsl:object-type function enables them to do so.
  49. *
  50. * The Xalan extensions MethodResolver converts 'object-type' to 'objectType'.
  51. *
  52. * @param obj The object to be typed.
  53. * @return objectType 'string', 'number', 'boolean', 'node-set', 'RTF', or 'external'.
  54. *
  55. * @see <a href="http://www.exslt.org/">EXSLT</a>
  56. */
  57. public static String objectType (Object obj)
  58. {
  59. if (obj instanceof String)
  60. return "string";
  61. else if (obj instanceof Boolean)
  62. return "boolean";
  63. else if (obj instanceof Number)
  64. return "number";
  65. else if (obj instanceof DTMNodeIterator)
  66. {
  67. DTMIterator dtmI = ((DTMNodeIterator)obj).getDTMIterator();
  68. if (dtmI instanceof com.sun.org.apache.xpath.internal.axes.RTFIterator)
  69. return "RTF";
  70. else
  71. return "node-set";
  72. }
  73. else
  74. return "unknown";
  75. }
  76. /**
  77. * The exsl:node-set function converts a result tree fragment (which is what you get
  78. * when you use the content of xsl:variable rather than its select attribute to give
  79. * a variable value) into a node set. This enables you to process the XML that you create
  80. * within a variable, and therefore do multi-step processing.
  81. *
  82. * You can also use this function to turn a string into a text node, which is helpful
  83. * if you want to pass a string to a function that only accepts a node set.
  84. *
  85. * The Xalan extensions MethodResolver converts 'node-set' to 'nodeSet'.
  86. *
  87. * @param myProcesser is passed in by the Xalan extension processor
  88. * @param rtf The result tree fragment to be converted to a node-set.
  89. *
  90. * @return node-set with the contents of the result tree fragment.
  91. *
  92. * Note: Already implemented in the xalan namespace as nodeset.
  93. *
  94. * @see <a href="http://www.exslt.org/">EXSLT</a>
  95. */
  96. public static NodeSet nodeSet(ExpressionContext myProcessor, Object rtf)
  97. {
  98. return Extensions.nodeset(myProcessor, rtf);
  99. }
  100. }