KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > nls > StringHelper


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.nls;
12
13 public class StringHelper {
14     
15     protected static String JavaDoc preparePropertiesString(String JavaDoc s, char[] newLine) {
16         if (s == null)
17             return null;
18         int length = s.length();
19         int nlLength = newLine.length;
20         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(length + nlLength);
21         for (int i = 0; i < length; i++) {
22             char c = s.charAt(i);
23             if (i + nlLength < length) {
24                 boolean notNewLine = false;
25                 for (int j = 0; j < nlLength; j++)
26                     if (s.charAt(i + j) != newLine[j])
27                         notNewLine = true;
28                 if (!notNewLine)
29                     sb.append("\\"); //$NON-NLS-1$
30
}
31             sb.append(c);
32         }
33         return sb.toString();
34     }
35     
36     protected static String JavaDoc unwindEscapeChars(String JavaDoc s) {
37         if (s != null) {
38             int length = s.length();
39             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(length);
40             for (int i = 0; i < length; i++) {
41                 char c = s.charAt(i);
42                 sb.append(getUnwoundString(c));
43             }
44             return sb.toString();
45         }
46         return null;
47     }
48
49     protected static String JavaDoc getUnwoundString(char c) {
50         switch (c) {
51             case '\b' :
52                 return "\\b";//$NON-NLS-1$
53
case '\t' :
54                 return "\\t";//$NON-NLS-1$
55
case '\n' :
56                 return "\\n";//$NON-NLS-1$
57
case '\f' :
58                 return "\\f";//$NON-NLS-1$
59
case '\r' :
60                 return "\\r";//$NON-NLS-1$
61
case '\\' :
62                 return "\\\\";//$NON-NLS-1$
63
}
64         return String.valueOf(c);
65     }
66
67     protected static String JavaDoc windEscapeChars(String JavaDoc s) {
68         if (s == null)
69             return null;
70
71         char aChar;
72         int len= s.length();
73         StringBuffer JavaDoc outBuffer= new StringBuffer JavaDoc(len);
74
75         for (int x= 0; x < len;) {
76             aChar= s.charAt(x++);
77             if (aChar == '\\') {
78                 aChar= s.charAt(x++);
79                 if (aChar == 'u') {
80                     // Read the xxxx
81
int value= 0;
82                     for (int i= 0; i < 4; i++) {
83                         aChar= s.charAt(x++);
84                         switch (aChar) {
85                             case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
86                                 value= (value << 4) + aChar - '0';
87                                 break;
88                             case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
89                                 value= (value << 4) + 10 + aChar - 'a';
90                                 break;
91                             case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
92                                 value= (value << 4) + 10 + aChar - 'A';
93                                 break;
94                             default:
95                                 throw new IllegalArgumentException JavaDoc("Malformed \\uxxxx encoding."); //$NON-NLS-1$
96
}
97                     }
98                     outBuffer.append((char) value);
99                 } else {
100                     if (aChar == 't') {
101                         outBuffer.append('\t');
102                     } else {
103                         if (aChar == 'r') {
104                             outBuffer.append('\r');
105                         } else {
106                             if (aChar == 'n') {
107                                 outBuffer.append('\n');
108                             } else {
109                                 if (aChar == 'f') {
110                                     outBuffer.append('\f');
111                                 } else {
112                                     outBuffer.append(aChar);
113                                 }
114                             }
115                         }
116                     }
117                 }
118             } else
119                 outBuffer.append(aChar);
120         }
121         return outBuffer.toString();
122     }
123     
124     protected static boolean isValidLocalization(String JavaDoc name) {
125         if (name.length() <= 0) {
126             return false;
127         }
128         for (int i = 0; i < name.length(); i++) {
129             char c = name.charAt(i);
130             if ((c < 'A' || 'Z' < c) && (c < 'a' || 'z' < c)
131                     && (c < '0' || '9' < c) && c != '_' && c != '-' && c != '/') {
132                 return false;
133             }
134         }
135         return true;
136     }
137 }
138
Popular Tags