01: /*
02: * Copyright 1999-2004 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: FuncExtElementAvailable.java,v 1.13 2004/08/17 19:25:36 jycli Exp $
18: */
19: package org.apache.xpath.functions;
20:
21: import org.apache.xalan.templates.Constants;
22: import org.apache.xalan.transformer.TransformerImpl;
23: import org.apache.xml.utils.QName;
24: import org.apache.xpath.ExtensionsProvider;
25: import org.apache.xpath.XPathContext;
26: import org.apache.xpath.objects.XBoolean;
27: import org.apache.xpath.objects.XObject;
28:
29: /**
30: * Execute the ExtElementAvailable() function.
31: * @xsl.usage advanced
32: */
33: public class FuncExtElementAvailable extends FunctionOneArg {
34: static final long serialVersionUID = -472533699257968546L;
35:
36: /**
37: * Execute the function. The function must return
38: * a valid object.
39: * @param xctxt The current execution context.
40: * @return A valid XObject.
41: *
42: * @throws javax.xml.transform.TransformerException
43: */
44: public XObject execute(XPathContext xctxt)
45: throws javax.xml.transform.TransformerException {
46:
47: String prefix;
48: String namespace;
49: String methName;
50:
51: String fullName = m_arg0.execute(xctxt).str();
52: int indexOfNSSep = fullName.indexOf(':');
53:
54: if (indexOfNSSep < 0) {
55: prefix = "";
56: namespace = Constants.S_XSLNAMESPACEURL;
57: methName = fullName;
58: } else {
59: prefix = fullName.substring(0, indexOfNSSep);
60: namespace = xctxt.getNamespaceContext()
61: .getNamespaceForPrefix(prefix);
62: if (null == namespace)
63: return XBoolean.S_FALSE;
64: methName = fullName.substring(indexOfNSSep + 1);
65: }
66:
67: if (namespace.equals(Constants.S_XSLNAMESPACEURL)
68: || namespace.equals(Constants.S_BUILTIN_EXTENSIONS_URL)) {
69: try {
70: TransformerImpl transformer = (TransformerImpl) xctxt
71: .getOwnerObject();
72: return transformer.getStylesheet()
73: .getAvailableElements().containsKey(
74: new QName(namespace, methName)) ? XBoolean.S_TRUE
75: : XBoolean.S_FALSE;
76: } catch (Exception e) {
77: return XBoolean.S_FALSE;
78: }
79: } else {
80: //dml
81: ExtensionsProvider extProvider = (ExtensionsProvider) xctxt
82: .getOwnerObject();
83: return extProvider.elementAvailable(namespace, methName) ? XBoolean.S_TRUE
84: : XBoolean.S_FALSE;
85: }
86: }
87: }
|