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