DocumentFunction.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* DocumentFunction.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.List;
  37. import java.util.TreeSet;
  38. import javax.xml.namespace.QName;
  39. import javax.xml.transform.TransformerException;
  40. import javax.xml.transform.dom.DOMSource;
  41. import javax.xml.xpath.XPathFunction;
  42. import javax.xml.xpath.XPathFunctionException;
  43. import org.w3c.dom.Node;
  44. import gnu.xml.xpath.Constant;
  45. import gnu.xml.xpath.Expr;
  46. import gnu.xml.xpath.Function;
  47. import gnu.xml.xpath.IdFunction;
  48. /**
  49. * The XSLT <code>document()</code>function.
  50. *
  51. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  52. */
  53. final class DocumentFunction
  54. extends Expr
  55. implements Function, XPathFunction
  56. {
  57. final Stylesheet stylesheet;
  58. final Node base;
  59. List args;
  60. List values;
  61. DocumentFunction(Stylesheet stylesheet, Node base)
  62. {
  63. this.stylesheet = stylesheet;
  64. this.base = base;
  65. }
  66. public Object evaluate(List args)
  67. throws XPathFunctionException
  68. {
  69. values = args;
  70. return evaluate(null, 1, 1);
  71. }
  72. public void setArguments(List args)
  73. {
  74. this.args = args;
  75. }
  76. public Object evaluate(Node context, int pos, int len)
  77. {
  78. int arity = args.size();
  79. if (values == null)
  80. {
  81. 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. }
  88. Object ret;
  89. switch (arity)
  90. {
  91. case 1:
  92. Object arg = values.get(0);
  93. if (arg instanceof Collection)
  94. {
  95. Collection ns = (Collection) arg;
  96. Collection acc = new TreeSet();
  97. for (Iterator i = ns.iterator(); i.hasNext(); )
  98. {
  99. Node node = (Node) i.next();
  100. String s = Expr.stringValue(node);
  101. acc.addAll(document(s, node.getBaseURI()));
  102. }
  103. ret = acc;
  104. }
  105. else
  106. {
  107. String s = Expr._string(context, arg);
  108. ret = document(s, base.getBaseURI());
  109. }
  110. break;
  111. case 2:
  112. Object arg1 = values.get(0);
  113. Object arg2 = values.get(1);
  114. if (!(arg2 instanceof Collection))
  115. throw new RuntimeException("second argument is not a node-set");
  116. Collection arg2ns = (Collection) arg2;
  117. String base2 = arg2ns.isEmpty() ? null :
  118. ((Node) arg2ns.iterator().next()).getBaseURI();
  119. if (arg1 instanceof Collection)
  120. {
  121. Collection arg1ns = (Collection) arg1;
  122. Collection acc = new TreeSet();
  123. for (Iterator i = arg1ns.iterator(); i.hasNext(); )
  124. {
  125. Node node = (Node) i.next();
  126. String s = Expr.stringValue(node);
  127. acc.addAll(document(s, base2));
  128. }
  129. ret = acc;
  130. }
  131. else
  132. {
  133. String s = Expr._string(context, arg1);
  134. ret = document(s, base2);
  135. }
  136. break;
  137. default:
  138. throw new RuntimeException("invalid arity");
  139. }
  140. values = null;
  141. return ret;
  142. }
  143. /**
  144. * The XSL <code>document</code> function.
  145. * @see XSLT 12.1
  146. * @param uri the URI from which to retrieve nodes
  147. * @param base the base URI for relative URIs
  148. */
  149. Collection document(String uri, String base)
  150. {
  151. if ("".equals(uri) || uri == null)
  152. uri = this.base.getBaseURI();
  153. // Get fragment
  154. Expr fragment = null;
  155. int hi = uri.indexOf('#');
  156. if (hi != -1)
  157. {
  158. String f = uri.substring(hi + 1);
  159. uri = uri.substring(0, hi);
  160. // TODO handle xpointer() here
  161. // this only handles IDs
  162. fragment = new IdFunction(new Constant(f));
  163. }
  164. // Get document source
  165. try
  166. {
  167. DOMSource source;
  168. XSLURIResolver resolver = stylesheet.factory.resolver;
  169. synchronized (resolver)
  170. {
  171. if (stylesheet.transformer != null)
  172. {
  173. resolver.setUserResolver(stylesheet.transformer.uriResolver);
  174. resolver.setUserListener(stylesheet.transformer.errorListener);
  175. }
  176. source = resolver.resolveDOM(null, base, uri);
  177. }
  178. Node node = source.getNode();
  179. // Strip whitespace
  180. TransformerImpl.strip(stylesheet, node);
  181. if (fragment == null)
  182. return Collections.singleton(node);
  183. else
  184. {
  185. Object ret = fragment.evaluate(node, 1, 1);
  186. if (!(ret instanceof Collection))
  187. {
  188. // XXX Report error?
  189. return Collections.EMPTY_SET;
  190. }
  191. return (Collection) ret;
  192. }
  193. }
  194. catch (TransformerException e)
  195. {
  196. String msg = "can't open " + uri;
  197. if (base != null)
  198. msg += " with base " + base;
  199. throw new RuntimeException(msg);
  200. }
  201. }
  202. public Expr clone(Object context)
  203. {
  204. Stylesheet s = stylesheet;
  205. if (context instanceof Stylesheet)
  206. s = (Stylesheet) context;
  207. DocumentFunction f = new DocumentFunction(s, base);
  208. int len = args.size();
  209. List args2 = new ArrayList(len);
  210. for (int i = 0; i < len; i++)
  211. args2.add(((Expr) args.get(i)).clone(context));
  212. f.setArguments(args2);
  213. return f;
  214. }
  215. public boolean references(QName var)
  216. {
  217. for (Iterator i = args.iterator(); i.hasNext(); )
  218. {
  219. if (((Expr) i.next()).references(var))
  220. return true;
  221. }
  222. return false;
  223. }
  224. }