KeyFunction.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* KeyFunction.java --
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.transform;
  32. import java.util.ArrayList;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.Iterator;
  36. import java.util.LinkedHashSet;
  37. import java.util.LinkedList;
  38. import java.util.List;
  39. import javax.xml.namespace.QName;
  40. import javax.xml.xpath.XPathFunction;
  41. import javax.xml.xpath.XPathFunctionException;
  42. import org.w3c.dom.Document;
  43. import org.w3c.dom.Node;
  44. import gnu.xml.xpath.Expr;
  45. import gnu.xml.xpath.Function;
  46. import gnu.xml.xpath.Pattern;
  47. /**
  48. * The XSLT <code>key()</code>function.
  49. *
  50. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  51. */
  52. final class KeyFunction
  53. extends Pattern
  54. implements XPathFunction, Function
  55. {
  56. final Stylesheet stylesheet;
  57. List args;
  58. KeyFunction(Stylesheet stylesheet)
  59. {
  60. this.stylesheet = stylesheet;
  61. }
  62. public Object evaluate(List args)
  63. throws XPathFunctionException
  64. {
  65. // Useless...
  66. return Collections.EMPTY_SET;
  67. }
  68. public void setArguments(List args)
  69. {
  70. this.args = args;
  71. }
  72. public boolean matches(Node context)
  73. {
  74. Object ret = evaluate(context, 1, 1);
  75. return !((Collection) ret).isEmpty();
  76. }
  77. public Object evaluate(Node context, int pos, int len)
  78. {
  79. // Evaluate arguments
  80. int arity = args.size();
  81. List values = new ArrayList(arity);
  82. for (int i = 0; i < arity; i++)
  83. {
  84. Expr arg = (Expr) args.get(i);
  85. values.add(arg.evaluate(context, pos, len));
  86. }
  87. // Get key name
  88. QName keyName = QName.valueOf(_string(context, values.get(0)));
  89. // Expand qualified name
  90. String uri = keyName.getNamespaceURI();
  91. String prefix = keyName.getPrefix();
  92. if ((uri == null || uri.length() == 0) &&
  93. (prefix != null && prefix.length() > 0))
  94. {
  95. uri = stylesheet.getNamespaceURI(prefix);
  96. if (uri != null && uri.length() > 0)
  97. {
  98. String localName = keyName.getLocalPart();
  99. keyName = new QName(uri, localName, prefix);
  100. }
  101. }
  102. // Compute matching key set
  103. Collection keySet = new LinkedList();
  104. for (Iterator i = stylesheet.keys.iterator(); i.hasNext(); )
  105. {
  106. Key key = (Key) i.next();
  107. if (key.name.equals(keyName))
  108. {
  109. keySet.add(key);
  110. }
  111. }
  112. // Get target
  113. Object target = values.get(1);
  114. Collection acc = new LinkedHashSet();
  115. Document doc = (context instanceof Document) ? (Document) context :
  116. context.getOwnerDocument();
  117. if (target instanceof Collection)
  118. {
  119. for (Iterator i = ((Collection) target).iterator(); i.hasNext(); )
  120. {
  121. String val = Expr.stringValue((Node) i.next());
  122. addKeyNodes(doc, keySet, val, acc);
  123. }
  124. }
  125. else
  126. {
  127. String val = Expr._string(context, target);
  128. addKeyNodes(doc, keySet, val, acc);
  129. }
  130. List ret = new ArrayList(acc);
  131. Collections.sort(ret, documentOrderComparator);
  132. return ret;
  133. }
  134. final void addKeyNodes(Node node, Collection keySet,
  135. String value, Collection acc)
  136. {
  137. addKeyNodeIfMatch(node, keySet, value, acc);
  138. // Apply children
  139. for (Node ctx = node.getFirstChild(); ctx != null;
  140. ctx = ctx.getNextSibling())
  141. {
  142. addKeyNodes(ctx, keySet, value, acc);
  143. }
  144. }
  145. final void addKeyNodeIfMatch(Node node, Collection keySet,
  146. String value, Collection acc)
  147. {
  148. for (Iterator i = keySet.iterator(); i.hasNext(); )
  149. {
  150. Key key = (Key) i.next();
  151. if (key.match.matches(node))
  152. {
  153. Object eval = key.use.evaluate(node, 1, 1);
  154. if (eval instanceof Collection)
  155. {
  156. for (Iterator j = ((Collection) eval).iterator();
  157. j.hasNext(); )
  158. {
  159. String keyValue = Expr.stringValue((Node) j.next());
  160. if (value.equals(keyValue))
  161. {
  162. acc.add(node);
  163. return;
  164. }
  165. }
  166. }
  167. else
  168. {
  169. String keyValue = Expr._string(node, eval);
  170. if (value.equals(keyValue))
  171. {
  172. acc.add(node);
  173. return;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. public Expr clone(Object context)
  180. {
  181. Stylesheet s = stylesheet;
  182. if (context instanceof Stylesheet)
  183. {
  184. s = (Stylesheet) context;
  185. }
  186. KeyFunction f = new KeyFunction(s);
  187. int len = args.size();
  188. List args2 = new ArrayList(len);
  189. for (int i = 0; i < len; i++)
  190. {
  191. args2.add(((Expr) args.get(i)).clone(context));
  192. }
  193. f.setArguments(args2);
  194. return f;
  195. }
  196. public boolean references(QName var)
  197. {
  198. for (Iterator i = args.iterator(); i.hasNext(); )
  199. {
  200. if (((Expr) i.next()).references(var))
  201. {
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. }