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-- |
6 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
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 | 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 | 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 |
*/ |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
31287
diff
changeset
|
21 |
|
6 | 22 |
package com.sun.org.apache.xpath.internal.functions; |
23 |
||
24 |
import java.io.BufferedInputStream; |
|
25 |
import java.io.InputStream; |
|
26 |
import java.util.Properties; |
|
27 |
||
28 |
import com.sun.org.apache.xpath.internal.XPathContext; |
|
29 |
import com.sun.org.apache.xpath.internal.objects.XObject; |
|
30 |
import com.sun.org.apache.xpath.internal.objects.XString; |
|
31 |
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; |
|
12458 | 32 |
import com.sun.org.apache.xalan.internal.utils.SecuritySupport; |
6 | 33 |
|
34 |
/** |
|
35 |
* Execute the SystemProperty() function. |
|
36 |
* @xsl.usage advanced |
|
37 |
*/ |
|
38 |
public 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 |
||
31284
e9b8469adb9a
8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents:
25868
diff
changeset
|
69 |
loadPropertyFile(xsltInfo); |
6 | 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 |
{ |
|
16953 | 102 |
result = SecuritySupport.getSystemProperty(propName); |
6 | 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 |
{ |
|
16953 | 124 |
result = SecuritySupport.getSystemProperty(fullName); |
6 | 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 |
/** |
|
31287
d6581a7c221f
8129880: Cleanup usage of Class.getResource in jaxp
dfuchs
parents:
31284
diff
changeset
|
159 |
* Retrieve a property bundle from XSLT_PROPERTIES |
6 | 160 |
* |
161 |
* @param target The target property bag the file will be placed into. |
|
162 |
*/ |
|
31284
e9b8469adb9a
8129572: Cleanup usage of getResourceAsStream in jaxp
dfuchs
parents:
25868
diff
changeset
|
163 |
private void loadPropertyFile(Properties target) |
6 | 164 |
{ |
165 |
try |
|
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 | 169 |
|
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 | 174 |
} |
175 |
catch (Exception ex) |
|
176 |
{ |
|
177 |
// ex.printStackTrace(); |
|
178 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(ex); |
|
179 |
} |
|
180 |
} |
|
181 |
} |