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: FuncSystemProperty.java,v 1.18 2004/02/23 10:29:37 aruny Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.functions;
  20. import java.io.BufferedInputStream;
  21. import java.io.InputStream;
  22. import java.util.Properties;
  23. import com.sun.org.apache.xpath.internal.XPathContext;
  24. import com.sun.org.apache.xpath.internal.objects.XNumber;
  25. import com.sun.org.apache.xpath.internal.objects.XObject;
  26. import com.sun.org.apache.xpath.internal.objects.XString;
  27. import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  28. /**
  29. * Execute the SystemProperty() function.
  30. * @xsl.usage advanced
  31. */
  32. public class FuncSystemProperty extends FunctionOneArg
  33. {
  34. /**
  35. * The path/filename of the property file: XSLTInfo.properties
  36. * Maintenance note: see also
  37. * com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl.XSLT_PROPERTIES
  38. */
  39. static String XSLT_PROPERTIES = "com/sun/org/apache/xalan/internal/res/XSLTInfo.properties";
  40. /**
  41. * Execute the function. The function must return
  42. * a valid object.
  43. * @param xctxt The current execution context.
  44. * @return A valid XObject.
  45. *
  46. * @throws javax.xml.transform.TransformerException
  47. */
  48. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  49. {
  50. String fullName = m_arg0.execute(xctxt).str();
  51. int indexOfNSSep = fullName.indexOf(':');
  52. String result;
  53. String propName = "";
  54. // List of properties where the name of the
  55. // property argument is to be looked for.
  56. Properties xsltInfo = new Properties();
  57. loadPropertyFile(XSLT_PROPERTIES, xsltInfo);
  58. if (indexOfNSSep > 0)
  59. {
  60. String prefix = (indexOfNSSep >= 0)
  61. ? fullName.substring(0, indexOfNSSep) : "";
  62. String namespace;
  63. namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
  64. propName = (indexOfNSSep < 0)
  65. ? fullName : fullName.substring(indexOfNSSep + 1);
  66. if (namespace.startsWith("http://www.w3.org/XSL/Transform")
  67. || namespace.equals("http://www.w3.org/1999/XSL/Transform"))
  68. {
  69. result = xsltInfo.getProperty(propName);
  70. if (null == result)
  71. {
  72. warn(xctxt, XPATHErrorResources.WG_PROPERTY_NOT_SUPPORTED,
  73. new Object[]{ fullName }); //"XSL Property not supported: "+fullName);
  74. return XString.EMPTYSTRING;
  75. }
  76. }
  77. else
  78. {
  79. warn(xctxt, XPATHErrorResources.WG_DONT_DO_ANYTHING_WITH_NS,
  80. new Object[]{ namespace,
  81. fullName }); //"Don't currently do anything with namespace "+namespace+" in property: "+fullName);
  82. try
  83. {
  84. result = System.getProperty(propName);
  85. if (null == result)
  86. {
  87. // result = System.getenv(propName);
  88. return XString.EMPTYSTRING;
  89. }
  90. }
  91. catch (SecurityException se)
  92. {
  93. warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
  94. new Object[]{ fullName }); //"SecurityException when trying to access XSL system property: "+fullName);
  95. return XString.EMPTYSTRING;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. try
  102. {
  103. result = System.getProperty(fullName);
  104. if (null == result)
  105. {
  106. // result = System.getenv(fullName);
  107. return XString.EMPTYSTRING;
  108. }
  109. }
  110. catch (SecurityException se)
  111. {
  112. warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
  113. new Object[]{ fullName }); //"SecurityException when trying to access XSL system property: "+fullName);
  114. return XString.EMPTYSTRING;
  115. }
  116. }
  117. if (propName.equals("version") && result.length() > 0)
  118. {
  119. try
  120. {
  121. // Needs to return the version number of the spec we conform to.
  122. return new XNumber(1.0);
  123. }
  124. catch (Exception ex)
  125. {
  126. return new XString(result);
  127. }
  128. }
  129. else
  130. return new XString(result);
  131. }
  132. /**
  133. * Retrieve a propery bundle from a specified file
  134. *
  135. * @param file The string name of the property file. The name
  136. * should already be fully qualified as path/filename
  137. * @param target The target property bag the file will be placed into.
  138. */
  139. public void loadPropertyFile(String file, Properties target)
  140. {
  141. try
  142. {
  143. // Use SecuritySupport class to provide priveleged access to property file
  144. SecuritySupport ss = SecuritySupport.getInstance();
  145. InputStream is = ss.getResourceAsStream(ObjectFactory.findClassLoader(),
  146. file);
  147. // get a buffered version
  148. BufferedInputStream bis = new BufferedInputStream(is);
  149. target.load(bis); // and load up the property bag from this
  150. bis.close(); // close out after reading
  151. }
  152. catch (Exception ex)
  153. {
  154. // ex.printStackTrace();
  155. throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(ex);
  156. }
  157. }
  158. }