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