01: /* ****************************************************************************
02: * SystemProperties.java
03: * ****************************************************************************/
04:
05: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
06: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
07: * Use is subject to license terms. *
08: * J_LZ_COPYRIGHT_END *********************************************************/
09:
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: public class SystemProperties {
27: public static DataEncoder getProperties() {
28: DataEncoder encoder = new DataEncoder();
29: encoder.startDocument();
30:
31: encoder.startElement("system", new AttributesImpl());
32:
33: Properties prop = System.getProperties();
34: Enumeration propertyNames = prop.propertyNames();
35: while (propertyNames.hasMoreElements()) {
36: String key = (String) propertyNames.nextElement();
37: String val = xmlEscape(prop.getProperty(key));
38: AttributesImpl attrs = new AttributesImpl();
39: attrs.addAttribute("", "key", "", "CDATA", key);
40: attrs.addAttribute("", "val", "", "CDATA", val);
41:
42: encoder.startElement("property", attrs);
43: encoder.endElement();
44: }
45:
46: encoder.endElement();
47: encoder.endDocument();
48: return encoder;
49: }
50:
51: public static DataEncoder getProperty(String key) {
52: DataEncoder encoder = new DataEncoder();
53: encoder.startDocument();
54:
55: encoder.startElement("system", new AttributesImpl());
56:
57: String val = xmlEscape(System.getProperty(key));
58: AttributesImpl attrs = new AttributesImpl();
59: attrs.addAttribute("", "key", "", "CDATA", key);
60: attrs.addAttribute("", "val", "", "CDATA", val);
61: encoder.startElement("property", attrs);
62: encoder.endElement();
63:
64: encoder.endElement();
65:
66: encoder.endDocument();
67: return encoder;
68: }
69:
70: /**
71: * Escape the 5 entities defined by XML.
72: * These are: '<', '>', '\', '&', '"'.
73: *
74: * @param s an xml string
75: * @return an escaped xml string
76: */
77: static String xmlEscape(String s) {
78: if (s == null)
79: return null;
80: StringBuffer sb = new StringBuffer();
81: for (int i = 0; i < s.length(); i++) {
82: char c = s.charAt(i);
83: if (c == '<') {
84: sb.append("<");
85: } else if (c == '>') {
86: sb.append(">");
87: } else if (c == '\'') {
88: sb.append("'");
89: } else if (c == '&') {
90: sb.append("&");
91: } else if (c == '"') {
92: sb.append(""");
93: } else {
94: sb.append(c);
95: }
96: }
97: return sb.toString();
98: }
99: }
|