KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBProperties


1 package com.ca.commons.cbutil;
2
3 import java.io.*;
4 import java.util.*;
5
6 /**
7  * <p>A hack class for formatting. The java Properties class is astonishingly broken in too many ways to
8  * enumerate. This is the beginnings of an attempt to migrate to something less laughable. It is based
9  * on the sun code, which thoughtfully makes every useful method in the base class private. What a joke.
10  * <p/>
11  * <p>For a start, this class:
12  * <ul><li>a) puts everything in alphabetical order
13  * <li>b) translates any 'property' ending in '.comment' to a properties file comment '#...' eliding the comment.
14  * </ul>
15  */

16 public class CBProperties extends Properties
17 {
18     private static final String JavaDoc specialSaveChars = "=: \t\r\n\f#!";
19
20     public CBProperties(Properties props)
21     {
22         Enumeration keys = props.keys();
23         while (keys.hasMoreElements())
24         {
25             Object JavaDoc key = keys.nextElement();
26             Object JavaDoc val = props.get(key);
27             put(key, val);
28         }
29     }
30
31     public Enumeration keys()
32     {
33         Vector sortedKeys = new Vector();
34         Enumeration baseKeys = super.keys();
35         while (baseKeys.hasMoreElements())
36         {
37             String JavaDoc key = (String JavaDoc) baseKeys.nextElement();
38             sortedKeys.addElement(key);
39         }
40         Collections.sort(sortedKeys);
41
42         /* The sortedkeys file should now contain (some) ordered pairs of properties
43          * in the form:
44          * my.property = 76
45          * my.property.comment = a very nice comment
46          *
47          * ... this swaps those around, replacing the 'my.property.comment' with '#my.property ='
48          # for pretty commenting.
49          */

50         for (int i = 1; i < sortedKeys.size(); i++)
51         {
52             String JavaDoc key = (String JavaDoc) sortedKeys.get(i);
53             if (key.endsWith(".comment"))
54             {
55                 String JavaDoc newkey = "#" + key.substring(0, key.length() - 8);
56                 String JavaDoc value = super.getProperty(key);
57                 super.remove(key);
58                 super.setProperty(newkey, value);
59                 String JavaDoc previousKey = (String JavaDoc) sortedKeys.get(i - 1);
60                 if (key.startsWith(previousKey))
61                 {
62                     sortedKeys.set(i - 1, newkey);
63                     sortedKeys.set(i, previousKey);
64                 }
65                 else
66                     sortedKeys.set(i, newkey); // change name, but not position. Allows for 'free floating' comments.
67
}
68         }
69
70         return sortedKeys.elements();
71     }
72
73     public synchronized void store(OutputStream out, String JavaDoc header)
74             throws IOException
75     {
76         BufferedWriter awriter;
77         //TODO: Use UTF-8 !!!
78
awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));
79         if (header != null)
80             writeln(awriter, "#" + header);
81         writeln(awriter, "#" + new Date().toString());
82         for (Enumeration e = keys(); e.hasMoreElements();)
83         {
84             String JavaDoc key = (String JavaDoc) e.nextElement();
85             String JavaDoc val = (String JavaDoc) get(key);
86             key = saveConvert(key, true);
87
88             val = saveConvert(val, false);
89
90             if (key.charAt(0) == '#') // write out comments nicely
91
{
92                 writeln(awriter, "");
93                 writeln(awriter, key + " " + val);
94             }
95             else
96                 writeln(awriter, key + "=" + val);
97         }
98         awriter.flush();
99     }
100
101     /*
102      * Converts unicodes to encoded &#92;uxxxx
103      * and writes out any of the characters in specialSaveChars
104      * with a preceding slash
105      */

106     private String JavaDoc saveConvert(String JavaDoc theString, boolean escapeSpace)
107     {
108         int len = theString.length();
109         StringBuffer JavaDoc outBuffer = new StringBuffer JavaDoc(len * 2);
110
111         for (int x = 0; x < len; x++)
112         {
113             char aChar = theString.charAt(x);
114
115             switch (aChar)
116             {
117                 case ' ':
118                     if (x == 0 || escapeSpace)
119                         outBuffer.append('\\');
120
121                     outBuffer.append(' ');
122                     break;
123                 case '#': // allow comments to be passed through!
124
if (x != 0)
125                         outBuffer.append('\\');
126                     outBuffer.append('#');
127                     break;
128                 case '\\':
129                     outBuffer.append('\\');
130                     outBuffer.append('\\');
131                     break;
132                 case '\t':
133                     outBuffer.append('\\');
134                     outBuffer.append('t');
135                     break;
136                 case '\n':
137                     outBuffer.append('\\');
138                     outBuffer.append('n');
139                     break;
140                 case '\r':
141                     outBuffer.append('\\');
142                     outBuffer.append('r');
143                     break;
144                 case '\f':
145                     outBuffer.append('\\');
146                     outBuffer.append('f');
147                     break;
148                 default:
149                     if ((aChar < 0x0020) || (aChar > 0x007e))
150                     {
151                         outBuffer.append('\\');
152                         outBuffer.append('u');
153                         outBuffer.append(toHex((aChar >> 12) & 0xF));
154                         outBuffer.append(toHex((aChar >> 8) & 0xF));
155                         outBuffer.append(toHex((aChar >> 4) & 0xF));
156                         outBuffer.append(toHex(aChar & 0xF));
157                     }
158                     else
159                     {
160                         if (specialSaveChars.indexOf(aChar) != -1)
161                             outBuffer.append('\\');
162                         outBuffer.append(aChar);
163                     }
164             }
165         }
166         return outBuffer.toString();
167     }
168
169     private static void writeln(BufferedWriter bw, String JavaDoc s) throws IOException
170     {
171         bw.write(s);
172         bw.newLine();
173     }
174
175
176     /**
177      * Convert a nibble to a hex character
178      *
179      * @param nibble the nibble to convert.
180      */

181     private static char toHex(int nibble)
182     {
183         return hexDigit[(nibble & 0xF)];
184     }
185
186     /**
187      * A table of hex digits
188      */

189     private static final char[] hexDigit = {
190         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
191     };
192 }
193
Popular Tags