Merge from the pain train
[official-gcc.git] / libjava / gnu / xml / transform / DocumentFunction.java
blob78862dc9e5740285dd0aefebb7aec286608eaaf0
1 /* DocumentFunction.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.transform;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.TreeSet;
46 import javax.xml.transform.TransformerException;
47 import javax.xml.transform.dom.DOMSource;
48 import javax.xml.xpath.XPathFunction;
49 import javax.xml.xpath.XPathFunctionException;
50 import org.w3c.dom.Node;
51 import gnu.xml.xpath.Constant;
52 import gnu.xml.xpath.Expr;
53 import gnu.xml.xpath.Function;
54 import gnu.xml.xpath.IdFunction;
56 /**
57 * The XSLT <code>document()</code>function.
59 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
61 final class DocumentFunction
62 extends Expr
63 implements Function, XPathFunction
66 final Stylesheet stylesheet;
67 final Node base;
68 List args;
69 List values;
71 DocumentFunction(Stylesheet stylesheet, Node base)
73 this.stylesheet = stylesheet;
74 this.base = base;
77 public Object evaluate(List args)
78 throws XPathFunctionException
80 values = args;
81 return evaluate(null, 1, 1);
84 public void setArguments(List args)
86 this.args = args;
89 public Object evaluate(Node context, int pos, int len)
91 int arity = args.size();
92 if (values == null)
94 values = new ArrayList(arity);
95 for (int i = 0; i < arity; i++)
97 Expr arg = (Expr) args.get(i);
98 values.add(arg.evaluate(context, pos, len));
101 Object ret;
102 switch (arity)
104 case 1:
105 Object arg = values.get(0);
106 if (arg instanceof Collection)
108 Collection ns = (Collection) arg;
109 Collection acc = new TreeSet();
110 for (Iterator i = ns.iterator(); i.hasNext(); )
112 Node node = (Node) i.next();
113 String s = Expr.stringValue(node);
114 acc.addAll(document(s, node.getBaseURI()));
116 ret = acc;
118 else
120 String s = Expr._string(context, arg);
121 ret = document(s, base.getBaseURI());
123 break;
124 case 2:
125 Object arg1 = values.get(0);
126 Object arg2 = values.get(1);
127 if (!(arg2 instanceof Collection))
129 throw new RuntimeException("second argument is not a node-set");
131 Collection arg2ns = (Collection) arg2;
132 String base2 = arg2ns.isEmpty() ? null :
133 ((Node) arg2ns.iterator().next()).getBaseURI();
134 if (arg1 instanceof Collection)
136 Collection arg1ns = (Collection) arg1;
137 Collection acc = new TreeSet();
138 for (Iterator i = arg1ns.iterator(); i.hasNext(); )
140 Node node = (Node) i.next();
141 String s = Expr.stringValue(node);
142 acc.addAll(document(s, base2));
144 ret = acc;
146 else
148 String s = Expr._string(context, arg1);
149 ret = document(s, base2);
151 break;
152 default:
153 throw new RuntimeException("invalid arity");
155 values = null;
156 return ret;
160 * The XSL <code>document</code> function.
161 * @see XSLT 12.1
162 * @param uri the URI from which to retrieve nodes
163 * @param base the base URI for relative URIs
165 Collection document(String uri, String base)
167 if ("".equals(uri) || uri == null)
169 uri = this.base.getBaseURI();
172 // Get fragment
173 Expr fragment = null;
174 int hi = uri.indexOf('#');
175 if (hi != -1)
177 String f = uri.substring(hi + 1);
178 uri = uri.substring(0, hi);
179 // TODO handle xpointer() here
180 // this only handles IDs
181 fragment = new IdFunction(new Constant(f));
184 // Get document source
187 DOMSource source;
188 XSLURIResolver resolver = stylesheet.factory.resolver;
189 synchronized (resolver)
191 if (stylesheet.transformer != null)
193 resolver.setUserResolver(stylesheet.transformer.uriResolver);
194 resolver.setUserListener(stylesheet.transformer.errorListener);
196 source = resolver.resolveDOM(null, base, uri);
198 Node node = source.getNode();
199 if (fragment == null)
201 return Collections.singleton(node);
203 else
205 Object ret = fragment.evaluate(node, 1, 1);
206 if (!(ret instanceof Collection))
208 // XXX Report error?
209 return Collections.EMPTY_SET;
211 return (Collection) ret;
214 catch (TransformerException e)
216 String msg = "can't open " + uri;
217 if (base != null)
219 msg += " with base " + base;
221 throw new RuntimeException(msg);
225 public Expr clone(Object context)
227 Stylesheet s = stylesheet;
228 if (context instanceof Stylesheet)
230 s = (Stylesheet) context;
232 DocumentFunction f = new DocumentFunction(s, base);
233 int len = args.size();
234 List args2 = new ArrayList(len);
235 for (int i = 0; i < len; i++)
237 args2.add(((Expr) args.get(i)).clone(context));
239 f.setArguments(args2);
240 return f;