001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: FuncSystemProperty.java,v 1.21 2005/07/25 20:26:46 mcnamara Exp $
018: */
019: package org.apache.xpath.functions;
020:
021: import java.io.BufferedInputStream;
022: import java.io.InputStream;
023: import java.util.Properties;
024:
025: import org.apache.xpath.XPathContext;
026: import org.apache.xpath.objects.XNumber;
027: import org.apache.xpath.objects.XObject;
028: import org.apache.xpath.objects.XString;
029: import org.apache.xpath.res.XPATHErrorResources;
030:
031: /**
032: * Execute the SystemProperty() function.
033: * @xsl.usage advanced
034: */
035: public class FuncSystemProperty extends FunctionOneArg {
036: static final long serialVersionUID = 3694874980992204867L;
037: /**
038: * The path/filename of the property file: XSLTInfo.properties
039: * Maintenance note: see also
040: * org.apache.xalan.processor.TransformerFactoryImpl.XSLT_PROPERTIES
041: */
042: static final String XSLT_PROPERTIES = "org/apache/xalan/res/XSLTInfo.properties";
043:
044: /**
045: * Execute the function. The function must return
046: * a valid object.
047: * @param xctxt The current execution context.
048: * @return A valid XObject.
049: *
050: * @throws javax.xml.transform.TransformerException
051: */
052: public XObject execute(XPathContext xctxt)
053: throws javax.xml.transform.TransformerException {
054:
055: String fullName = m_arg0.execute(xctxt).str();
056: int indexOfNSSep = fullName.indexOf(':');
057: String result;
058: String propName = "";
059:
060: // List of properties where the name of the
061: // property argument is to be looked for.
062: Properties xsltInfo = new Properties();
063:
064: loadPropertyFile(XSLT_PROPERTIES, xsltInfo);
065:
066: if (indexOfNSSep > 0) {
067: String prefix = (indexOfNSSep >= 0) ? fullName.substring(0,
068: indexOfNSSep) : "";
069: String namespace;
070:
071: namespace = xctxt.getNamespaceContext()
072: .getNamespaceForPrefix(prefix);
073: propName = (indexOfNSSep < 0) ? fullName : fullName
074: .substring(indexOfNSSep + 1);
075:
076: if (namespace.startsWith("http://www.w3.org/XSL/Transform")
077: || namespace
078: .equals("http://www.w3.org/1999/XSL/Transform")) {
079: result = xsltInfo.getProperty(propName);
080:
081: if (null == result) {
082: warn(
083: xctxt,
084: XPATHErrorResources.WG_PROPERTY_NOT_SUPPORTED,
085: new Object[] { fullName }); //"XSL Property not supported: "+fullName);
086:
087: return XString.EMPTYSTRING;
088: }
089: } else {
090: warn(
091: xctxt,
092: XPATHErrorResources.WG_DONT_DO_ANYTHING_WITH_NS,
093: new Object[] { namespace, fullName }); //"Don't currently do anything with namespace "+namespace+" in property: "+fullName);
094:
095: try {
096: result = System.getProperty(propName);
097:
098: if (null == result) {
099:
100: // result = System.getenv(propName);
101: return XString.EMPTYSTRING;
102: }
103: } catch (SecurityException se) {
104: warn(xctxt,
105: XPATHErrorResources.WG_SECURITY_EXCEPTION,
106: new Object[] { fullName }); //"SecurityException when trying to access XSL system property: "+fullName);
107:
108: return XString.EMPTYSTRING;
109: }
110: }
111: } else {
112: try {
113: result = System.getProperty(fullName);
114:
115: if (null == result) {
116:
117: // result = System.getenv(fullName);
118: return XString.EMPTYSTRING;
119: }
120: } catch (SecurityException se) {
121: warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
122: new Object[] { fullName }); //"SecurityException when trying to access XSL system property: "+fullName);
123:
124: return XString.EMPTYSTRING;
125: }
126: }
127:
128: if (propName.equals("version") && result.length() > 0) {
129: try {
130: // Needs to return the version number of the spec we conform to.
131: return new XString("1.0");
132: } catch (Exception ex) {
133: return new XString(result);
134: }
135: } else
136: return new XString(result);
137: }
138:
139: /**
140: * Retrieve a propery bundle from a specified file
141: *
142: * @param file The string name of the property file. The name
143: * should already be fully qualified as path/filename
144: * @param target The target property bag the file will be placed into.
145: */
146: public void loadPropertyFile(String file, Properties target) {
147: try {
148: // Use SecuritySupport class to provide priveleged access to property file
149: SecuritySupport ss = SecuritySupport.getInstance();
150:
151: InputStream is = ss.getResourceAsStream(ObjectFactory
152: .findClassLoader(), file);
153:
154: // get a buffered version
155: BufferedInputStream bis = new BufferedInputStream(is);
156:
157: target.load(bis); // and load up the property bag from this
158: bis.close(); // close out after reading
159: } catch (Exception ex) {
160: // ex.printStackTrace();
161: throw new org.apache.xml.utils.WrappedRuntimeException(ex);
162: }
163: }
164: }
|