KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > configuration > LanguageProperties


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.ui.web.admin.configuration;
15
16 import java.io.BufferedReader JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20
21 /** Properties bag that handles "double type encoding".
22  *
23  * @version $Id: LanguageProperties.java,v 1.1 2006/01/17 20:32:19 anatom Exp $
24  */

25 public class LanguageProperties extends java.util.Properties JavaDoc {
26     private static final String JavaDoc keyValueSeparators = "=: \t\r\n\f";
27     private static final String JavaDoc strictKeyValueSeparators = "=:";
28     //private static final String specialSaveChars = "=: \t\r\n\f#!";
29
private static final String JavaDoc whiteSpaceChars = " \t\r\n\f";
30     
31     public synchronized void load(InputStream JavaDoc inStream) throws IOException JavaDoc {
32         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inStream));//This sentence is crucial
33
while (true) {
34             // Get next line
35
String JavaDoc line = in.readLine();
36             if (line == null) {
37                 return;
38             }
39             if (line.length() > 0) {
40                 // Continue lines that end in slashes if they are not comments
41
char firstChar = line.charAt(0);
42                 if ( (firstChar != '#') && (firstChar != '!')) {
43                     while (continueLine(line)) {
44                         String JavaDoc nextLine = in.readLine();
45                         if (nextLine == null) {
46                             nextLine = "";
47                         }
48                         String JavaDoc loppedLine = line.substring(0, line.length() - 1);
49                         // Advance beyond whitespace on new line
50
int startIndex = 0;
51                         for (startIndex = 0; startIndex < nextLine.length(); startIndex++) {
52                             if (whiteSpaceChars.indexOf(nextLine.charAt(startIndex)) == -1) {
53                                 break;
54                             }
55                         }
56                         nextLine = nextLine.substring(startIndex, nextLine.length());
57                         line = new String JavaDoc(loppedLine + nextLine);
58                     }
59                     // Find start of key
60
int len = line.length();
61                     int keyStart;
62                     for (keyStart = 0; keyStart < len; keyStart++) {
63                         if (whiteSpaceChars.indexOf(line.charAt(keyStart)) == -1) {
64                             break;
65                         }
66                     }
67                     // Blank lines are ignored
68
if (keyStart == len) {
69                         continue;
70                     }
71                     // Find separation between key and value
72
int separatorIndex;
73                     for (separatorIndex = keyStart; separatorIndex < len; separatorIndex++) {
74                         char currentChar = line.charAt(separatorIndex);
75                         if (currentChar == '\\') {
76                             separatorIndex++;
77                         }
78                         else if (keyValueSeparators.indexOf(currentChar) != -1) {
79                             break;
80                         }
81                     }
82                     // Skip over whitespace after key if any
83
int valueIndex;
84                     for (valueIndex = separatorIndex; valueIndex < len; valueIndex++) {
85                         if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) {
86                             break;
87                         }
88                     }
89                     // Skip over one non whitespace key value separators if any
90
if (valueIndex < len) {
91                         if (strictKeyValueSeparators.indexOf(line.charAt(valueIndex)) != -1) {
92                             valueIndex++;
93                             // Skip over white space after other separators if any
94
}
95                     }
96                     while (valueIndex < len) {
97                         if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) {
98                             break;
99                         }
100                         valueIndex++;
101                     }
102                     String JavaDoc key = line.substring(keyStart, separatorIndex);
103                     String JavaDoc value = (separatorIndex < len) ? line.substring(valueIndex, len) : "";
104                     // Convert then store key and value
105
key = loadConvert(key);
106                     value = loadConvert(value);
107                     put(key, value);
108                 }
109             }
110         }
111     }
112     /*
113      * Returns true if the given line is a line that must
114      * be appended to the next line
115      */

116     private boolean continueLine (String JavaDoc line) {
117         int slashCount = 0;
118         int index = line.length() - 1;
119         while((index >= 0) && (line.charAt(index--) == '\\'))
120             slashCount++;
121         return (slashCount % 2 == 1);
122     }
123     /*
124      * Converts encoded \\uxxxx to unicode chars
125      * and changes special saved chars to their original forms
126      */

127     private String JavaDoc loadConvert (String JavaDoc theString) {
128         char aChar;
129         int len = theString.length();
130         StringBuffer JavaDoc outBuffer = new StringBuffer JavaDoc(len);
131         for(int x=0; x<len; ) {
132             aChar = theString.charAt(x++);
133             if (aChar == '\\') {
134                 aChar = theString.charAt(x++);
135                 if(aChar == 'u') {
136                     // Read the xxxx
137
int value=0;
138                     for (int i=0; i<4; i++) {
139                         aChar = theString.charAt(x++);
140                         switch (aChar) {
141                             case '0': case '1': case '2': case '3': case '4':
142                             case '5': case '6': case '7': case '8': case '9':
143                                 value = (value << 4) + aChar - '0';
144                                 break;
145                             case 'a': case 'b': case 'c':
146                             case 'd': case 'e': case 'f':
147                                 value = (value << 4) + 10 + aChar - 'a';
148                                 break;
149                             case 'A': case 'B': case 'C':
150                             case 'D': case 'E': case 'F':
151                                 value = (value << 4) + 10 + aChar - 'A';
152                                 break;
153                             default:
154                                 throw new IllegalArgumentException JavaDoc("Malformed \\uxxxx encoding.");
155                         }
156                     }
157                     outBuffer.append((char)value);
158                 } else {
159                     if (aChar == 't') aChar = '\t';
160                     else if (aChar == 'r') aChar = '\r';
161                     else if (aChar == 'n') aChar = '\n';
162                     else if (aChar == 'f') aChar = '\f';
163                     outBuffer.append(aChar);
164                     }
165             } else
166                 outBuffer.append(aChar);
167         }
168         return outBuffer.toString();
169     }
170 }
171
Popular Tags