1 13 14 package org.ejbca.ui.web.admin.configuration; 15 16 import java.io.BufferedReader ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.InputStreamReader ; 20 21 25 public class LanguageProperties extends java.util.Properties { 26 private static final String keyValueSeparators = "=: \t\r\n\f"; 27 private static final String strictKeyValueSeparators = "=:"; 28 private static final String whiteSpaceChars = " \t\r\n\f"; 30 31 public synchronized void load(InputStream inStream) throws IOException { 32 BufferedReader in = new BufferedReader (new InputStreamReader (inStream)); while (true) { 34 String line = in.readLine(); 36 if (line == null) { 37 return; 38 } 39 if (line.length() > 0) { 40 char firstChar = line.charAt(0); 42 if ( (firstChar != '#') && (firstChar != '!')) { 43 while (continueLine(line)) { 44 String nextLine = in.readLine(); 45 if (nextLine == null) { 46 nextLine = ""; 47 } 48 String loppedLine = line.substring(0, line.length() - 1); 49 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 (loppedLine + nextLine); 58 } 59 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 if (keyStart == len) { 69 continue; 70 } 71 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 int valueIndex; 84 for (valueIndex = separatorIndex; valueIndex < len; valueIndex++) { 85 if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) { 86 break; 87 } 88 } 89 if (valueIndex < len) { 91 if (strictKeyValueSeparators.indexOf(line.charAt(valueIndex)) != -1) { 92 valueIndex++; 93 } 95 } 96 while (valueIndex < len) { 97 if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) { 98 break; 99 } 100 valueIndex++; 101 } 102 String key = line.substring(keyStart, separatorIndex); 103 String value = (separatorIndex < len) ? line.substring(valueIndex, len) : ""; 104 key = loadConvert(key); 106 value = loadConvert(value); 107 put(key, value); 108 } 109 } 110 } 111 } 112 116 private boolean continueLine (String 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 127 private String loadConvert (String theString) { 128 char aChar; 129 int len = theString.length(); 130 StringBuffer outBuffer = new StringBuffer (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 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 ("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 |