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