001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: // $Id: JAXPExtensionsProvider.java,v 1.1 2005/05/17 17:24:26 mkwan Exp $
018: package org.apache.xpath.jaxp;
019:
020: import javax.xml.transform.TransformerException;
021: import javax.xml.xpath.XPathFunctionResolver;
022: import javax.xml.xpath.XPathFunction;
023: import javax.xml.xpath.XPathFunctionException;
024:
025: import org.apache.xpath.ExtensionsProvider;
026: import org.apache.xpath.XPathContext;
027: import org.apache.xpath.objects.XObject;
028: import org.apache.xpath.objects.XNodeSet;
029: import org.apache.xpath.res.XPATHErrorResources;
030: import org.apache.xalan.res.XSLMessages;
031:
032: import org.apache.xpath.functions.FuncExtFunction;
033: import java.util.Vector;
034: import java.util.ArrayList;
035: import javax.xml.namespace.QName;
036:
037: /**
038: *
039: * @author Ramesh Mandava ( ramesh.mandava@sun.com )
040: */
041: public class JAXPExtensionsProvider implements ExtensionsProvider {
042:
043: private final XPathFunctionResolver resolver;
044: private boolean extensionInvocationDisabled = false;
045:
046: public JAXPExtensionsProvider(XPathFunctionResolver resolver) {
047: this .resolver = resolver;
048: this .extensionInvocationDisabled = false;
049: }
050:
051: public JAXPExtensionsProvider(XPathFunctionResolver resolver,
052: boolean featureSecureProcessing) {
053: this .resolver = resolver;
054: this .extensionInvocationDisabled = featureSecureProcessing;
055: }
056:
057: /**
058: * Is the extension function available?
059: */
060:
061: public boolean functionAvailable(String ns, String funcName)
062: throws javax.xml.transform.TransformerException {
063: try {
064: if (funcName == null) {
065: String fmsg = XSLMessages.createXPATHMessage(
066: XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
067: new Object[] { "Function Name" });
068: throw new NullPointerException(fmsg);
069: }
070: //Find the XPathFunction corresponding to namespace and funcName
071: javax.xml.namespace.QName myQName = new QName(ns, funcName);
072: javax.xml.xpath.XPathFunction xpathFunction = resolver
073: .resolveFunction(myQName, 0);
074: if (xpathFunction == null) {
075: return false;
076: }
077: return true;
078: } catch (Exception e) {
079: return false;
080: }
081:
082: }
083:
084: /**
085: * Is the extension element available?
086: */
087: public boolean elementAvailable(String ns, String elemName)
088: throws javax.xml.transform.TransformerException {
089: return false;
090: }
091:
092: /**
093: * Execute the extension function.
094: */
095: public Object extFunction(String ns, String funcName,
096: Vector argVec, Object methodKey)
097: throws javax.xml.transform.TransformerException {
098: try {
099:
100: if (funcName == null) {
101: String fmsg = XSLMessages.createXPATHMessage(
102: XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
103: new Object[] { "Function Name" });
104: throw new NullPointerException(fmsg);
105: }
106: //Find the XPathFunction corresponding to namespace and funcName
107: javax.xml.namespace.QName myQName = new QName(ns, funcName);
108:
109: // JAXP 1.3 spec says When XMLConstants.FEATURE_SECURE_PROCESSING
110: // feature is set then invocation of extension functions need to
111: // throw XPathFunctionException
112: if (extensionInvocationDisabled) {
113: String fmsg = XSLMessages
114: .createXPATHMessage(
115: XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED,
116: new Object[] { myQName.toString() });
117: throw new XPathFunctionException(fmsg);
118: }
119:
120: // Assuming user is passing all the needed parameters ( including
121: // default values )
122: int arity = argVec.size();
123:
124: javax.xml.xpath.XPathFunction xpathFunction = resolver
125: .resolveFunction(myQName, arity);
126:
127: // not using methodKey
128: ArrayList argList = new ArrayList(arity);
129: for (int i = 0; i < arity; i++) {
130: Object argument = argVec.elementAt(i);
131: // XNodeSet object() returns NodeVector and not NodeList
132: // Explicitly getting NodeList by using nodelist()
133: if (argument instanceof XNodeSet) {
134: argList.add(i, ((XNodeSet) argument).nodelist());
135: } else if (argument instanceof XObject) {
136: Object passedArgument = ((XObject) argument)
137: .object();
138: argList.add(i, passedArgument);
139: } else {
140: argList.add(i, argument);
141: }
142: }
143:
144: return (xpathFunction.evaluate(argList));
145: } catch (XPathFunctionException xfe) {
146: // If we get XPathFunctionException then we want to terminate
147: // further execution by throwing WrappedRuntimeException
148: throw new org.apache.xml.utils.WrappedRuntimeException(xfe);
149: } catch (Exception e) {
150: throw new javax.xml.transform.TransformerException(e);
151: }
152:
153: }
154:
155: /**
156: * Execute the extension function.
157: */
158: public Object extFunction(FuncExtFunction extFunction, Vector argVec)
159: throws javax.xml.transform.TransformerException {
160: try {
161: String namespace = extFunction.getNamespace();
162: String functionName = extFunction.getFunctionName();
163: int arity = extFunction.getArgCount();
164: javax.xml.namespace.QName myQName = new javax.xml.namespace.QName(
165: namespace, functionName);
166:
167: // JAXP 1.3 spec says When XMLConstants.FEATURE_SECURE_PROCESSING
168: // feature is set then invocation of extension functions need to
169: // throw XPathFunctionException
170: if (extensionInvocationDisabled) {
171: String fmsg = XSLMessages
172: .createXPATHMessage(
173: XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED,
174: new Object[] { myQName.toString() });
175: throw new XPathFunctionException(fmsg);
176: }
177:
178: XPathFunction xpathFunction = resolver.resolveFunction(
179: myQName, arity);
180:
181: ArrayList argList = new ArrayList(arity);
182: for (int i = 0; i < arity; i++) {
183: Object argument = argVec.elementAt(i);
184: // XNodeSet object() returns NodeVector and not NodeList
185: // Explicitly getting NodeList by using nodelist()
186: if (argument instanceof XNodeSet) {
187: argList.add(i, ((XNodeSet) argument).nodelist());
188: } else if (argument instanceof XObject) {
189: Object passedArgument = ((XObject) argument)
190: .object();
191: argList.add(i, passedArgument);
192: } else {
193: argList.add(i, argument);
194: }
195: }
196:
197: return (xpathFunction.evaluate(argList));
198:
199: } catch (XPathFunctionException xfe) {
200: // If we get XPathFunctionException then we want to terminate
201: // further execution by throwing WrappedRuntimeException
202: throw new org.apache.xml.utils.WrappedRuntimeException(xfe);
203: } catch (Exception e) {
204: throw new javax.xml.transform.TransformerException(e);
205: }
206: }
207:
208: }
|