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