1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xpath.internal.compiler;
23
24import javax.xml.transform.TransformerException;
25
26import com.sun.org.apache.xpath.internal.functions.Function;
27import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
28import com.sun.org.apache.xalan.internal.utils.ConfigurationError;
29
30/**
31 * Lazy load of functions into the function table as needed, so we don't
32 * have to load all the functions allowed in XPath and XSLT on startup.
33 * @xsl.usage advanced
34 */
35public class FuncLoader
36{
37
38  /** The function ID, which may correspond to one of the FUNC_XXX values
39   *  found in {@link com.sun.org.apache.xpath.internal.compiler.FunctionTable}, but may
40   *  be a value installed by an external module.  */
41  private int m_funcID;
42
43  /** The class name of the function.  Must not be null.   */
44  private String m_funcName;
45
46  /**
47   * Get the local class name of the function class.  If function name does
48   * not have a '.' in it, it is assumed to be relative to
49   * 'com.sun.org.apache.xpath.internal.functions'.
50   *
51   * @return The class name of the {com.sun.org.apache.xpath.internal.functions.Function} class.
52   */
53  public String getName()
54  {
55    return m_funcName;
56  }
57
58  /**
59   * Construct a function loader
60   *
61   * @param funcName The class name of the {com.sun.org.apache.xpath.internal.functions.Function}
62   *             class, which, if it does not have a '.' in it, is assumed to
63   *             be relative to 'com.sun.org.apache.xpath.internal.functions'.
64   * @param funcID  The function ID, which may correspond to one of the FUNC_XXX
65   *    values found in {@link com.sun.org.apache.xpath.internal.compiler.FunctionTable}, but may
66   *    be a value installed by an external module.
67   */
68  public FuncLoader(String funcName, int funcID)
69  {
70
71    super();
72
73    m_funcID = funcID;
74    m_funcName = funcName;
75  }
76
77  /**
78   * Get a Function instance that this instance is liaisoning for.
79   *
80   * @return non-null reference to Function derivative.
81   *
82   * @throws javax.xml.transform.TransformerException if ClassNotFoundException,
83   *    IllegalAccessException, or InstantiationException is thrown.
84   */
85  Function getFunction() throws TransformerException
86  {
87    try
88    {
89      String className = m_funcName;
90      if (className.indexOf(".") < 0) {
91        className = "com.sun.org.apache.xpath.internal.functions." + className;
92      }
93      //hack for loading only built-in function classes.
94      String subString = className.substring(0,className.lastIndexOf('.'));
95      if(!(subString.equals ("com.sun.org.apache.xalan.internal.templates") ||
96           subString.equals ("com.sun.org.apache.xpath.internal.functions"))) {
97            throw new TransformerException("Application can't install his own xpath function.");
98      }
99
100      return (Function) ObjectFactory.newInstance(className, true);
101
102    }
103    catch (ConfigurationError e)
104    {
105      throw new TransformerException(e.getException());
106    }
107  }
108}
109