Merge from mainline
[official-gcc.git] / libjava / classpath / gnu / xml / transform / FunctionAvailableFunction.java
blobab86401d0e4981798535575eb6a19dca321ca2c4
1 /* FunctionAvailableFunction.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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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.namespace.NamespaceContext;
47 import javax.xml.namespace.QName;
48 import javax.xml.xpath.XPathFunction;
49 import javax.xml.xpath.XPathFunctionException;
50 import org.w3c.dom.Node;
51 import gnu.xml.xpath.Expr;
52 import gnu.xml.xpath.Function;
54 /**
55 * The XSLT <code>function-available</code> function.
57 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
59 class FunctionAvailableFunction
60 extends Expr
61 implements Function, XPathFunction
64 static final Collection xsltFunctions;
65 static final Collection xpathFunctions;
66 static
68 TreeSet acc = new TreeSet();
69 acc.add("document");
70 acc.add("key");
71 acc.add("format-number");
72 acc.add("current");
73 acc.add("unparsed-entity-uri");
74 acc.add("generate-id");
75 acc.add("system-property");
76 acc.add("element-available");
77 acc.add("function-available");
78 xsltFunctions = Collections.unmodifiableSet(acc);
79 acc = new TreeSet();
80 acc.add("boolean");
81 acc.add("ceiling");
82 acc.add("concat");
83 acc.add("contains");
84 acc.add("count");
85 acc.add("false");
86 acc.add("floor");
87 acc.add("id");
88 acc.add("lang");
89 acc.add("last");
90 acc.add("local-name");
91 acc.add("name");
92 acc.add("namespace-uri");
93 acc.add("normalize-space");
94 acc.add("not");
95 acc.add("number");
96 acc.add("position");
97 acc.add("round");
98 acc.add("starts-with");
99 acc.add("string");
100 acc.add("string-length");
101 acc.add("substring-after");
102 acc.add("substring-before");
103 acc.add("substring");
104 acc.add("sum");
105 acc.add("translate");
106 acc.add("true");
107 xpathFunctions = Collections.unmodifiableSet(acc);
110 final NamespaceContext nsctx;
111 List args;
113 FunctionAvailableFunction(NamespaceContext nsctx)
115 this.nsctx = nsctx;
118 public Object evaluate(List args)
119 throws XPathFunctionException
121 // Useless...
122 return Collections.EMPTY_SET;
125 public void setArguments(List args)
127 this.args = args;
130 public Object evaluate(Node context, int pos, int len)
132 Expr arg = (Expr) args.get(0);
133 Object val = arg.evaluate(context, pos, len);
134 String name = _string(context, val);
135 String prefix, localName, uri;
136 int ci = name.indexOf(':');
137 if (ci == -1)
139 prefix = null;
140 localName = name;
142 else
144 prefix = name.substring(0, ci);
145 localName = name.substring(ci + 1);
147 uri = nsctx.getNamespaceURI(prefix);
148 if (uri == null)
150 return (xpathFunctions.contains(localName) ||
151 xsltFunctions.contains(localName)) ?
152 Boolean.TRUE : Boolean.FALSE;
154 else if (Stylesheet.XSL_NS.equals(uri))
156 return xsltFunctions.contains(localName) ?
157 Boolean.TRUE : Boolean.FALSE;
159 // TODO extension functions
160 return Boolean.FALSE;
163 public Expr clone(Object context)
165 NamespaceContext n = nsctx;
166 if (context instanceof NamespaceContext)
167 n = (NamespaceContext) context;
168 FunctionAvailableFunction f = new FunctionAvailableFunction(n);
169 int len = args.size();
170 List args2 = new ArrayList(len);
171 for (int i = 0; i < len; i++)
172 args2.add(((Expr) args.get(i)).clone(context));
173 f.setArguments(args2);
174 return f;
177 public boolean references(QName var)
179 for (Iterator i = args.iterator(); i.hasNext(); )
181 if (((Expr) i.next()).references(var))
182 return true;
184 return false;
187 public String toString()
189 return "function-available(" + args.get(0) + ")";