KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > UtilConvert


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.properties;
21
22 /**
23  * Contains conversion utilities which allow reading and storing a properties file
24  * while preserving formatting and comments that user may have entered.
25  * <p>
26  * Huge portions of this class marked by <i>// prj40 trunk compatability</i>
27  * are there only for being able to run unmodified i18n module code in
28  * prj40_prototype branch and trunk. Properties module is not
29  * prj40 branched and it is updated by <tt>cvs up -f -rprj40_prototype</tt>.
30  * <br>
31  * 28th March 2003 Petr Kuzel
32  *
33  * @author Petr Jiricka
34  * @author Petr Kuzel - simplification
35  */

36 public class UtilConvert {
37
38     private UtilConvert() {
39     }
40
41     /**
42      * These characters play role as key-value sepsrators
43      */

44     public static final String JavaDoc keyValueSeparators = "=: \t\r\n\f";
45
46     public static final String JavaDoc strictKeyValueSeparators = "=:";
47
48     /** Differs from JDK's implementation in that it does not save ' ' as '\ '. */
49     private static final String JavaDoc specialSaveChars = "=:\t\r\n\f#!";
50
51     public static final String JavaDoc whiteSpaceChars = " \t\r\n\f";
52
53
54     /**
55      * Escape key value. Converts string to one with escaped ' ','=',':', and last '\\'
56      * in case they are not escaped already. Used for formating user input.
57      * //!!! wrong semantics what does it mean if not escaped already?
58      */

59     public static String JavaDoc escapePropertiesSpecialChars (String JavaDoc source) {
60         return source; // prj40 trunk compatability
61
// if(source == null) return null;
62
// StringBuffer result = new StringBuffer();
63
// for(int i=0; i<source.length(); i++) {
64
// char x = source.charAt(i);
65
// if(x == ' ' || x == '=' || x == ':') {
66
// if( i==0 || (i>0 && source.charAt(i-1) != '\\'))
67
// result.append('\\');
68
// }
69
// // last char == '\\'
70
// if(i==source.length()-1 && x == '\\') {
71
// if( i>0 && source.charAt(i-1)!='\\')
72
// result.append('\\');
73
// }
74
// result.append(x);
75
// }
76
// return result.toString();
77
}
78     
79     /** Checks whether the string contains only spaces */
80     private static boolean onlySpaces(String JavaDoc s){
81         for (int i = 0; i<s.length(); i++){
82             if (s.charAt(i) != ' ') return false;
83         }
84         return true;
85     }
86     
87     /** Escapes spaces in outer part of string. */
88     public static String JavaDoc escapeOutsideSpaces(String JavaDoc source){
89         return source; // prj40 trunk compatability
90
// if (source == null || source.length() == 0) return source;
91
// StringBuffer result = new StringBuffer();
92
// int i = 0;
93
// while (source.charAt(i) == ' '){
94
// result.append('\\');
95
// result.append(' ');
96
// if ((i+1) == source.length()) return result.toString();
97
// i++;
98
// }
99
// while (!onlySpaces(source.substring(i))){
100
// result.append(source.charAt(i));
101
// if ((i+1) == source.length()) return result.toString();
102
// i++;
103
// }
104
// while (i < source.length()){
105
// result.append('\\');
106
// result.append(' ');
107
// i++;
108
// }
109
// return result.toString();
110
}
111
112     /**
113      * Escape user's value. It escapes last '\\' character only
114      * to prevent user to wrongly create continuation line.
115      */

116     public static String JavaDoc escapeLineContinuationChar(String JavaDoc source) {
117         return source; // prj40 trunk compatability
118
// if(source == null) return null;
119
// if(source.endsWith("\\")) { //NOI18N
120
// if(source.length()>1 && source.charAt(source.length()-2)!='\\')
121
// return new String(new StringBuffer(source).append('\\'));
122
// }
123
// return source;
124
}
125
126     /**
127      * Converts these java special chars ('\t', '\n', '\b', '\r', '\f') to encoded escapes.
128      * Note there are not converted unicode chars.
129      */

130     public static String JavaDoc escapeJavaSpecialChars(String JavaDoc source) {
131         return source; // prj40 trunk compatability
132
// if(source == null) return null;
133
// StringBuffer result = new StringBuffer();
134
// for (int i=0; i<source.length(); i++) {
135
// char ch = source.charAt(i);
136
// switch(ch) {
137
// case '\t':
138
// result.append("\\t"); //NOI18N
139
// break;
140
// case '\n':
141
// result.append("\\n"); //NOI18N
142
// break;
143
// case '\b':
144
// result.append("\\b"); //NOI18N
145
// break;
146
// case '\r':
147
// result.append("\\r"); //NOI18N
148
// break;
149
// case '\f':
150
// result.append("\\f"); //NOI18N
151
// break;
152
// default:
153
// result.append(ch);
154
// }
155
// }
156
// return result.toString();
157
}
158
159
160     /**
161      * Converts encoded '\\uxxxx' to chars.
162      * Note there are not converted '\\"', '\\'', '\\ ', '\\\\' and java special chars escapes.
163      */

164     public static String JavaDoc unicodesToChars (String JavaDoc theString) {
165         return theString; // prj40 trunk compatability
166
// if (theString == null) return null;
167
// char aChar;
168
// char next;
169
// int len = theString.length();
170
// StringBuffer outBuffer = new StringBuffer(len);
171
//
172
// for(int x=0; x<len; x++) {
173
// aChar = theString.charAt(x);
174
// if(x+5 < len) { // if there is space for uXXXX chars enough
175
// next = theString.charAt(x+1);
176
// try {
177
// if (aChar == '\\' && next == 'u') {
178
// // Read the xxxx
179
// int value=0;
180
// for (int i=0; i<4; i++) {
181
// next = theString.charAt(x+1+i+1);
182
// switch (next) {
183
// case '0': case '1': case '2': case '3': case '4':
184
// case '5': case '6': case '7': case '8': case '9':
185
// value = (value << 4) + next - '0';
186
// break;
187
// case 'a': case 'b': case 'c':
188
// case 'd': case 'e': case 'f':
189
// value = (value << 4) + 10 + next - 'a';
190
// break;
191
// case 'A': case 'B': case 'C':
192
// case 'D': case 'E': case 'F':
193
// value = (value << 4) + 10 + next - 'A';
194
// break;
195
// default:
196
// throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
197
// }
198
// }
199
// outBuffer.append((char)value);
200
// x += 5;
201
// } else
202
// outBuffer.append(aChar);
203
// } catch (IllegalArgumentException iae) {
204
// outBuffer.append(aChar); // not unicode -> interpret as a normal char
205
// }
206
// } else
207
// outBuffer.append(aChar);
208
// }
209
// return outBuffer.toString();
210
}
211
212     /**
213      * Convert chars to encoded '\\uxxxx' using comment escaping rules.
214      * @param commentString
215      * @return escaped comment
216      */

217     public static String JavaDoc escapeComment(String JavaDoc commentString) {
218         return charsToUnicodes(commentString, true);
219     }
220
221     /**
222     * Converts chars to encoded '\\uxxxx'.
223      * Note there are not converted '\\"', '\\'', '\\ ', '\\\\' and java special chars escapes.
224     */

225     public static String JavaDoc charsToUnicodes(String JavaDoc s){
226         return charsToUnicodes(s, false);
227     }
228     
229     
230     /**
231     * Converts chars to encoded '\\uxxxx'. If skipWhiteSpaces is true, then white spaces won't be converted
232      * Note there are not converted '\\"', '\\'', '\\ ', '\\\\' and java special chars escapes.
233     */

234     public static String JavaDoc charsToUnicodes(String JavaDoc theString, boolean skipWhiteSpaces) {
235         return theString; // prj40 trunk compatability
236
// if(theString == null) return null;
237
// char aChar;
238
// int len = theString.length();
239
// StringBuffer outBuffer = new StringBuffer(len*2);
240
//
241
// for(int x=0; x<len; ) {
242
// aChar = theString.charAt(x++);
243
// if ((aChar < 20) || (aChar > 127) ) {
244
//
245
// if (skipWhiteSpaces && Character.isWhitespace(aChar)){
246
// // do not convert white spaces
247
// outBuffer.append(aChar);
248
// continue;
249
// }
250
//
251
// outBuffer.append('\\');
252
// outBuffer.append('u');
253
// outBuffer.append(toHex((aChar >> 12) & 0xF));
254
// outBuffer.append(toHex((aChar >> 8) & 0xF));
255
// outBuffer.append(toHex((aChar >> 4) & 0xF));
256
// outBuffer.append(toHex((aChar >> 0) & 0xF));
257
// } else {
258
// outBuffer.append(aChar);
259
// }
260
// }
261
// return outBuffer.toString();
262
}
263
264
265     /**
266      * Converts encoded \\uxxxx to unicode chars
267      * and changes special saved chars to their original forms
268      */

269     public static String JavaDoc loadConvert (String JavaDoc theString) {
270         char aChar;
271         final int len = theString.length();
272         StringBuffer JavaDoc outBuffer = new StringBuffer JavaDoc(len);
273
274         main:
275         for(int x=0; x<len; ) {
276             aChar = theString.charAt(x++);
277             if (aChar == '\\' && x != len) {
278                 aChar = theString.charAt(x++);
279                 if(aChar == 'u') {
280                     if (x > len - 4) {
281                         outBuffer.append('\\').append('u');
282                         continue main;
283                     }
284                     // Read the xxxx
285
int value=0;
286                     for (int i=0; i<4; i++) {
287                         aChar = theString.charAt(x++);
288                         switch (aChar) {
289                         case '0': case '1': case '2': case '3': case '4':
290                         case '5': case '6': case '7': case '8': case '9':
291                             value = (value << 4) + aChar - '0';
292                             break;
293                         case 'a': case 'b': case 'c':
294                         case 'd': case 'e': case 'f':
295                             value = (value << 4) + 10 + aChar - 'a';
296                             break;
297                         case 'A': case 'B': case 'C':
298                         case 'D': case 'E': case 'F':
299                             value = (value << 4) + 10 + aChar - 'A';
300                             break;
301                         default:
302                             /*
303                              * Handle a malformed \\uxxxx encoding:
304                              *
305                              * We want to print "\\u" plus all the hexadecimal
306                              * digits that passed the above switch.
307                              * To achieve it, print ("\\u")...,
308                              */

309                             outBuffer.append('\\').append('u');
310
311                             /* ... move 'x' back to character after "u"... */
312                             x -= i + 1;
313
314                             /* ... and continue with the main loop. */
315                             continue main;
316                         }
317                     }
318                     outBuffer.append((char)value);
319                 } else {
320                     if (aChar == 't') aChar = '\t';
321                     else if (aChar == 'r') aChar = '\r';
322                     else if (aChar == 'n') aChar = '\n';
323                     else if (aChar == 'f') aChar = '\f';
324                     outBuffer.append(aChar);
325                 }
326             } else
327                 outBuffer.append(aChar);
328         }
329         return outBuffer.toString();
330     }
331
332     /**
333      * Converts unicodes to encoded \\uxxxx
334      * and writes out any of the characters in specialSaveChars
335      * with a preceding slash.
336      * Differs from Sun's implementation in that it does not save ' ' as '\ '.
337      */

338     public static String JavaDoc saveConvert(String JavaDoc theString) {
339         return saveConvert(theString, false);
340     }
341
342     public static String JavaDoc saveConvert(String JavaDoc theString, boolean escapeSpace) {
343         char aChar;
344         int len = theString.length();
345         StringBuffer JavaDoc outBuffer = new StringBuffer JavaDoc(len*2);
346
347         for(int x=0; x<len; ) {
348             aChar = theString.charAt(x++);
349             switch(aChar) {
350             case '\\':outBuffer.append('\\'); outBuffer.append('\\');
351                 continue;
352             case '\t':outBuffer.append('\\'); outBuffer.append('t');
353                 continue;
354             case '\n':outBuffer.append('\\'); outBuffer.append('n');
355                 continue;
356             case '\r':outBuffer.append('\\'); outBuffer.append('r');
357                 continue;
358             case '\f':outBuffer.append('\\'); outBuffer.append('f');
359                 continue;
360             default:
361                 if ((aChar < 20) || (aChar > 127)) {
362                     outBuffer.append('\\');
363                     outBuffer.append('u');
364                     outBuffer.append(toHex((aChar >> 12) & 0xF));
365                     outBuffer.append(toHex((aChar >> 8) & 0xF));
366                     outBuffer.append(toHex((aChar >> 4) & 0xF));
367                     outBuffer.append(toHex((aChar >> 0) & 0xF));
368                 } else if (escapeSpace && (aChar == ' ')) {
369                     outBuffer.append('\\').append(' ');
370                 } else {
371                     if (specialSaveChars.indexOf(aChar) != -1)
372                         outBuffer.append('\\');
373                     outBuffer.append(aChar);
374                 }
375             }
376         }
377         return outBuffer.toString();
378     }
379
380     /**
381      * Convert a nibble to a hex character
382      * @param nibble the nibble to convert.
383      */

384     private static char toHex(int nibble) {
385         return hexDigit[(nibble & 0xF)];
386     }
387
388     /** A table of hex digits */
389     private static final char[] hexDigit = {
390         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
391     };
392 }
393
Popular Tags