1 16 17 19 package com.sun.org.apache.xpath.internal.jaxp; 20 21 import javax.xml.transform.TransformerException ; 22 import javax.xml.xpath.XPathFunctionResolver ; 23 import javax.xml.xpath.XPathFunction ; 24 import javax.xml.xpath.XPathFunctionException ; 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 ; 35 import java.util.ArrayList ; 36 import javax.xml.namespace.QName ; 37 38 42 public class JAXPExtensionsProvider implements ExtensionsProvider { 43 44 private final XPathFunctionResolver resolver; 45 private boolean extensionInvocationDisabled = false; 46 47 public JAXPExtensionsProvider(XPathFunctionResolver resolver) { 48 this.resolver = resolver; 49 this.extensionInvocationDisabled = false; 50 } 51 52 public JAXPExtensionsProvider(XPathFunctionResolver resolver, 53 boolean featureSecureProcessing ) { 54 this.resolver = resolver; 55 this.extensionInvocationDisabled = featureSecureProcessing; 56 } 57 58 61 62 public boolean functionAvailable(String ns, String funcName) 63 throws javax.xml.transform.TransformerException { 64 try { 65 if ( funcName == null ) { 66 String fmsg = XSLMessages.createXPATHMessage( 67 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 68 new Object [] {"Function Name"} ); 69 throw new NullPointerException ( fmsg ); 70 } 71 javax.xml.namespace.QName myQName = new QName ( ns, funcName ); 73 javax.xml.xpath.XPathFunction xpathFunction = 74 resolver.resolveFunction ( myQName, 0 ); 75 if ( xpathFunction == null ) { 76 return false; 77 } 78 return true; 79 } catch ( Exception e ) { 80 return false; 81 } 82 83 84 } 85 86 87 90 public boolean elementAvailable(String ns, String elemName) 91 throws javax.xml.transform.TransformerException { 92 return false; 93 } 94 95 98 public Object extFunction(String ns, String funcName, Vector argVec, 99 Object methodKey) throws javax.xml.transform.TransformerException { 100 try { 101 102 if ( funcName == null ) { 103 String fmsg = XSLMessages.createXPATHMessage( 104 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 105 new Object [] {"Function Name"} ); 106 throw new NullPointerException ( fmsg ); 107 } 108 javax.xml.namespace.QName myQName = new QName ( ns, funcName ); 110 111 if ( extensionInvocationDisabled ) { 115 String fmsg = XSLMessages.createXPATHMessage( 116 XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, 117 new Object [] { myQName.toString() } ); 118 throw new XPathFunctionException ( fmsg ); 119 } 120 121 int arity = argVec.size(); 124 125 javax.xml.xpath.XPathFunction xpathFunction = 126 resolver.resolveFunction ( myQName, arity ); 127 128 ArrayList argList = new ArrayList ( arity); 130 for ( int i=0; i<arity; i++ ) { 131 Object argument = argVec.elementAt( i ); 132 if ( argument instanceof XNodeSet ) { 135 argList.add ( i, ((XNodeSet)argument).nodelist() ); 136 } else if ( argument instanceof XObject ) { 137 Object 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 xfe ) { 146 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException ( xfe ); 149 } catch ( Exception e ) { 150 throw new javax.xml.transform.TransformerException ( e ); 151 } 152 153 } 154 155 158 public Object extFunction(FuncExtFunction extFunction, 159 Vector argVec) 160 throws javax.xml.transform.TransformerException { 161 try { 162 String namespace = extFunction.getNamespace(); 163 String functionName = extFunction.getFunctionName(); 164 int arity = extFunction.getArgCount(); 165 javax.xml.namespace.QName myQName = 166 new javax.xml.namespace.QName ( namespace, functionName ); 167 168 if ( extensionInvocationDisabled ) { 172 String fmsg = XSLMessages.createXPATHMessage( 173 XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object [] { myQName.toString() } ); 174 throw new XPathFunctionException ( fmsg ); 175 } 176 177 XPathFunction xpathFunction = 178 resolver.resolveFunction( myQName, arity ); 179 180 ArrayList argList = new ArrayList ( arity); 181 for ( int i=0; i<arity; i++ ) { 182 Object argument = argVec.elementAt( i ); 183 if ( argument instanceof XNodeSet ) { 186 argList.add ( i, ((XNodeSet)argument).nodelist() ); 187 } else if ( argument instanceof XObject ) { 188 Object 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 xfe ) { 198 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException ( xfe ); 201 } catch ( Exception e ) { 202 throw new javax.xml.transform.TransformerException ( e ); 203 } 204 } 205 206 } 207 | Popular Tags |