1 /* ElementAvailableFunction.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)
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
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
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
.QName
;
47 import javax
.xml
.namespace
.NamespaceContext
;
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
;
55 * The XSLT <code>element-available</code> function.
57 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
59 class ElementAvailableFunction
61 implements Function
, XPathFunction
64 static final Collection elements
;
67 TreeSet acc
= new TreeSet();
68 acc
.add("stylesheet");
75 acc
.add("preserve-space");
76 acc
.add("strip-space");
78 acc
.add("decimal-format");
79 acc
.add("namespace-alias");
80 acc
.add("attribute-set");
81 acc
.add("apply-templates");
82 acc
.add("call-template");
93 acc
.add("processing-instruction");
99 acc
.add("with-param");
101 acc
.add("apply-imports");
102 elements
= Collections
.unmodifiableSet(acc
);
105 final NamespaceContext nsctx
;
108 ElementAvailableFunction(NamespaceContext nsctx
)
113 public Object
evaluate(List args
)
114 throws XPathFunctionException
117 return Collections
.EMPTY_SET
;
120 public void setArguments(List args
)
125 public Object
evaluate(Node context
, int pos
, int len
)
127 Expr arg
= (Expr
) args
.get(0);
128 Object val
= arg
.evaluate(context
, pos
, len
);
129 String name
= _string(context
, val
);
130 String prefix
, localName
, uri
;
131 int ci
= name
.indexOf(':');
139 prefix
= name
.substring(0, ci
);
140 localName
= name
.substring(ci
+ 1);
142 uri
= nsctx
.getNamespaceURI(prefix
);
143 if (Stylesheet
.XSL_NS
.equals(uri
))
145 return elements
.contains(localName
) ?
146 Boolean
.TRUE
: Boolean
.FALSE
;
148 // TODO extension elements
149 return Boolean
.FALSE
;
152 public Expr
clone(Object context
)
154 NamespaceContext n
= nsctx
;
155 if (context
instanceof NamespaceContext
)
156 n
= (NamespaceContext
) context
;
157 ElementAvailableFunction f
= new ElementAvailableFunction(n
);
158 int len
= args
.size();
159 List args2
= new ArrayList(len
);
160 for (int i
= 0; i
< len
; i
++)
161 args2
.add(((Expr
) args
.get(i
)).clone(context
));
162 f
.setArguments(args2
);
166 public boolean references(QName var
)
168 for (Iterator i
= args
.iterator(); i
.hasNext(); )
170 if (((Expr
) i
.next()).references(var
))
176 public String
toString()
178 return "element-available(" + args
.get(0) + ")";