001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: FuncExtFunctionAvailable.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xpath.functions;
022    
023    import org.apache.xalan.templates.Constants;
024    import org.apache.xpath.ExtensionsProvider;
025    import org.apache.xpath.XPathContext;
026    import org.apache.xpath.compiler.FunctionTable;
027    import org.apache.xpath.objects.XBoolean;
028    import org.apache.xpath.objects.XObject;
029    
030    /**
031     * Execute the ExtFunctionAvailable() function.
032     * @xsl.usage advanced
033     */
034    public class FuncExtFunctionAvailable extends FunctionOneArg
035    {
036        static final long serialVersionUID = 5118814314918592241L;
037        
038        transient private FunctionTable m_functionTable = null;
039    
040      /**
041       * Execute the function.  The function must return
042       * a valid object.
043       * @param xctxt The current execution context.
044       * @return A valid XObject.
045       *
046       * @throws javax.xml.transform.TransformerException
047       */
048      public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
049      {
050    
051        String prefix;
052        String namespace;
053        String methName;
054    
055        String fullName = m_arg0.execute(xctxt).str();
056        int indexOfNSSep = fullName.indexOf(':');
057    
058        if (indexOfNSSep < 0)
059        {
060          prefix = "";
061          namespace = Constants.S_XSLNAMESPACEURL;
062          methName = fullName;
063        }
064        else
065        {
066          prefix = fullName.substring(0, indexOfNSSep);
067          namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
068          if (null == namespace)
069            return XBoolean.S_FALSE;
070            methName = fullName.substring(indexOfNSSep + 1);
071        }
072    
073        if (namespace.equals(Constants.S_XSLNAMESPACEURL))
074        {
075          try
076          {
077            if (null == m_functionTable) m_functionTable = new FunctionTable();
078            return m_functionTable.functionAvailable(methName) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
079          }
080          catch (Exception e)
081          {
082            return XBoolean.S_FALSE;
083          }
084        }
085        else
086        {
087          //dml
088          ExtensionsProvider extProvider = (ExtensionsProvider)xctxt.getOwnerObject();
089          return extProvider.functionAvailable(namespace, methName)
090                 ? XBoolean.S_TRUE : XBoolean.S_FALSE;
091        }
092      }
093      
094      /**
095       * The function table is an instance field. In order to access this instance 
096       * field during evaluation, this method is called at compilation time to
097       * insert function table information for later usage. It should only be used
098       * during compiling of XPath expressions.
099       * @param aTable an instance of the function table
100       */
101      public void setFunctionTable(FunctionTable aTable){
102              m_functionTable = aTable;
103      }
104    }