KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > jaxp > JAXPExtensionsProvider


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 // $Id: JAXPExtensionsProvider.java,v 1.9 2004/07/10 21:39:19 rameshm Exp $
18

19 package com.sun.org.apache.xpath.internal.jaxp;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22 import javax.xml.xpath.XPathFunctionResolver JavaDoc;
23 import javax.xml.xpath.XPathFunction JavaDoc;
24 import javax.xml.xpath.XPathFunctionException JavaDoc;
25
26 import com.sun.org.apache.xpath.internal.ExtensionsProvider;
27 import com.sun.org.apache.xpath.internal.XPathContext;
28 import com.sun.org.apache.xpath.internal.objects.XObject;
29 import com.sun.org.apache.xpath.internal.objects.XNodeSet;
30 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
31 import com.sun.org.apache.xalan.internal.res.XSLMessages;
32
33 import com.sun.org.apache.xpath.internal.functions.FuncExtFunction;
34 import java.util.Vector JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import javax.xml.namespace.QName JavaDoc;
37
38 /**
39  *
40  * @author Ramesh Mandava ( ramesh.mandava@sun.com )
41  */

42 public class JAXPExtensionsProvider implements ExtensionsProvider {
43         
44     private final XPathFunctionResolver JavaDoc resolver;
45     private boolean extensionInvocationDisabled = false;
46     
47     public JAXPExtensionsProvider(XPathFunctionResolver JavaDoc resolver) {
48         this.resolver = resolver;
49         this.extensionInvocationDisabled = false;
50     }
51
52     public JAXPExtensionsProvider(XPathFunctionResolver JavaDoc resolver,
53         boolean featureSecureProcessing ) {
54         this.resolver = resolver;
55         this.extensionInvocationDisabled = featureSecureProcessing;
56     }
57
58     /**
59      * Is the extension function available?
60      */

61
62     public boolean functionAvailable(String JavaDoc ns, String JavaDoc funcName)
63           throws javax.xml.transform.TransformerException JavaDoc {
64       try {
65         if ( funcName == null ) {
66             String JavaDoc fmsg = XSLMessages.createXPATHMessage(
67                 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
68                 new Object JavaDoc[] {"Function Name"} );
69             throw new NullPointerException JavaDoc ( fmsg );
70         }
71         //Find the XPathFunction corresponding to namespace and funcName
72
javax.xml.namespace.QName JavaDoc myQName = new QName JavaDoc( ns, funcName );
73         javax.xml.xpath.XPathFunction JavaDoc xpathFunction =
74             resolver.resolveFunction ( myQName, 0 );
75         if ( xpathFunction == null ) {
76             return false;
77         }
78         return true;
79       } catch ( Exception JavaDoc e ) {
80         return false;
81       }
82        
83
84     }
85         
86
87     /**
88      * Is the extension element available?
89      */

90     public boolean elementAvailable(String JavaDoc ns, String JavaDoc elemName)
91           throws javax.xml.transform.TransformerException JavaDoc {
92         return false;
93     }
94
95     /**
96      * Execute the extension function.
97      */

98     public Object JavaDoc extFunction(String JavaDoc ns, String JavaDoc funcName, Vector JavaDoc argVec,
99         Object JavaDoc methodKey) throws javax.xml.transform.TransformerException JavaDoc {
100         try {
101
102             if ( funcName == null ) {
103                 String JavaDoc fmsg = XSLMessages.createXPATHMessage(
104                     XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
105                     new Object JavaDoc[] {"Function Name"} );
106                 throw new NullPointerException JavaDoc ( fmsg );
107             }
108             //Find the XPathFunction corresponding to namespace and funcName
109
javax.xml.namespace.QName JavaDoc myQName = new QName JavaDoc( ns, funcName );
110
111             // JAXP 1.3 spec says When XMLConstants.FEATURE_SECURE_PROCESSING
112
// feature is set then invocation of extension functions need to
113
// throw XPathFunctionException
114
if ( extensionInvocationDisabled ) {
115                 String JavaDoc fmsg = XSLMessages.createXPATHMessage(
116                     XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED,
117                     new Object JavaDoc[] { myQName.toString() } );
118                 throw new XPathFunctionException JavaDoc ( fmsg );
119             }
120
121             // Assuming user is passing all the needed parameters ( including
122
// default values )
123
int arity = argVec.size();
124
125             javax.xml.xpath.XPathFunction JavaDoc xpathFunction =
126                 resolver.resolveFunction ( myQName, arity );
127
128             // not using methodKey
129
ArrayList JavaDoc argList = new ArrayList JavaDoc( arity);
130             for ( int i=0; i<arity; i++ ) {
131                 Object JavaDoc argument = argVec.elementAt( i );
132                 // XNodeSet object() returns NodeVector and not NodeList
133
// Explicitly getting NodeList by using nodelist()
134
if ( argument instanceof XNodeSet ) {
135                     argList.add ( i, ((XNodeSet)argument).nodelist() );
136                 } else if ( argument instanceof XObject ) {
137                     Object JavaDoc passedArgument = ((XObject)argument).object();
138                     argList.add ( i, passedArgument );
139                 } else {
140                     argList.add ( i, argument );
141                 }
142             }
143
144             return ( xpathFunction.evaluate ( argList ));
145         } catch ( XPathFunctionException JavaDoc xfe ) {
146             // If we get XPathFunctionException then we want to terminate
147
// further execution by throwing WrappedRuntimeException
148
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException ( xfe );
149         } catch ( Exception JavaDoc e ) {
150             throw new javax.xml.transform.TransformerException JavaDoc ( e );
151         }
152     
153     }
154
155     /**
156      * Execute the extension function.
157      */

158     public Object JavaDoc extFunction(FuncExtFunction extFunction,
159                               Vector JavaDoc argVec)
160         throws javax.xml.transform.TransformerException JavaDoc {
161         try {
162             String JavaDoc namespace = extFunction.getNamespace();
163             String JavaDoc functionName = extFunction.getFunctionName();
164             int arity = extFunction.getArgCount();
165             javax.xml.namespace.QName JavaDoc myQName =
166                 new javax.xml.namespace.QName JavaDoc( namespace, functionName );
167
168             // JAXP 1.3 spec says When XMLConstants.FEATURE_SECURE_PROCESSING
169
// feature is set then invocation of extension functions need to
170
// throw XPathFunctionException
171
if ( extensionInvocationDisabled ) {
172                 String JavaDoc fmsg = XSLMessages.createXPATHMessage(
173                     XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object JavaDoc[] { myQName.toString() } );
174                 throw new XPathFunctionException JavaDoc ( fmsg );
175             }
176
177             XPathFunction JavaDoc xpathFunction =
178                 resolver.resolveFunction( myQName, arity );
179
180             ArrayList JavaDoc argList = new ArrayList JavaDoc( arity);
181             for ( int i=0; i<arity; i++ ) {
182                 Object JavaDoc argument = argVec.elementAt( i );
183                 // XNodeSet object() returns NodeVector and not NodeList
184
// Explicitly getting NodeList by using nodelist()
185
if ( argument instanceof XNodeSet ) {
186                     argList.add ( i, ((XNodeSet)argument).nodelist() );
187                 } else if ( argument instanceof XObject ) {
188                     Object JavaDoc passedArgument = ((XObject)argument).object();
189                     argList.add ( i, passedArgument );
190                 } else {
191                     argList.add ( i, argument );
192                 }
193             }
194        
195             return ( xpathFunction.evaluate ( argList ));
196
197         } catch ( XPathFunctionException JavaDoc xfe ) {
198             // If we get XPathFunctionException then we want to terminate
199
// further execution by throwing WrappedRuntimeException
200
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException ( xfe );
201         } catch ( Exception JavaDoc e ) {
202             throw new javax.xml.transform.TransformerException JavaDoc ( e );
203         }
204     }
205
206 }
207
Popular Tags