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: FuncExtElementAvailable.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.xalan.transformer.TransformerImpl;
025    import org.apache.xml.utils.QName;
026    import org.apache.xpath.ExtensionsProvider;
027    import org.apache.xpath.XPathContext;
028    import org.apache.xpath.objects.XBoolean;
029    import org.apache.xpath.objects.XObject;
030    
031    /**
032     * Execute the ExtElementAvailable() function.
033     * @xsl.usage advanced
034     */
035    public class FuncExtElementAvailable extends FunctionOneArg
036    {
037        static final long serialVersionUID = -472533699257968546L;
038    
039      /**
040       * Execute the function.  The function must return
041       * a valid object.
042       * @param xctxt The current execution context.
043       * @return A valid XObject.
044       *
045       * @throws javax.xml.transform.TransformerException
046       */
047      public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
048      {
049    
050        String prefix;
051        String namespace;
052        String methName;
053    
054        String fullName = m_arg0.execute(xctxt).str();
055        int indexOfNSSep = fullName.indexOf(':');
056    
057        if (indexOfNSSep < 0)
058        {
059          prefix = "";
060          namespace = Constants.S_XSLNAMESPACEURL;
061          methName = fullName;
062        }
063        else
064        {
065          prefix = fullName.substring(0, indexOfNSSep);
066          namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
067          if (null == namespace)
068            return XBoolean.S_FALSE;
069          methName= fullName.substring(indexOfNSSep + 1);
070        }
071    
072        if (namespace.equals(Constants.S_XSLNAMESPACEURL)
073        ||  namespace.equals(Constants.S_BUILTIN_EXTENSIONS_URL))
074        {
075          try
076          {
077            TransformerImpl transformer = (TransformerImpl) xctxt.getOwnerObject();
078            return transformer.getStylesheet().getAvailableElements().containsKey(
079                                                                new QName(namespace, methName))
080                   ? XBoolean.S_TRUE : XBoolean.S_FALSE;
081          }
082          catch (Exception e)
083          {
084            return XBoolean.S_FALSE;
085          }
086        }
087        else
088        {
089          //dml
090          ExtensionsProvider extProvider = (ExtensionsProvider)xctxt.getOwnerObject();
091          return extProvider.elementAvailable(namespace, methName)
092                 ? XBoolean.S_TRUE : XBoolean.S_FALSE;
093        }
094      }
095    }