KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > PropertiesUtil


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.util;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.Enumeration JavaDoc;
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 JavaDoc createWritableName(String JavaDoc source) {
40         if (source.indexOf(' ') >= 0) {
41             // has blanks
42
StringBuffer JavaDoc writableName = new StringBuffer JavaDoc();
43             for (int i = 0; i < source.length(); i++) {
44                 char c = source.charAt(i);
45                 if (c == ' ') {
46                     writableName.append("\\ "); //$NON-NLS-1$
47
} else
48                     writableName.append(c);
49             }
50             source = writableName.toString();
51         }
52         return createEscapedValue(source);
53     }
54
55     public static String JavaDoc createEscapedValue(String JavaDoc value) {
56         // if required, escape property values as \\uXXXX
57
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(value.length() * 2);
58         // assume expansion by less than factor of 2
59
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                 // handle characters requiring leading \
67
buf.append('\\');
68                 buf.append(character);
69             } else if ((character < 0x0020) || (character > 0x007e)) {
70                 // handle characters outside base range (encoded)
71
buf.append('\\');
72                 buf.append('u');
73                 buf.append(HEX[(character >> 12) & 0xF]); // first nibble
74
buf.append(HEX[(character >> 8) & 0xF]); // second nibble
75
buf.append(HEX[(character >> 4) & 0xF]); // third nibble
76
buf.append(HEX[character & 0xF]); // fourth nibble
77
} else {
78                 // handle base characters
79
buf.append(character);
80             }
81         }
82         return buf.toString();
83     }
84     
85     /*
86      * Copied from org.eclipse.jdt.internal.ui.refactoring.nls.ExternalizeWizardPage.windEscapeChars(String s)
87      */

88     public static String JavaDoc windEscapeChars(String JavaDoc s) {
89         if (s == null)
90             return null;
91
92         char aChar;
93         int len= s.length();
94         StringBuffer JavaDoc outBuffer= new StringBuffer JavaDoc(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                     // Read the xxxx
104
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 JavaDoc("Malformed \\uxxxx encoding."); //$NON-NLS-1$
121
}
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 JavaDoc indent, String JavaDoc name, String JavaDoc value, PrintWriter JavaDoc writer) {
149         String JavaDoc writableName = createWritableName(name);
150         writer.print(writableName + " = "); //$NON-NLS-1$
151

152         writer.println(PropertiesUtil.createEscapedValue(value));
153     }
154
155     public static void writeKeyValuePair(String JavaDoc indent, String JavaDoc name, Enumeration JavaDoc tokens, PrintWriter JavaDoc writer) {
156         String JavaDoc writableName = createWritableName(name);
157         writer.print(writableName + " = "); //$NON-NLS-1$
158
if (!tokens.hasMoreElements()) {
159             writer.println();
160             return;
161         }
162         int indentLength = name.length() + 3;
163         for (;tokens.hasMoreElements();) {
164             String JavaDoc token = tokens.nextElement().toString();
165             writer.print(PropertiesUtil.createEscapedValue(token));
166             if (tokens.hasMoreElements()) {
167                 writer.println(",\\"); //$NON-NLS-1$
168
for (int j = 0; j < indentLength; j++) {
169                     writer.print(" "); //$NON-NLS-1$
170
}
171             } else
172                 writer.println(""); //$NON-NLS-1$
173
}
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