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: ExpressionVisitor.java 468637 2006-10-28 06:51:02Z minchau $
020     */
021    package org.apache.xalan.extensions;
022    
023    import org.apache.xalan.templates.StylesheetRoot;
024    import org.apache.xpath.ExpressionOwner;
025    import org.apache.xpath.XPathVisitor;
026    import org.apache.xpath.functions.FuncExtFunction;
027    import org.apache.xpath.functions.FuncExtFunctionAvailable;
028    import org.apache.xpath.functions.Function;
029    
030    /**
031     * When {@link org.apache.xalan.processor.StylesheetHandler} creates 
032     * an {@link org.apache.xpath.XPath}, the ExpressionVisitor
033     * visits the XPath expression. For any extension functions it 
034     * encounters, it instructs StylesheetRoot to register the
035     * extension namespace. 
036     * 
037     * This mechanism is required to locate extension functions
038     * that may be embedded within an expression.
039     */
040    public class ExpressionVisitor extends XPathVisitor
041    {
042      private StylesheetRoot m_sroot;
043      
044      /**
045       * The constructor sets the StylesheetRoot variable which
046       * is used to register extension namespaces.
047       * @param sroot the StylesheetRoot that is being constructed.
048       */
049      public ExpressionVisitor (StylesheetRoot sroot)
050      {
051        m_sroot = sroot;
052      }
053      
054      /**
055       * If the function is an extension function, register the namespace.
056       * 
057       * @param owner The current XPath object that owns the expression.
058       * @param func The function currently being visited.
059       * 
060       * @return true to continue the visit in the subtree, if any.
061       */
062      public boolean visitFunction(ExpressionOwner owner, Function func)
063      {
064        if (func instanceof FuncExtFunction)
065        {
066          String namespace = ((FuncExtFunction)func).getNamespace();
067          m_sroot.getExtensionNamespacesManager().registerExtension(namespace);      
068        }
069        else if (func instanceof FuncExtFunctionAvailable)
070        {
071          String arg = ((FuncExtFunctionAvailable)func).getArg0().toString();
072          if (arg.indexOf(":") > 0)
073          {
074            String prefix = arg.substring(0,arg.indexOf(":"));
075            String namespace = this.m_sroot.getNamespaceForPrefix(prefix);
076            m_sroot.getExtensionNamespacesManager().registerExtension(namespace);
077          }
078        }
079        return true;
080      }
081    
082    }