|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.qedeq.kernel.utility; |
|
19 |
| |
|
20 |
| import java.io.ByteArrayOutputStream; |
|
21 |
| import java.io.IOException; |
|
22 |
| import java.io.UnsupportedEncodingException; |
|
23 |
| import java.util.Properties; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| public final class StringUtility { |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
0
| private StringUtility() {
|
|
40 |
| |
|
41 |
| } |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
30464
| public static String replace(final String text,
|
|
53 |
| final String search, final String replace) { |
|
54 |
| |
|
55 |
30464
| final int len = search.length();
|
|
56 |
30464
| if (len == 0) {
|
|
57 |
0
| return text;
|
|
58 |
| } |
|
59 |
30464
| final StringBuffer result = new StringBuffer();
|
|
60 |
30464
| int pos1 = 0;
|
|
61 |
30464
| int pos2;
|
|
62 |
?
| while (0 <= (pos2 = text.indexOf(search, pos1))) {
|
|
63 |
13651
| result.append(text.substring(pos1, pos2));
|
|
64 |
13651
| result.append(replace);
|
|
65 |
13651
| pos1 = pos2 + len;
|
|
66 |
| } |
|
67 |
30464
| if (pos1 < text.length()) {
|
|
68 |
29302
| result.append(text.substring(pos1));
|
|
69 |
| } |
|
70 |
30464
| return result.toString();
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
30424
| public static void replace(final StringBuffer text,
|
|
83 |
| final String search, final String replace) { |
|
84 |
| |
|
85 |
30424
| final String result = replace(text.toString(), search, replace);
|
|
86 |
30424
| text.setLength(0);
|
|
87 |
30424
| text.append(result);
|
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
127
| public static String quote(final String unquoted) {
|
|
117 |
127
| String result = "\"";
|
|
118 |
| |
|
119 |
127
| for (int i = 0; i < unquoted.length(); i++) {
|
|
120 |
147
| if (unquoted.charAt(i) == '\"') {
|
|
121 |
0
| result += "\"\"";
|
|
122 |
| } else { |
|
123 |
147
| result += unquoted.charAt(i);
|
|
124 |
| } |
|
125 |
| } |
|
126 |
127
| result += '\"';
|
|
127 |
127
| return result;
|
|
128 |
| } |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
0
| public static boolean isLetterDigitString(final String text) {
|
|
140 |
0
| if (text.length() <= 0) {
|
|
141 |
0
| return false;
|
|
142 |
| } |
|
143 |
0
| if (!Character.isLetter(text.charAt(0))) {
|
|
144 |
0
| return false;
|
|
145 |
| } |
|
146 |
0
| for (int i = 1; i < text.length(); i++) {
|
|
147 |
0
| if (!Character.isLetterOrDigit(text.charAt(i))) {
|
|
148 |
0
| return false;
|
|
149 |
| } |
|
150 |
| } |
|
151 |
0
| return true;
|
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
78
| public static StringBuffer getSpaces(final int length) {
|
|
161 |
78
| final StringBuffer buffer = new StringBuffer(length >= 0 ? length : 0);
|
|
162 |
78
| for (int i = 0; i < length; i++) {
|
|
163 |
417
| buffer.append(' ');
|
|
164 |
| } |
|
165 |
78
| return buffer;
|
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
30222
| public static String getClassName(final Class clazz) {
|
|
175 |
30222
| return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
1875
| public static void deleteLineLeadingWhitespace(final StringBuffer buffer) {
|
|
200 |
1875
| int start = -1;
|
|
201 |
?
| while (0 <= (start = buffer.indexOf("\n", start + 1))) {
|
|
202 |
429
| if (start + 1 < buffer.length() && '\n' != buffer.charAt(start + 1)) {
|
|
203 |
384
| break;
|
|
204 |
| } |
|
205 |
| } |
|
206 |
1875
| if (start >= 0) {
|
|
207 |
384
| int next = start + 1;
|
|
208 |
384
| while (next < buffer.length() && Character.isWhitespace(buffer.charAt(next))
|
|
209 |
| && '\n' != buffer.charAt(next)) { |
|
210 |
5401
| next++;
|
|
211 |
| } |
|
212 |
384
| final String empty = buffer.substring(start, next);
|
|
213 |
384
| if (empty.length() > 0) {
|
|
214 |
384
| replace(buffer, empty, "\n");
|
|
215 |
| } |
|
216 |
| } |
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
0
| public static String escapeProperty(final String value) {
|
|
226 |
0
| Properties newprops = new Properties();
|
|
227 |
0
| newprops.put("key", value);
|
|
228 |
0
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
229 |
0
| try {
|
|
230 |
0
| newprops.store(out, null);
|
|
231 |
| } catch (IOException e) { |
|
232 |
0
| throw new RuntimeException(e);
|
|
233 |
| } |
|
234 |
0
| try {
|
|
235 |
0
| final String file = out.toString("ISO-8859-1");
|
|
236 |
0
| return file.substring(file.indexOf('\n') + 1 + "key=".length());
|
|
237 |
| } catch (UnsupportedEncodingException e) { |
|
238 |
0
| throw new RuntimeException(e);
|
|
239 |
| } |
|
240 |
| } |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
3560
| public static String decodeXmlMarkup(final StringBuffer value) {
|
|
249 |
| |
|
250 |
| |
|
251 |
3560
| replace(value, "<", "<");
|
|
252 |
3560
| replace(value, ">", ">");
|
|
253 |
3560
| replace(value, "&", "&");
|
|
254 |
3560
| return value.toString();
|
|
255 |
| } |
|
256 |
| |
|
257 |
| |
|
258 |
| } |