01: /*
02: * Copyright 1999-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.xalan.extensions;
18:
19: import java.util.List;
20: import java.util.Vector;
21:
22: import javax.xml.transform.TransformerException;
23: import javax.xml.xpath.XPathFunction;
24: import javax.xml.xpath.XPathFunctionException;
25:
26: /**
27: * A sample implementation of XPathFunction, with support for
28: * EXSLT extension functions and Java extension functions.
29: */
30: public class XPathFunctionImpl implements XPathFunction {
31: private ExtensionHandler m_handler;
32: private String m_funcName;
33:
34: /**
35: * Construct an instance of XPathFunctionImpl from the
36: * ExtensionHandler and function name.
37: */
38: public XPathFunctionImpl(ExtensionHandler handler, String funcName) {
39: m_handler = handler;
40: m_funcName = funcName;
41: }
42:
43: /**
44: * @see javax.xml.xpath.XPathFunction#evaluate(java.util.List)
45: */
46: public Object evaluate(List args) throws XPathFunctionException {
47: Vector argsVec = listToVector(args);
48:
49: try {
50: // The method key and ExpressionContext are set to null.
51: return m_handler.callFunction(m_funcName, argsVec, null,
52: null);
53: } catch (TransformerException e) {
54: throw new XPathFunctionException(e);
55: }
56: }
57:
58: /**
59: * Convert a java.util.List to a java.util.Vector.
60: * No conversion is done if the List is already a Vector.
61: */
62: private static Vector listToVector(List args) {
63: if (args == null)
64: return null;
65: else if (args instanceof Vector)
66: return (Vector) args;
67: else {
68: Vector result = new Vector();
69: result.addAll(args);
70: return result;
71: }
72: }
73: }
|