KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > ext > Properties


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen.ext;
22
23 import org.ejen.util.XSLUtil;
24 import org.ejen.util.DOMUtil;
25 import java.util.Enumeration JavaDoc;
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.apache.xpath.NodeSet;
31 import org.apache.xml.utils.WrappedRuntimeException;
32 import org.apache.xalan.extensions.ExpressionContext;
33
34 /**
35  * Properties utility (static methods).
36  * <p>
37  * <table class="usage">
38  * <tr><th class="usage">Usage (XSL stylesheet)</th></tr>
39  * <tr><td class="usage"><pre>
40  *
41  * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
42  *
43  * &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
44  * ...
45  * <b>xmlns:pro="org.ejen.ext.Properties"
46  * exclude-result-prefixes="pro ..."</b>
47  * version="1.0"&gt;
48  *
49  * &lt;xsl:output method="xml" encoding="iso-8859-1"/&gt;
50  *
51  * &lt;xsl:template match="ejen"&gt;
52  *
53  * &lt;xsl:copy-of select="pro:{@link #getSystemProperties(ExpressionContext) getSystemProperties}()"/&gt;
54  * &lt;xsl:copy-of select="pro:{@link #load(ExpressionContext,String) load}($file-name)"/&gt;
55  *
56  * &lt;/xsl:template&gt;
57  *
58  * &lt;/xsl:stylesheet&gt;
59  * </pre></td></tr></table>
60  * @author F. Wolff
61  * @version 1.0
62  * @see java.util.Properties
63  */

64 public class Properties {
65
66     /**
67      * Protected constructor (prevents instanciation).
68      */

69     protected Properties() {}
70
71     /**
72      * Returns a NodeSet that contains system properties.
73      * <p>
74      * <table class="usage"><tr><td class="usage"><pre>
75      *
76      * &lt;xsl:value-of select="pro:getSystemProperties()"/&gt;
77      * </pre></td></tr></table>
78      * <p>
79      * Returned NodeSet contains a set of property Nodes with the following format:
80      * <p>
81      * <table class="usage"><tr><td class="usage"><pre>
82      *
83      * &lt;property&gt;
84      * &lt;name&gt;&lt;![CDATA[java.runtime.name]]&gt;&lt;/name&gt;
85      * &lt;value&gt;&lt;![CDATA[Java(TM) 2 Runtime Environment, Standard Edition]]&gt;&lt;/value&gt;
86      * &lt;/property&gt;
87      * </pre></td></tr></table>
88      * <p>
89      * @param context automatically passed by the xalan extension mechanism.
90      * @return system properties.
91      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error or SecurityException.
92      */

93     public static NodeSet getSystemProperties(ExpressionContext context) {
94         Document JavaDoc doc = XSLUtil.getContextDocument(context);
95
96         try {
97             java.util.Properties JavaDoc props = System.getProperties();
98             NodeSet ns = new NodeSet();
99
100             for (Enumeration JavaDoc e = props.propertyNames(); e.hasMoreElements();) {
101                 String JavaDoc name = (String JavaDoc) (e.nextElement());
102                 Element JavaDoc parent = doc.createElement("property");
103
104                 DOMUtil.createCDATANode(doc, parent, "name", name);
105                 DOMUtil.createCDATANode(doc, parent, "value",
106                         props.getProperty(name, ""));
107                 ns.addElement(parent);
108             }
109             return ns;
110         } catch (WrappedRuntimeException e) {
111             throw e;
112         } catch (Exception JavaDoc e) {
113             throw new WrappedRuntimeException(e);
114         }
115     }
116
117     /**
118      * Loads a properties file and returns it as a NodeSet.
119      * <p>
120      * <table class="usage"><tr><td class="usage"><pre>
121      *
122      * &lt;xsl:value-of select="pro:load($file-name)"/&gt;
123      * </pre></td></tr></table>
124      * <p>
125      * Returned NodeSet contains a set of property Nodes with the following format:
126      * <p>
127      * <table class="usage"><tr><td class="usage"><pre>
128      *
129      * &lt;property&gt;
130      * &lt;name&gt;&lt;![CDATA[path]]&gt;&lt;/name&gt;
131      * &lt;value&gt;&lt;![CDATA[../../ejen]]&gt;&lt;/value&gt;
132      * &lt;/property&gt;
133      * </pre></td></tr></table>
134      * <p>
135      * See {@link java.util.Properties#load(java.io.InputStream)}.
136      * <p>
137      * <dd><dl><dt><b>XSLT parameters:</b>
138      * <dd><b>[Mandatory/AVT]</b> name of the file.
139      * </dl></dd>
140      * <p>
141      * @param context automatically passed by the xalan extension mechanism.
142      * @param fileName name of the file.
143      * @return system properties.
144      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error or SecurityException.
145      */

146     public static NodeSet load(ExpressionContext context, String JavaDoc fileName) {
147         fileName = XSLUtil.evaluate(context, fileName);
148         Document JavaDoc doc = XSLUtil.getContextDocument(context);
149         BufferedInputStream JavaDoc bis = null;
150
151         try {
152             bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(fileName));
153             java.util.Properties JavaDoc props = new java.util.Properties JavaDoc();
154
155             props.load(bis);
156             NodeSet ns = new NodeSet();
157
158             for (Enumeration JavaDoc e = props.propertyNames(); e.hasMoreElements();) {
159                 String JavaDoc name = (String JavaDoc) (e.nextElement());
160                 Element JavaDoc parent = doc.createElement("property");
161
162                 DOMUtil.createCDATANode(doc, parent, "name", name);
163                 DOMUtil.createCDATANode(doc, parent, "value",
164                         props.getProperty(name, ""));
165                 ns.addElement(parent);
166             }
167             return ns;
168         } catch (WrappedRuntimeException e) {
169             throw e;
170         } catch (Exception JavaDoc e) {
171             throw new WrappedRuntimeException(e);
172         }
173         finally {
174             if (bis != null) {
175                 try {
176                     bis.close();
177                 } catch (Exception JavaDoc e) {}
178                 finally {
179                     bis = null;
180                 }
181             }
182         }
183     }
184 }
185
Popular Tags