1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.lib;
  58. import org.w3c.dom.Node;
  59. import org.w3c.dom.Document;
  60. import org.w3c.dom.DocumentFragment;
  61. import org.w3c.dom.NodeList;
  62. import org.w3c.dom.Text;
  63. import org.w3c.dom.traversal.NodeIterator;
  64. import org.apache.xpath.NodeSet;
  65. import org.apache.xpath.objects.XObject;
  66. import org.apache.xpath.objects.XBoolean;
  67. import org.apache.xpath.objects.XNumber;
  68. import org.apache.xpath.objects.XRTreeFrag;
  69. import org.apache.xpath.XPath;
  70. import org.apache.xpath.XPathContext;
  71. import org.apache.xpath.DOMHelper;
  72. import org.apache.xml.dtm.DTMIterator;
  73. import org.apache.xml.dtm.DTM;
  74. import org.apache.xml.dtm.ref.DTMNodeIterator;
  75. import org.apache.xml.utils.XMLString;
  76. import org.xml.sax.SAXNotSupportedException;
  77. import java.util.Hashtable;
  78. import java.util.StringTokenizer;
  79. import org.apache.xalan.extensions.ExpressionContext;
  80. import org.apache.xalan.res.XSLMessages;
  81. import org.apache.xalan.res.XSLTErrorResources;
  82. import org.apache.xalan.xslt.EnvironmentCheck;
  83. import javax.xml.parsers.*;
  84. /**
  85. * <meta name="usage" content="general"/>
  86. * This class contains EXSLT common extension functions.
  87. * It is accessed by specifying a namespace URI as follows:
  88. * <pre>
  89. * xmlns:exslt="http://exslt.org/common"
  90. * </pre>
  91. *
  92. * The documentation for each function has been copied from the relevant
  93. * EXSLT Implementer page.
  94. *
  95. * @see <a href="http://www.exslt.org/">EXSLT</a>
  96. */
  97. public class ExsltCommon
  98. {
  99. /**
  100. * The exsl:object-type function returns a string giving the type of the object passed
  101. * as the argument. The possible object types are: 'string', 'number', 'boolean',
  102. * 'node-set', 'RTF', or 'external'.
  103. *
  104. * Most XSLT object types can be coerced to each other without error. However, there are
  105. * certain coercions that raise errors, most importantly treating anything other than a
  106. * node set as a node set. Authors of utilities such as named templates or user-defined
  107. * extension functions may wish to give some flexibility in the parameter and argument values
  108. * that are accepted by the utility; the exsl:object-type function enables them to do so.
  109. *
  110. * The Xalan extensions MethodResolver converts 'object-type' to 'objectType'.
  111. *
  112. * @param obj The object to be typed.
  113. * @return objectType 'string', 'number', 'boolean', 'node-set', 'RTF', or 'external'.
  114. *
  115. * @see <a href="http://www.exslt.org/">EXSLT</a>
  116. */
  117. public static String objectType (Object obj)
  118. {
  119. if (obj instanceof String)
  120. return "string";
  121. else if (obj instanceof Boolean)
  122. return "boolean";
  123. else if (obj instanceof Number)
  124. return "number";
  125. else if (obj instanceof DTMNodeIterator)
  126. {
  127. DTMIterator dtmI = ((DTMNodeIterator)obj).getDTMIterator();
  128. if (dtmI instanceof org.apache.xpath.axes.RTFIterator)
  129. return "RTF";
  130. else
  131. return "node-set";
  132. }
  133. else
  134. return "unknown";
  135. }
  136. /**
  137. * The exsl:node-set function converts a result tree fragment (which is what you get
  138. * when you use the content of xsl:variable rather than its select attribute to give
  139. * a variable value) into a node set. This enables you to process the XML that you create
  140. * within a variable, and therefore do multi-step processing.
  141. *
  142. * You can also use this function to turn a string into a text node, which is helpful
  143. * if you want to pass a string to a function that only accepts a node set.
  144. *
  145. * The Xalan extensions MethodResolver converts 'node-set' to 'nodeSet'.
  146. *
  147. * @param myProcesser is passed in by the Xalan extension processor
  148. * @param rtf The result tree fragment to be converted to a node-set.
  149. *
  150. * @returns node-set with the contents of the result tree fragment.
  151. *
  152. * Note: Already implemented in the xalan namespace as nodeset.
  153. *
  154. * @see <a href="http://www.exslt.org/">EXSLT</a>
  155. */
  156. public static NodeSet nodeSet(ExpressionContext myProcessor, Object rtf)
  157. {
  158. return Extensions.nodeset(myProcessor, rtf);
  159. }
  160. }