1 30 package com.genimen.djeneric.util; 31 32 import java.io.BufferedWriter ; 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.io.OutputStreamWriter ; 36 import java.util.ArrayList ; 37 import java.util.Collections ; 38 import java.util.Comparator ; 39 import java.util.Date ; 40 import java.util.ListIterator ; 41 import java.util.Properties ; 42 43 47 48 55 56 class KeyComparator implements Comparator 57 { 58 public int compare(Object o1, Object o2) 59 { 60 String s1 = (String ) o1; 61 String s2 = (String ) o2; 62 63 int dotCount1 = countDots(s1); 64 int dotCount2 = countDots(s2); 65 66 if (dotCount1 == dotCount2) return s1.compareTo(s2); 67 68 return dotCount1 - dotCount2; 69 } 70 71 public static int countDots(String s) 72 { 73 int idx = s.indexOf("."); 74 int count = 0; 75 while (idx >= 0) 76 { 77 count++; 78 idx = s.indexOf(".", idx + 1); 79 } 80 return count; 81 } 82 83 public boolean equals(Object obj) 84 { 85 return obj == this; 86 } 87 88 } 89 90 public class DjProperties extends Properties 91 { 92 private static final long serialVersionUID = 1L; 93 94 private static final String specialSaveChars = "=: \t\r\n\f#!"; 95 96 public synchronized void store(OutputStream out, String header) throws IOException 97 { 98 BufferedWriter awriter; 99 awriter = new BufferedWriter (new OutputStreamWriter (out, "8859_1")); 100 if (header != null) writeln(awriter, "#" + header); 101 102 writeln(awriter, "#" + new Date ().toString()); 103 104 ArrayList sortedKeys = new ArrayList (size()); 105 106 sortedKeys.addAll(this.keySet()); 107 Collections.sort(sortedKeys, new KeyComparator()); 108 109 String prevGroupKey = ""; 110 int prevDotCount = -1; 111 int groupCount = 1; 112 for (ListIterator e = sortedKeys.listIterator(); e.hasNext();) 113 { 114 String key = (String ) e.next(); 115 String val = (String ) get(key); 116 key = saveConvert(key, true); 117 118 String groupKey = key; 119 int dotIdx = key.lastIndexOf("."); 120 boolean endGroup = false; 121 if (dotIdx != -1) 122 { 123 groupKey = key.substring(0, dotIdx); 124 if (groupKey.equals(prevGroupKey)) groupCount++; 125 else if (groupCount > 1) { 127 endGroup = true; 128 } 129 } 130 else if (groupCount > 1) { 132 endGroup = true; 133 } 134 135 int dotCount = KeyComparator.countDots(key); 136 if (dotCount != prevDotCount) endGroup = true; 137 prevDotCount = dotCount; 138 139 if (endGroup) 140 { 141 awriter.newLine(); groupCount = 1; } 144 145 prevGroupKey = groupKey; 146 147 150 val = saveConvert(val, false); 151 writeln(awriter, key + "=" + val); 152 } 153 awriter.flush(); 154 } 155 156 private static void writeln(BufferedWriter bw, String s) throws IOException 157 { 158 bw.write(s); 159 bw.newLine(); 160 } 161 162 167 private String saveConvert(String theString, boolean escapeSpace) 168 { 169 int len = theString.length(); 170 StringBuffer outBuffer = new StringBuffer (len * 2); 171 172 for (int x = 0; x < len; x++) 173 { 174 char aChar = theString.charAt(x); 175 switch (aChar) 176 { 177 case ' ' : 178 if (x == 0 || escapeSpace) outBuffer.append('\\'); 179 180 outBuffer.append(' '); 181 break; 182 case '\\' : 183 outBuffer.append('\\'); 184 outBuffer.append('\\'); 185 break; 186 case '\t' : 187 outBuffer.append('\\'); 188 outBuffer.append('t'); 189 break; 190 case '\n' : 191 outBuffer.append('\\'); 192 outBuffer.append('n'); 193 break; 194 case '\r' : 195 outBuffer.append('\\'); 196 outBuffer.append('r'); 197 break; 198 case '\f' : 199 outBuffer.append('\\'); 200 outBuffer.append('f'); 201 break; 202 default : 203 if ((aChar < 0x0020) || (aChar > 0x007e)) 204 { 205 outBuffer.append('\\'); 206 outBuffer.append('u'); 207 outBuffer.append(toHex((aChar >> 12) & 0xF)); 208 outBuffer.append(toHex((aChar >> 8) & 0xF)); 209 outBuffer.append(toHex((aChar >> 4) & 0xF)); 210 outBuffer.append(toHex(aChar & 0xF)); 211 } 212 else 213 { 214 if (specialSaveChars.indexOf(aChar) != -1) outBuffer.append('\\'); 215 outBuffer.append(aChar); 216 } 217 } 218 } 219 return outBuffer.toString(); 220 } 221 222 private static char toHex(int nibble) 223 { 224 return hexDigit[(nibble & 0xF)]; 225 } 226 227 228 private static final char[] hexDigit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 229 'F' }; 230 231 } | Popular Tags |