01: /*
02: * Copyright 1999-2005 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: FuncExtFunctionAvailable.java,v 1.13 2005/05/17 21:25:47 jycli Exp $
18: */
19: package org.apache.xpath.functions;
20:
21: import org.apache.xalan.templates.Constants;
22: import org.apache.xpath.ExtensionsProvider;
23: import org.apache.xpath.XPathContext;
24: import org.apache.xpath.compiler.FunctionTable;
25: import org.apache.xpath.objects.XBoolean;
26: import org.apache.xpath.objects.XObject;
27:
28: /**
29: * Execute the ExtFunctionAvailable() function.
30: * @xsl.usage advanced
31: */
32: public class FuncExtFunctionAvailable extends FunctionOneArg {
33: static final long serialVersionUID = 5118814314918592241L;
34:
35: transient private FunctionTable m_functionTable = null;
36:
37: /**
38: * Execute the function. The function must return
39: * a valid object.
40: * @param xctxt The current execution context.
41: * @return A valid XObject.
42: *
43: * @throws javax.xml.transform.TransformerException
44: */
45: public XObject execute(XPathContext xctxt)
46: throws javax.xml.transform.TransformerException {
47:
48: String prefix;
49: String namespace;
50: String methName;
51:
52: String fullName = m_arg0.execute(xctxt).str();
53: int indexOfNSSep = fullName.indexOf(':');
54:
55: if (indexOfNSSep < 0) {
56: prefix = "";
57: namespace = Constants.S_XSLNAMESPACEURL;
58: methName = fullName;
59: } else {
60: prefix = fullName.substring(0, indexOfNSSep);
61: namespace = xctxt.getNamespaceContext()
62: .getNamespaceForPrefix(prefix);
63: if (null == namespace)
64: return XBoolean.S_FALSE;
65: methName = fullName.substring(indexOfNSSep + 1);
66: }
67:
68: if (namespace.equals(Constants.S_XSLNAMESPACEURL)) {
69: try {
70: if (null == m_functionTable)
71: m_functionTable = new FunctionTable();
72: return m_functionTable.functionAvailable(methName) ? XBoolean.S_TRUE
73: : XBoolean.S_FALSE;
74: } catch (Exception e) {
75: return XBoolean.S_FALSE;
76: }
77: } else {
78: //dml
79: ExtensionsProvider extProvider = (ExtensionsProvider) xctxt
80: .getOwnerObject();
81: return extProvider.functionAvailable(namespace, methName) ? XBoolean.S_TRUE
82: : XBoolean.S_FALSE;
83: }
84: }
85:
86: /**
87: * The function table is an instance field. In order to access this instance
88: * field during evaluation, this method is called at compilation time to
89: * insert function table information for later usage. It should only be used
90: * during compiling of XPath expressions.
91: * @param aTable an instance of the function table
92: */
93: public void setFunctionTable(FunctionTable aTable) {
94: m_functionTable = aTable;
95: }
96: }
|