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: ExpressionVisitor.java 468637 2006-10-28 06:51:02Z minchau $
20 */
21package org.apache.xalan.extensions;
22
23import org.apache.xalan.templates.StylesheetRoot;
24import org.apache.xpath.ExpressionOwner;
25import org.apache.xpath.XPathVisitor;
26import org.apache.xpath.functions.FuncExtFunction;
27import org.apache.xpath.functions.FuncExtFunctionAvailable;
28import org.apache.xpath.functions.Function;
29
30/**
31 * When {@link org.apache.xalan.processor.StylesheetHandler} creates
32 * an {@link org.apache.xpath.XPath}, the ExpressionVisitor
33 * visits the XPath expression. For any extension functions it
34 * encounters, it instructs StylesheetRoot to register the
35 * extension namespace.
36 *
37 * This mechanism is required to locate extension functions
38 * that may be embedded within an expression.
39 */
40public class ExpressionVisitor extends XPathVisitor
41{
42  private StylesheetRoot m_sroot;
43
44  /**
45   * The constructor sets the StylesheetRoot variable which
46   * is used to register extension namespaces.
47   * @param sroot the StylesheetRoot that is being constructed.
48   */
49  public ExpressionVisitor (StylesheetRoot sroot)
50  {
51    m_sroot = sroot;
52  }
53
54  /**
55   * If the function is an extension function, register the namespace.
56   *
57   * @param owner The current XPath object that owns the expression.
58   * @param func The function currently being visited.
59   *
60   * @return true to continue the visit in the subtree, if any.
61   */
62  public boolean visitFunction(ExpressionOwner owner, Function func)
63  {
64    if (func instanceof FuncExtFunction)
65    {
66      String namespace = ((FuncExtFunction)func).getNamespace();
67      m_sroot.getExtensionNamespacesManager().registerExtension(namespace);
68    }
69    else if (func instanceof FuncExtFunctionAvailable)
70    {
71      String arg = ((FuncExtFunctionAvailable)func).getArg0().toString();
72      if (arg.indexOf(":") > 0)
73      {
74      	String prefix = arg.substring(0,arg.indexOf(":"));
75      	String namespace = this.m_sroot.getNamespaceForPrefix(prefix);
76      	m_sroot.getExtensionNamespacesManager().registerExtension(namespace);
77      }
78    }
79    return true;
80  }
81
82}
83