1 11 package org.eclipse.pde.internal.core.util; 12 13 import java.io.PrintWriter ; 14 import java.util.Enumeration ; 15 16 import org.eclipse.jface.text.BadLocationException; 17 import org.eclipse.jface.text.IDocument; 18 19 public class PropertiesUtil { 20 private static final char[] HEX = 21 { 22 '0', 23 '1', 24 '2', 25 '3', 26 '4', 27 '5', 28 '6', 29 '7', 30 '8', 31 '9', 32 'A', 33 'B', 34 'C', 35 'D', 36 'E', 37 'F' }; 38 39 public static String createWritableName(String source) { 40 if (source.indexOf(' ') >= 0) { 41 StringBuffer writableName = new StringBuffer (); 43 for (int i = 0; i < source.length(); i++) { 44 char c = source.charAt(i); 45 if (c == ' ') { 46 writableName.append("\\ "); } else 48 writableName.append(c); 49 } 50 source = writableName.toString(); 51 } 52 return createEscapedValue(source); 53 } 54 55 public static String createEscapedValue(String value) { 56 StringBuffer buf = new StringBuffer (value.length() * 2); 58 for (int i = 0; i < value.length(); i++) { 60 char character = value.charAt(i); 61 if (character == '\\' 62 || character == '\t' 63 || character == '\r' 64 || character == '\n' 65 || character == '\f') { 66 buf.append('\\'); 68 buf.append(character); 69 } else if ((character < 0x0020) || (character > 0x007e)) { 70 buf.append('\\'); 72 buf.append('u'); 73 buf.append(HEX[(character >> 12) & 0xF]); buf.append(HEX[(character >> 8) & 0xF]); buf.append(HEX[(character >> 4) & 0xF]); buf.append(HEX[character & 0xF]); } else { 78 buf.append(character); 80 } 81 } 82 return buf.toString(); 83 } 84 85 88 public static String windEscapeChars(String s) { 89 if (s == null) 90 return null; 91 92 char aChar; 93 int len= s.length(); 94 StringBuffer outBuffer= new StringBuffer (len); 95 96 for (int x= 0; x < len;) { 97 aChar= s.charAt(x++); 98 if (aChar == '\\') { 99 if (x >= len) 100 break; 101 aChar= s.charAt(x++); 102 if (aChar == 'u') { 103 int value= 0; 105 for (int i= 0; i < 4; i++) { 106 if (x >= len) 107 break; 108 aChar= s.charAt(x++); 109 switch (aChar) { 110 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': 111 value= (value << 4) + aChar - '0'; 112 break; 113 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 114 value= (value << 4) + 10 + aChar - 'a'; 115 break; 116 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 117 value= (value << 4) + 10 + aChar - 'A'; 118 break; 119 default: 120 throw new IllegalArgumentException ("Malformed \\uxxxx encoding."); } 122 } 123 outBuffer.append((char) value); 124 } else { 125 if (aChar == 't') { 126 outBuffer.append('\t'); 127 } else { 128 if (aChar == 'r') { 129 outBuffer.append('\r'); 130 } else { 131 if (aChar == 'n') { 132 outBuffer.append('\n'); 133 } else { 134 if (aChar == 'f') { 135 outBuffer.append('\f'); 136 } else { 137 outBuffer.append(aChar); 138 } 139 } 140 } 141 } 142 } 143 } else 144 outBuffer.append(aChar); 145 } 146 return outBuffer.toString(); 147 } 148 public static void writeKeyValuePair(String indent, String name, String value, PrintWriter writer) { 149 String writableName = createWritableName(name); 150 writer.print(writableName + " = "); 152 writer.println(PropertiesUtil.createEscapedValue(value)); 153 } 154 155 public static void writeKeyValuePair(String indent, String name, Enumeration tokens, PrintWriter writer) { 156 String writableName = createWritableName(name); 157 writer.print(writableName + " = "); if (!tokens.hasMoreElements()) { 159 writer.println(); 160 return; 161 } 162 int indentLength = name.length() + 3; 163 for (;tokens.hasMoreElements();) { 164 String token = tokens.nextElement().toString(); 165 writer.print(PropertiesUtil.createEscapedValue(token)); 166 if (tokens.hasMoreElements()) { 167 writer.println(",\\"); for (int j = 0; j < indentLength; j++) { 169 writer.print(" "); } 171 } else 172 writer.println(""); } 174 } 175 176 public static int getInsertOffset(IDocument doc) { 177 int offset = doc.getLength(); 178 for (int i = doc.getNumberOfLines() - 1; i >= 0; i--) { 179 try { 180 if (doc.get(doc.getLineOffset(i), doc.getLineLength(i)).trim().length() > 0) { 181 break; 182 } 183 offset = doc.getLineOffset(i); 184 } catch (BadLocationException e) { 185 } 186 } 187 return offset; 188 } 189 190 public static boolean isNewlineNeeded(IDocument doc) throws BadLocationException { 191 int line = doc.getLineOfOffset(getInsertOffset(doc)); 192 return doc.get(doc.getLineOffset(line), doc.getLineLength(line)).trim().length() > 0; 193 } 194 195 } 196 | Popular Tags |