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.xpath.functions;
  58. import java.util.Hashtable;
  59. import java.util.StringTokenizer;
  60. import org.apache.xpath.res.XPATHErrorResources;
  61. //import org.w3c.dom.Node;
  62. //import org.w3c.dom.traversal.NodeIterator;
  63. import org.apache.xml.dtm.DTM;
  64. import org.apache.xml.dtm.DTMIterator;
  65. import java.util.Vector;
  66. import org.apache.xpath.XPathContext;
  67. import org.apache.xpath.DOMHelper;
  68. import org.apache.xpath.XPath;
  69. import org.apache.xpath.NodeSetDTM;
  70. import org.apache.xpath.objects.XObject;
  71. import org.apache.xpath.objects.XNodeSet;
  72. import org.apache.xml.utils.StringVector;
  73. /**
  74. * <meta name="usage" content="advanced"/>
  75. * Execute the Id() function.
  76. */
  77. public class FuncId extends FunctionOneArg
  78. {
  79. /**
  80. * Fill in a list with nodes that match a space delimited list if ID
  81. * ID references.
  82. *
  83. * @param xctxt The runtime XPath context.
  84. * @param docContext The document where the nodes are being looked for.
  85. * @param refval A space delimited list of ID references.
  86. * @param usedrefs List of references for which nodes were found.
  87. * @param nodeSet Node set where the nodes will be added to.
  88. * @param mayBeMore true if there is another set of nodes to be looked for.
  89. *
  90. * @return The usedrefs value.
  91. */
  92. private StringVector getNodesByID(XPathContext xctxt, int docContext,
  93. String refval, StringVector usedrefs,
  94. NodeSetDTM nodeSet, boolean mayBeMore)
  95. {
  96. if (null != refval)
  97. {
  98. String ref = null;
  99. // DOMHelper dh = xctxt.getDOMHelper();
  100. StringTokenizer tokenizer = new StringTokenizer(refval);
  101. boolean hasMore = tokenizer.hasMoreTokens();
  102. DTM dtm = xctxt.getDTM(docContext);
  103. while (hasMore)
  104. {
  105. ref = tokenizer.nextToken();
  106. hasMore = tokenizer.hasMoreTokens();
  107. if ((null != usedrefs) && usedrefs.contains(ref))
  108. {
  109. ref = null;
  110. continue;
  111. }
  112. int node = dtm.getElementById(ref);
  113. if (DTM.NULL != node)
  114. nodeSet.addNodeInDocOrder(node, xctxt);
  115. if ((null != ref) && (hasMore || mayBeMore))
  116. {
  117. if (null == usedrefs)
  118. usedrefs = new StringVector();
  119. usedrefs.addElement(ref);
  120. }
  121. }
  122. }
  123. return usedrefs;
  124. }
  125. /**
  126. * Execute the function. The function must return
  127. * a valid object.
  128. * @param xctxt The current execution context.
  129. * @return A valid XObject.
  130. *
  131. * @throws javax.xml.transform.TransformerException
  132. */
  133. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  134. {
  135. int context = xctxt.getCurrentNode();
  136. DTM dtm = xctxt.getDTM(context);
  137. int docContext = dtm.getDocument();
  138. if (DTM.NULL == docContext)
  139. error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null);
  140. XObject arg = m_arg0.execute(xctxt);
  141. int argType = arg.getType();
  142. XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
  143. NodeSetDTM nodeSet = nodes.mutableNodeset();
  144. if (XObject.CLASS_NODESET == argType)
  145. {
  146. DTMIterator ni = arg.iter();
  147. StringVector usedrefs = null;
  148. int pos = ni.nextNode();
  149. while (DTM.NULL != pos)
  150. {
  151. DTM ndtm = ni.getDTM(pos);
  152. String refval = ndtm.getStringValue(pos).toString();
  153. pos = ni.nextNode();
  154. usedrefs = getNodesByID(xctxt, docContext, refval, usedrefs, nodeSet,
  155. DTM.NULL != pos);
  156. }
  157. // ni.detach();
  158. }
  159. else if (XObject.CLASS_NULL == argType)
  160. {
  161. return nodes;
  162. }
  163. else
  164. {
  165. String refval = arg.str();
  166. getNodesByID(xctxt, docContext, refval, null, nodeSet, false);
  167. }
  168. return nodes;
  169. }
  170. }