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. // $Id: JAXPExtensionsProvider.java,v 1.9 2004/07/10 21:39:19 rameshm Exp $
  17. package com.sun.org.apache.xpath.internal.jaxp;
  18. import javax.xml.transform.TransformerException;
  19. import javax.xml.xpath.XPathFunctionResolver;
  20. import javax.xml.xpath.XPathFunction;
  21. import javax.xml.xpath.XPathFunctionException;
  22. import com.sun.org.apache.xpath.internal.ExtensionsProvider;
  23. import com.sun.org.apache.xpath.internal.XPathContext;
  24. import com.sun.org.apache.xpath.internal.objects.XObject;
  25. import com.sun.org.apache.xpath.internal.objects.XNodeSet;
  26. import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  27. import com.sun.org.apache.xalan.internal.res.XSLMessages;
  28. import com.sun.org.apache.xpath.internal.functions.FuncExtFunction;
  29. import java.util.Vector;
  30. import java.util.ArrayList;
  31. import javax.xml.namespace.QName;
  32. /**
  33. *
  34. * @author Ramesh Mandava ( ramesh.mandava@sun.com )
  35. */
  36. public class JAXPExtensionsProvider implements ExtensionsProvider {
  37. private final XPathFunctionResolver resolver;
  38. private boolean extensionInvocationDisabled = false;
  39. public JAXPExtensionsProvider(XPathFunctionResolver resolver) {
  40. this.resolver = resolver;
  41. this.extensionInvocationDisabled = false;
  42. }
  43. public JAXPExtensionsProvider(XPathFunctionResolver resolver,
  44. boolean featureSecureProcessing ) {
  45. this.resolver = resolver;
  46. this.extensionInvocationDisabled = featureSecureProcessing;
  47. }
  48. /**
  49. * Is the extension function available?
  50. */
  51. public boolean functionAvailable(String ns, String funcName)
  52. throws javax.xml.transform.TransformerException {
  53. try {
  54. if ( funcName == null ) {
  55. String fmsg = XSLMessages.createXPATHMessage(
  56. XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
  57. new Object[] {"Function Name"} );
  58. throw new NullPointerException ( fmsg );
  59. }
  60. //Find the XPathFunction corresponding to namespace and funcName
  61. javax.xml.namespace.QName myQName = new QName( ns, funcName );
  62. javax.xml.xpath.XPathFunction xpathFunction =
  63. resolver.resolveFunction ( myQName, 0 );
  64. if ( xpathFunction == null ) {
  65. return false;
  66. }
  67. return true;
  68. } catch ( Exception e ) {
  69. return false;
  70. }
  71. }
  72. /**
  73. * Is the extension element available?
  74. */
  75. public boolean elementAvailable(String ns, String elemName)
  76. throws javax.xml.transform.TransformerException {
  77. return false;
  78. }
  79. /**
  80. * Execute the extension function.
  81. */
  82. public Object extFunction(String ns, String funcName, Vector argVec,
  83. Object methodKey) throws javax.xml.transform.TransformerException {
  84. try {
  85. if ( funcName == null ) {
  86. String fmsg = XSLMessages.createXPATHMessage(
  87. XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
  88. new Object[] {"Function Name"} );
  89. throw new NullPointerException ( fmsg );
  90. }
  91. //Find the XPathFunction corresponding to namespace and funcName
  92. javax.xml.namespace.QName myQName = new QName( ns, funcName );
  93. // JAXP 1.3 spec says When XMLConstants.FEATURE_SECURE_PROCESSING
  94. // feature is set then invocation of extension functions need to
  95. // throw XPathFunctionException
  96. if ( extensionInvocationDisabled ) {
  97. String fmsg = XSLMessages.createXPATHMessage(
  98. XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED,
  99. new Object[] { myQName.toString() } );
  100. throw new XPathFunctionException ( fmsg );
  101. }
  102. // Assuming user is passing all the needed parameters ( including
  103. // default values )
  104. int arity = argVec.size();
  105. javax.xml.xpath.XPathFunction xpathFunction =
  106. resolver.resolveFunction ( myQName, arity );
  107. // not using methodKey
  108. ArrayList argList = new ArrayList( arity);
  109. for ( int i=0; i<arity; i++ ) {
  110. Object argument = argVec.elementAt( i );
  111. // XNodeSet object() returns NodeVector and not NodeList
  112. // Explicitly getting NodeList by using nodelist()
  113. if ( argument instanceof XNodeSet ) {
  114. argList.add ( i, ((XNodeSet)argument).nodelist() );
  115. } else if ( argument instanceof XObject ) {
  116. Object passedArgument = ((XObject)argument).object();
  117. argList.add ( i, passedArgument );
  118. } else {
  119. argList.add ( i, argument );
  120. }
  121. }
  122. return ( xpathFunction.evaluate ( argList ));
  123. } catch ( XPathFunctionException xfe ) {
  124. // If we get XPathFunctionException then we want to terminate
  125. // further execution by throwing WrappedRuntimeException
  126. throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException ( xfe );
  127. } catch ( Exception e ) {
  128. throw new javax.xml.transform.TransformerException ( e );
  129. }
  130. }
  131. /**
  132. * Execute the extension function.
  133. */
  134. public Object extFunction(FuncExtFunction extFunction,
  135. Vector argVec)
  136. throws javax.xml.transform.TransformerException {
  137. try {
  138. String namespace = extFunction.getNamespace();
  139. String functionName = extFunction.getFunctionName();
  140. int arity = extFunction.getArgCount();
  141. javax.xml.namespace.QName myQName =
  142. new javax.xml.namespace.QName( namespace, functionName );
  143. // JAXP 1.3 spec says When XMLConstants.FEATURE_SECURE_PROCESSING
  144. // feature is set then invocation of extension functions need to
  145. // throw XPathFunctionException
  146. if ( extensionInvocationDisabled ) {
  147. String fmsg = XSLMessages.createXPATHMessage(
  148. XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object[] { myQName.toString() } );
  149. throw new XPathFunctionException ( fmsg );
  150. }
  151. XPathFunction xpathFunction =
  152. resolver.resolveFunction( myQName, arity );
  153. ArrayList argList = new ArrayList( arity);
  154. for ( int i=0; i<arity; i++ ) {
  155. Object argument = argVec.elementAt( i );
  156. // XNodeSet object() returns NodeVector and not NodeList
  157. // Explicitly getting NodeList by using nodelist()
  158. if ( argument instanceof XNodeSet ) {
  159. argList.add ( i, ((XNodeSet)argument).nodelist() );
  160. } else if ( argument instanceof XObject ) {
  161. Object passedArgument = ((XObject)argument).object();
  162. argList.add ( i, passedArgument );
  163. } else {
  164. argList.add ( i, argument );
  165. }
  166. }
  167. return ( xpathFunction.evaluate ( argList ));
  168. } catch ( XPathFunctionException xfe ) {
  169. // If we get XPathFunctionException then we want to terminate
  170. // further execution by throwing WrappedRuntimeException
  171. throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException ( xfe );
  172. } catch ( Exception e ) {
  173. throw new javax.xml.transform.TransformerException ( e );
  174. }
  175. }
  176. }