jaxp/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncSystemProperty.java
author dfuchs
Thu, 25 Jun 2015 20:06:37 +0200
changeset 31287 d6581a7c221f
parent 31284 e9b8469adb9a
child 44797 8b3b3b911b8a
permissions -rw-r--r--
8129880: Cleanup usage of Class.getResource in jaxp Reviewed-by: joehw, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: FuncSystemProperty.java,v 1.2.4.2 2005/09/14 20:18:45 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xpath.internal.functions;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.io.BufferedInputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.InputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.util.Properties;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xpath.internal.XPathContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xpath.internal.objects.XObject;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xpath.internal.objects.XString;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    33
import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * Execute the SystemProperty() function.
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * @xsl.usage advanced
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
public class FuncSystemProperty extends FunctionOneArg
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
    static final long serialVersionUID = 3694874980992204867L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
   * The path/filename of the property file: XSLTInfo.properties
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
   * Maintenance note: see also
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
   * com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl.XSLT_PROPERTIES
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  static final String XSLT_PROPERTIES =
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
            "com/sun/org/apache/xalan/internal/res/XSLTInfo.properties";
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
   * Execute the function.  The function must return
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
   * a valid object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
   * @param xctxt The current execution context.
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   * @return A valid XObject.
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   * @throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    String fullName = m_arg0.execute(xctxt).str();
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    int indexOfNSSep = fullName.indexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    String result;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    String propName = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    // List of properties where the name of the
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    // property argument is to be looked for.
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    Properties xsltInfo = new Properties();
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
31284
e9b8469adb9a 8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents: 25868
diff changeset
    70
    loadPropertyFile(xsltInfo);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    if (indexOfNSSep > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
      String prefix = (indexOfNSSep >= 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
                      ? fullName.substring(0, indexOfNSSep) : "";
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
      String namespace;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
      namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
      propName = (indexOfNSSep < 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
                 ? fullName : fullName.substring(indexOfNSSep + 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
      if (namespace.startsWith("http://www.w3.org/XSL/Transform")
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
              || namespace.equals("http://www.w3.org/1999/XSL/Transform"))
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        result = xsltInfo.getProperty(propName);
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        if (null == result)
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
          warn(xctxt, XPATHErrorResources.WG_PROPERTY_NOT_SUPPORTED,
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
               new Object[]{ fullName });  //"XSL Property not supported: "+fullName);
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
          return XString.EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        warn(xctxt, XPATHErrorResources.WG_DONT_DO_ANYTHING_WITH_NS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
             new Object[]{ namespace,
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
                           fullName });  //"Don't currently do anything with namespace "+namespace+" in property: "+fullName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 16280
diff changeset
   103
          result = SecuritySupport.getSystemProperty(propName);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
          if (null == result)
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
          {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
            // result = System.getenv(propName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
            return XString.EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        catch (SecurityException se)
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
          warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
               new Object[]{ fullName });  //"SecurityException when trying to access XSL system property: "+fullName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
          return XString.EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
      try
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
      {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 16280
diff changeset
   125
        result = SecuritySupport.getSystemProperty(fullName);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        if (null == result)
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
          // result = System.getenv(fullName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
          return XString.EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
      catch (SecurityException se)
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
             new Object[]{ fullName });  //"SecurityException when trying to access XSL system property: "+fullName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        return XString.EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    if (propName.equals("version") && result.length() > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
      try
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
        // Needs to return the version number of the spec we conform to.
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
        return new XString("1.0");
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
      catch (Exception ex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        return new XString(result);
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
      return new XString(result);
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
  /**
31287
d6581a7c221f 8129880: Cleanup usage of Class.getResource in jaxp
dfuchs
parents: 31284
diff changeset
   160
   * Retrieve a property bundle from XSLT_PROPERTIES
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
   * @param target The target property bag the file will be placed into.
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
   */
31284
e9b8469adb9a 8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents: 25868
diff changeset
   164
  private void loadPropertyFile(Properties target)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    {
31284
e9b8469adb9a 8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents: 25868
diff changeset
   168
      // Use SecuritySupport class to provide privileged access to property file
e9b8469adb9a 8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents: 25868
diff changeset
   169
      InputStream is = SecuritySupport.getResourceAsStream(XSLT_PROPERTIES);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
      // get a buffered version
31284
e9b8469adb9a 8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents: 25868
diff changeset
   172
      try (BufferedInputStream bis = new BufferedInputStream(is)) {
e9b8469adb9a 8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents: 25868
diff changeset
   173
          target.load(bis);  // and load up the property bag from this
e9b8469adb9a 8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents: 25868
diff changeset
   174
      }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
    catch (Exception ex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
      // ex.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
      throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(ex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
}