KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > functions > FuncSystemProperty


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: FuncSystemProperty.java,v 1.18 2004/02/23 10:29:37 aruny Exp $
18  */

19 package org.apache.xpath.functions;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.apache.xpath.XPathContext;
26 import org.apache.xpath.objects.XNumber;
27 import org.apache.xpath.objects.XObject;
28 import org.apache.xpath.objects.XString;
29 import org.apache.xpath.res.XPATHErrorResources;
30
31 /**
32  * Execute the SystemProperty() function.
33  * @xsl.usage advanced
34  */

35 public class FuncSystemProperty extends FunctionOneArg
36 {
37   /**
38    * The path/filename of the property file: XSLTInfo.properties
39    * Maintenance note: see also
40    * org.apache.xalan.processor.TransformerFactoryImpl.XSLT_PROPERTIES
41    */

42   static String JavaDoc XSLT_PROPERTIES = "org/apache/xalan/res/XSLTInfo.properties";
43
44   /**
45    * Execute the function. The function must return
46    * a valid object.
47    * @param xctxt The current execution context.
48    * @return A valid XObject.
49    *
50    * @throws javax.xml.transform.TransformerException
51    */

52   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
53   {
54
55     String JavaDoc fullName = m_arg0.execute(xctxt).str();
56     int indexOfNSSep = fullName.indexOf(':');
57     String JavaDoc result;
58     String JavaDoc propName = "";
59
60     // List of properties where the name of the
61
// property argument is to be looked for.
62
Properties JavaDoc xsltInfo = new Properties JavaDoc();
63
64     loadPropertyFile(XSLT_PROPERTIES, xsltInfo);
65
66     if (indexOfNSSep > 0)
67     {
68       String JavaDoc prefix = (indexOfNSSep >= 0)
69                       ? fullName.substring(0, indexOfNSSep) : "";
70       String JavaDoc namespace;
71
72       namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
73       propName = (indexOfNSSep < 0)
74                  ? fullName : fullName.substring(indexOfNSSep + 1);
75
76       if (namespace.startsWith("http://www.w3.org/XSL/Transform")
77               || namespace.equals("http://www.w3.org/1999/XSL/Transform"))
78       {
79         result = xsltInfo.getProperty(propName);
80
81         if (null == result)
82         {
83           warn(xctxt, XPATHErrorResources.WG_PROPERTY_NOT_SUPPORTED,
84                new Object JavaDoc[]{ fullName }); //"XSL Property not supported: "+fullName);
85

86           return XString.EMPTYSTRING;
87         }
88       }
89       else
90       {
91         warn(xctxt, XPATHErrorResources.WG_DONT_DO_ANYTHING_WITH_NS,
92              new Object JavaDoc[]{ namespace,
93                            fullName }); //"Don't currently do anything with namespace "+namespace+" in property: "+fullName);
94

95         try
96         {
97           result = System.getProperty(propName);
98
99           if (null == result)
100           {
101
102             // result = System.getenv(propName);
103
return XString.EMPTYSTRING;
104           }
105         }
106         catch (SecurityException JavaDoc se)
107         {
108           warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
109                new Object JavaDoc[]{ fullName }); //"SecurityException when trying to access XSL system property: "+fullName);
110

111           return XString.EMPTYSTRING;
112         }
113       }
114     }
115     else
116     {
117       try
118       {
119         result = System.getProperty(fullName);
120
121         if (null == result)
122         {
123
124           // result = System.getenv(fullName);
125
return XString.EMPTYSTRING;
126         }
127       }
128       catch (SecurityException JavaDoc se)
129       {
130         warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION,
131              new Object JavaDoc[]{ fullName }); //"SecurityException when trying to access XSL system property: "+fullName);
132

133         return XString.EMPTYSTRING;
134       }
135     }
136
137     if (propName.equals("version") && result.length() > 0)
138     {
139       try
140       {
141         // Needs to return the version number of the spec we conform to.
142
return new XNumber(1.0);
143       }
144       catch (Exception JavaDoc ex)
145       {
146         return new XString(result);
147       }
148     }
149     else
150       return new XString(result);
151   }
152
153   /**
154    * Retrieve a propery bundle from a specified file
155    *
156    * @param file The string name of the property file. The name
157    * should already be fully qualified as path/filename
158    * @param target The target property bag the file will be placed into.
159    */

160   public void loadPropertyFile(String JavaDoc file, Properties JavaDoc target)
161   {
162     try
163     {
164       // Use SecuritySupport class to provide priveleged access to property file
165
SecuritySupport ss = SecuritySupport.getInstance();
166
167       InputStream JavaDoc is = ss.getResourceAsStream(ObjectFactory.findClassLoader(),
168                                               file);
169
170       // get a buffered version
171
BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is);
172
173       target.load(bis); // and load up the property bag from this
174
bis.close(); // close out after reading
175
}
176     catch (Exception JavaDoc ex)
177     {
178       // ex.printStackTrace();
179
throw new org.apache.xml.utils.WrappedRuntimeException(ex);
180     }
181   }
182 }
183
Popular Tags