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