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