1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xpath.functions;
  58. import java.util.Properties;
  59. import java.io.BufferedInputStream;
  60. import java.io.InputStream;
  61. import java.lang.ClassLoader;
  62. import org.apache.xml.utils.PrefixResolver;
  63. import org.apache.xpath.res.XPATHErrorResources;
  64. //import org.w3c.dom.Node;
  65. import java.util.Vector;
  66. import org.apache.xpath.XPathContext;
  67. import org.apache.xpath.XPath;
  68. import org.apache.xpath.objects.XObject;
  69. import org.apache.xpath.objects.XNumber;
  70. import org.apache.xpath.objects.XString;
  71. /**
  72. * <meta name="usage" content="advanced"/>
  73. * Execute the SystemProperty() function.
  74. */
  75. public class FuncSystemProperty extends FunctionOneArg
  76. {
  77. /**
  78. * The path/filename of the property file: XSLTInfo.properties
  79. * Maintenance note: see also org.apache.xalan.processor.TransformerFactoryImpl.XSLT_PROPERTIES
  80. */
  81. static String XSLT_PROPERTIES = "org/apache/xalan/res/XSLTInfo.properties";
  82. /** a zero length Class array used in loadPropertyFile() */
  83. private static final Class[] NO_CLASSES = new Class[0];
  84. /** a zero length Object array used in loadPropertyFile() */
  85. private static final Object[] NO_OBJS = new Object[0];
  86. /**
  87. * Execute the function. The function must return
  88. * a valid object.
  89. * @param xctxt The current execution context.
  90. * @return A valid XObject.
  91. *
  92. * @throws javax.xml.transform.TransformerException
  93. */
  94. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  95. {
  96. String fullName = m_arg0.execute(xctxt).str();
  97. int indexOfNSSep = fullName.indexOf(':');
  98. String result;
  99. String propName = "";
  100. /* List of properties where the name of the property argument is
  101. * to be looked for. */
  102. Properties xsltInfo = new Properties();
  103. loadPropertyFile(XSLT_PROPERTIES, xsltInfo);
  104. if (indexOfNSSep > 0)
  105. {
  106. String prefix = (indexOfNSSep >= 0)
  107. ? fullName.substring(0, indexOfNSSep) : "";
  108. String namespace;
  109. namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
  110. propName = (indexOfNSSep < 0)
  111. ? fullName : fullName.substring(indexOfNSSep + 1);
  112. if (namespace.startsWith("http://www.w3.org/XSL/Transform")
  113. || namespace.equals("http://www.w3.org/1999/XSL/Transform"))
  114. {
  115. result = xsltInfo.getProperty(propName);
  116. if (null == result)
  117. {
  118. warn(xctxt, XPATHErrorResources.WG_PROPERTY_NOT_SUPPORTED,
  119. new Object[]{ fullName }); //"XSL Property not supported: "+fullName);
  120. return XString.EMPTYSTRING;
  121. }
  122. }
  123. else
  124. {
  125. warn(xctxt, XPATHErrorResources.WG_DONT_DO_ANYTHING_WITH_NS,
  126. new Object[]{ namespace,
  127. fullName }); //"Don't currently do anything with namespace "+namespace+" in property: "+fullName);
  128. try
  129. {
  130. result = System.getProperty(propName);
  131. if (null == result)
  132. {
  133. // result = System.getenv(propName);
  134. return XString.EMPTYSTRING;
  135. }
  136. }
  137. catch (SecurityException se)
  138. {
  139. warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
  140. new Object[]{ fullName }); //"SecurityException when trying to access XSL system property: "+fullName);
  141. return XString.EMPTYSTRING;
  142. }
  143. }
  144. }
  145. else
  146. {
  147. try
  148. {
  149. result = System.getProperty(fullName);
  150. if (null == result)
  151. {
  152. // result = System.getenv(fullName);
  153. return XString.EMPTYSTRING;
  154. }
  155. }
  156. catch (SecurityException se)
  157. {
  158. warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
  159. new Object[]{ fullName }); //"SecurityException when trying to access XSL system property: "+fullName);
  160. return XString.EMPTYSTRING;
  161. }
  162. }
  163. if (propName.equals("version") && result.length() > 0)
  164. {
  165. try
  166. {
  167. // Needs to return the version number of the spec we conform to.
  168. return new XNumber(1.0);
  169. }
  170. catch (Exception ex)
  171. {
  172. return new XString(result);
  173. }
  174. }
  175. else
  176. return new XString(result);
  177. }
  178. /**
  179. * Retrieve a propery bundle from a specified file
  180. *
  181. * @param file The string name of the property file. The name
  182. * should already be fully qualified as path/filename
  183. * @param target The target property bag the file will be placed into.
  184. */
  185. public void loadPropertyFile(String file, Properties target)
  186. {
  187. InputStream is = null;
  188. try
  189. {
  190. try {
  191. java.lang.reflect.Method getCCL = Thread.class.getMethod("getContextClassLoader", NO_CLASSES);
  192. if (getCCL != null) {
  193. ClassLoader contextClassLoader = (ClassLoader) getCCL.invoke(Thread.currentThread(), NO_OBJS);
  194. is = contextClassLoader.getResourceAsStream(file); // file should be already fully specified
  195. }
  196. }
  197. catch (Exception e) {}
  198. if (is == null) {
  199. // NOTE! For the below getResourceAsStream in Sun JDK 1.1.8M
  200. // we apparently must add the leading slash character - I
  201. // don't know why, but if it's not there, we throw an NPE from the below loading
  202. is = FuncSystemProperty.class.getResourceAsStream("/" + file); // file should be already fully specified
  203. }
  204. // get a buffered version
  205. BufferedInputStream bis = new BufferedInputStream(is);
  206. target.load(bis); // and load up the property bag from this
  207. bis.close(); // close out after reading
  208. }
  209. catch (Exception ex)
  210. {
  211. // ex.printStackTrace();
  212. throw new org.apache.xml.utils.WrappedRuntimeException(ex);
  213. }
  214. }
  215. }