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