KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > test > xmlrpc > SystemProperties


1 /* ****************************************************************************
2  * SystemProperties.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.test.xmlrpc;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.OutputStream;
15 import java.net.MalformedURLException;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18 import java.util.Date;
19 import java.util.Enumeration;
20 import java.util.HashMap;
21 import java.util.Properties;
22 import org.xml.sax.helpers.AttributesImpl;
23
24 import org.openlaszlo.xml.DataEncoder;
25
26
27 public class SystemProperties
28 {
29     public static DataEncoder getProperties() {
30         DataEncoder encoder = new DataEncoder();
31         encoder.startDocument();
32
33         encoder.startElement("system", new AttributesImpl());
34
35         Properties prop = System.getProperties();
36         Enumeration enum = prop.propertyNames();
37         while (enum.hasMoreElements()) {
38             String key = (String)enum.nextElement();
39             String val = xmlEscape(prop.getProperty(key));
40             AttributesImpl attrs = new AttributesImpl();
41             attrs.addAttribute("", "key", "", "CDATA", key);
42             attrs.addAttribute("", "val", "", "CDATA", val);
43
44             encoder.startElement("property", attrs);
45             encoder.endElement();
46         }
47
48         encoder.endElement();
49         encoder.endDocument();
50         return encoder;
51     }
52
53     public static DataEncoder getProperty(String key) {
54         DataEncoder encoder = new DataEncoder();
55         encoder.startDocument();
56
57         encoder.startElement("system", new AttributesImpl());
58
59         String val = xmlEscape(System.getProperty(key));
60         AttributesImpl attrs = new AttributesImpl();
61         attrs.addAttribute("", "key", "", "CDATA", key);
62         attrs.addAttribute("", "val", "", "CDATA", val);
63         encoder.startElement("property", attrs);
64         encoder.endElement();
65
66         encoder.endElement();
67
68         encoder.endDocument();
69         return encoder;
70     }
71
72     /**
73      * Escape the 5 entities defined by XML.
74      * These are: '<', '>', '\', '&amp;', '"'.
75      *
76      * @param s an xml string
77      * @return an escaped xml string
78      */

79     static String xmlEscape(String s) {
80         if (s == null) return null;
81         StringBuffer sb = new StringBuffer();
82         for(int i=0; i<s.length(); i++) {
83             char c = s.charAt(i);
84             if (c == '<') {
85                 sb.append("&lt;");
86             } else if (c == '>') {
87                 sb.append("&gt;");
88             } else if (c == '\'') {
89                 sb.append("&apos;");
90             } else if (c == '&') {
91                 sb.append("&amp;");
92             } else if (c == '"') {
93                 sb.append("&quot;");
94             } else {
95                 sb.append(c);
96             }
97         }
98         return sb.toString();
99     }
100 }
101
102
Popular Tags