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.transformer;
  58. import org.apache.xml.dtm.DTM;
  59. import org.apache.xml.dtm.DTMIterator;
  60. import org.apache.xml.utils.NodeVector;
  61. import org.apache.xml.utils.XMLString;
  62. import org.apache.xpath.objects.XNodeSet;
  63. import org.apache.xpath.objects.XObject;
  64. import java.util.Vector;
  65. import org.apache.xml.utils.QName;
  66. import org.apache.xalan.templates.KeyDeclaration;
  67. import org.apache.xalan.res.XSLMessages;
  68. import org.apache.xalan.res.XSLTErrorResources;
  69. /**
  70. * <meta name="usage" content="internal"/>
  71. * This class filters nodes from a key iterator, according to
  72. * whether or not the use value matches the ref value.
  73. */
  74. public class KeyRefIterator extends org.apache.xpath.axes.ChildTestIterator
  75. {
  76. /**
  77. * Constructor KeyRefIterator
  78. *
  79. *
  80. * @param ref Key value to match
  81. * @param ki The main key iterator used to walk the source tree
  82. */
  83. public KeyRefIterator(QName name, XMLString ref, Vector keyDecls, DTMIterator ki)
  84. {
  85. super(null);
  86. m_name = name;
  87. m_ref = ref;
  88. m_keyDeclarations = keyDecls;
  89. m_keysNodes = ki;
  90. setWhatToShow(org.apache.xml.dtm.DTMFilter.SHOW_ALL);
  91. }
  92. DTMIterator m_keysNodes;
  93. /**
  94. * Get the next node via getNextXXX. Bottlenecked for derived class override.
  95. * @return The next node on the axis, or DTM.NULL.
  96. */
  97. protected int getNextNode()
  98. {
  99. int next;
  100. while(DTM.NULL != (next = m_keysNodes.nextNode()))
  101. {
  102. if(DTMIterator.FILTER_ACCEPT == filterNode(next))
  103. break;
  104. }
  105. m_lastFetched = next;
  106. return next;
  107. }
  108. /**
  109. * Test whether a specified node is visible in the logical view of a
  110. * TreeWalker or NodeIterator. This function will be called by the
  111. * implementation of TreeWalker and NodeIterator; it is not intended to
  112. * be called directly from user code.
  113. *
  114. * @param testnode The node to check to see if it passes the filter or not.
  115. *
  116. * @return a constant to determine whether the node is accepted,
  117. * rejected, or skipped, as defined above .
  118. */
  119. public short filterNode(int testNode)
  120. {
  121. boolean foundKey = false;
  122. Vector keys = m_keyDeclarations;
  123. QName name = m_name;
  124. KeyIterator ki = (KeyIterator)(((XNodeSet)m_keysNodes).getContainedIter());
  125. org.apache.xpath.XPathContext xctxt = ki.getXPathContext();
  126. if(null == xctxt)
  127. assertion(false, "xctxt can not be null here!");
  128. try
  129. {
  130. XMLString lookupKey = m_ref;
  131. // System.out.println("lookupKey: "+lookupKey);
  132. int nDeclarations = keys.size();
  133. // Walk through each of the declarations made with xsl:key
  134. for (int i = 0; i < nDeclarations; i++)
  135. {
  136. KeyDeclaration kd = (KeyDeclaration) keys.elementAt(i);
  137. // Only continue if the name on this key declaration
  138. // matches the name on the iterator for this walker.
  139. if (!kd.getName().equals(name))
  140. continue;
  141. foundKey = true;
  142. // xctxt.setNamespaceContext(ki.getPrefixResolver());
  143. // Query from the node, according the the select pattern in the
  144. // use attribute in xsl:key.
  145. XObject xuse = kd.getUse().execute(xctxt, testNode, ki.getPrefixResolver());
  146. if (xuse.getType() != xuse.CLASS_NODESET)
  147. {
  148. XMLString exprResult = xuse.xstr();
  149. if (lookupKey.equals(exprResult))
  150. return DTMIterator.FILTER_ACCEPT;
  151. }
  152. else
  153. {
  154. DTMIterator nl = ((XNodeSet)xuse).iterRaw();
  155. int useNode;
  156. while (DTM.NULL != (useNode = nl.nextNode()))
  157. {
  158. DTM dtm = getDTM(useNode);
  159. XMLString exprResult = dtm.getStringValue(useNode);
  160. if ((null != exprResult) && lookupKey.equals(exprResult))
  161. return DTMIterator.FILTER_ACCEPT;
  162. }
  163. }
  164. } // end for(int i = 0; i < nDeclarations; i++)
  165. }
  166. catch (javax.xml.transform.TransformerException te)
  167. {
  168. throw new org.apache.xml.utils.WrappedRuntimeException(te);
  169. }
  170. if (!foundKey)
  171. throw new RuntimeException(
  172. XSLMessages.createMessage(
  173. XSLTErrorResources.ER_NO_XSLKEY_DECLARATION,
  174. new Object[] { name.getLocalName()}));
  175. return DTMIterator.FILTER_REJECT;
  176. }
  177. protected XMLString m_ref;
  178. protected QName m_name;
  179. /** Vector of Key declarations in the stylesheet.
  180. * @serial */
  181. protected Vector m_keyDeclarations;
  182. }