1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id: FuncExtFunctionAvailable.java 468655 2006-10-28 07:12:06Z minchau $
20  */
21 package org.apache.xpath.functions;
22 
23 import org.apache.xalan.templates.Constants;
24 import org.apache.xpath.ExtensionsProvider;
25 import org.apache.xpath.XPathContext;
26 import org.apache.xpath.compiler.FunctionTable;
27 import org.apache.xpath.objects.XBoolean;
28 import org.apache.xpath.objects.XObject;
29 
30 /**
31  * Execute the ExtFunctionAvailable() function.
32  * @xsl.usage advanced
33  */
34 public class FuncExtFunctionAvailable extends FunctionOneArg
35 {
36     static final long serialVersionUID = 5118814314918592241L;
37 
38     transient private FunctionTable m_functionTable = null;
39 
40   /**
41    * Execute the function.  The function must return
42    * a valid object.
43    * @param xctxt The current execution context.
44    * @return A valid XObject.
45    *
46    * @throws javax.xml.transform.TransformerException
47    */
execute(XPathContext xctxt)48   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
49   {
50 
51     String prefix;
52     String namespace;
53     String methName;
54 
55     String fullName = m_arg0.execute(xctxt).str();
56     int indexOfNSSep = fullName.indexOf(':');
57 
58     if (indexOfNSSep < 0)
59     {
60       prefix = "";
61       namespace = Constants.S_XSLNAMESPACEURL;
62       methName = fullName;
63     }
64     else
65     {
66       prefix = fullName.substring(0, indexOfNSSep);
67       namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
68       if (null == namespace)
69         return XBoolean.S_FALSE;
70         methName = fullName.substring(indexOfNSSep + 1);
71     }
72 
73     if (namespace.equals(Constants.S_XSLNAMESPACEURL))
74     {
75       try
76       {
77         if (null == m_functionTable) m_functionTable = new FunctionTable();
78         return m_functionTable.functionAvailable(methName) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
79       }
80       catch (Exception e)
81       {
82         return XBoolean.S_FALSE;
83       }
84     }
85     else
86     {
87       //dml
88       ExtensionsProvider extProvider = (ExtensionsProvider)xctxt.getOwnerObject();
89       return extProvider.functionAvailable(namespace, methName)
90              ? XBoolean.S_TRUE : XBoolean.S_FALSE;
91     }
92   }
93 
94   /**
95    * The function table is an instance field. In order to access this instance
96    * field during evaluation, this method is called at compilation time to
97    * insert function table information for later usage. It should only be used
98    * during compiling of XPath expressions.
99    * @param aTable an instance of the function table
100    */
setFunctionTable(FunctionTable aTable)101   public void setFunctionTable(FunctionTable aTable){
102           m_functionTable = aTable;
103   }
104 }
105