-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTalendString.java
More file actions
331 lines (313 loc) · 11.8 KB
/
Copy pathTalendString.java
File metadata and controls
331 lines (313 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// ============================================================================
//
// Copyright (c) 2006-2015, Talend Inc.
//
// This source code has been automatically generated by_Talend Open Studio for Big Data
// / Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================
package routines;
import java.util.Random;
import java.util.Vector;
public class TalendString {
/** Index of the first accent character **/
private static final int MIN = 192;
/** Index of the last accent character **/
private static final int MAX = 255;
/** used to save the link between with or without accents **/
private static final Vector map = initMap();
public static Vector getMap() {
return map;
}
/**
* return Replace the special character(e.g. <,>,& etc) within a string for XML file.
*
*
* {talendTypes} String
*
* {Category} TalendString
*
* {param} string("") input: The string with the special character(s) need to be replaced.
*
* {example} replaceSpecialCharForXML("<title>Empire <>Burlesque</title>") # <title>Empire <>Burlesque</title>
*/
public static String replaceSpecialCharForXML(String input) {
input = input.replaceAll("&", "&"); //$NON-NLS-1$ //$NON-NLS-2$
input = input.replaceAll("<", "<"); //$NON-NLS-1$ //$NON-NLS-2$
input = input.replaceAll(">", ">"); //$NON-NLS-1$ //$NON-NLS-2$
input = input.replaceAll("'", "'"); //$NON-NLS-1$ //$NON-NLS-2$
input = input.replaceAll("\"", """); //$NON-NLS-1$ //$NON-NLS-2$
return input;
}
/**
* check CDATA for xml
*
* {talendTypes} String
*
* {Category} TalendString
*
* {param} string("") input: the CDATA format data to be checked.
*/
public static String checkCDATAForXML(String input) {
if (input.startsWith("<![CDATA[") && input.endsWith("]]>")) { //$NON-NLS-1$ //$NON-NLS-2$
return input;
} else {
return TalendString.replaceSpecialCharForXML(input);
}
}
/**
* getAsciiRandomString : Return a randomly generated String
*
*
* {talendTypes} String
*
* {Category} TalendString
*
* {param} int(6) length: length of the String to return
*
* {example} getAsciiRandomString(6) # Art34Z
*/
public static String getAsciiRandomString(int length) {
Random random = new Random();
int cnt = 0;
StringBuffer buffer = new StringBuffer();
char ch;
int end = 'z' + 1;
int start = ' ';
while (cnt < length) {
ch = (char) (random.nextInt(end - start) + start);
if (Character.isLetterOrDigit(ch)) {
buffer.append(ch);
cnt++;
}
}
return buffer.toString();
}
/**
* talendTrim: Returns a copy of the string, with leading and trailing specified char omitted.
*
*
* {talendTypes} String
*
* {Category} TalendString
*
* {param} string("") origin: The original string need to be trimed.
*
* {param} char(' ') padding_char: The padding char for triming.
*
* {param} int(0) align: The alignment of the content in the original string. Positive int for right, negative int
* for left and zero for center. Positive integer to trim the left part, zero to trim both the left and the right part, negative to trim the right part.
*
*
* {example} talendTrim("$$talend open studio$$$$", '$', 0) # talend open studio
*/
public static String talendTrim(String origin, char padding_char, int align) {
if (null == origin) {
return null;
}
String sPaddingChar = quoteChars(Character.toString(padding_char));
if (align > 0) {// positive integer to trim left
origin = origin.replaceAll("^" + sPaddingChar + "+", "");
} else if (align == 0) {// zero to trim both left and right
origin = origin.replaceAll("^" + sPaddingChar + "+", "");
origin = origin.replaceAll(sPaddingChar + "+$", "");
} else if (align < 0) {// negative integer to trim right
origin = origin.replaceAll(sPaddingChar + "+$", "");
}
return origin;
}
private static String quoteChars( String padding_chars){
StringBuffer sb = new StringBuffer();
for (int i = 0; i < padding_chars.length(); i++) {
char c = padding_chars.charAt(i);
if (!(Character.isLetterOrDigit(c) || c == '_')) {
sb.append('\\');
sb.append(c);
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* self definition:escape char ,for example -->'\\'
*/
public static String addEscapeChars(String padding_chars,char escapeChar){
StringBuffer sb = new StringBuffer();
for (int i = 0; i < padding_chars.length(); i++) {
char c = padding_chars.charAt(i);
if (!(Character.isLetterOrDigit(c) || c == '_'||Character.isWhitespace(c))) {
sb.append(escapeChar);
sb.append(c);
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* Initialisation of the map for the accents.
*/
private static Vector initMap() {
Vector result = new Vector();
String car = null;
car = new String("A"); //$NON-NLS-1$
result.add(car); /* '\u00C0' alt-0192 */
result.add(car); /* '\u00C1' alt-0193 */
result.add(car); /* '\u00C2' alt-0194 */
result.add(car); /* '\u00C3' alt-0195 */
result.add(car); /* '\u00C4' alt-0196 */
result.add(car); /* '\u00C5' alt-0197 */
car = new String("AE"); //$NON-NLS-1$
result.add(car); /* '\u00C6' alt-0198 */
car = new String("C"); //$NON-NLS-1$
result.add(car); /* '\u00C7' alt-0199 */
car = new String("E"); //$NON-NLS-1$
result.add(car); /* '\u00C8' alt-0200 */
result.add(car); /* '\u00C9' alt-0201 */
result.add(car); /* '\u00CA' alt-0202 */
result.add(car); /* '\u00CB' alt-0203 */
car = new String("I"); //$NON-NLS-1$
result.add(car); /* '\u00CC' alt-0204 */
result.add(car); /* '\u00CD' alt-0205 */
result.add(car); /* '\u00CE' alt-0206 */
result.add(car); /* '\u00CF' alt-0207 */
car = new String("D"); //$NON-NLS-1$
result.add(car); /* '\u00D0' alt-0208 */
car = new String("N"); //$NON-NLS-1$
result.add(car); /* '\u00D1' alt-0209 */
car = new String("O"); //$NON-NLS-1$
result.add(car); /* '\u00D2' alt-0210 */
result.add(car); /* '\u00D3' alt-0211 */
result.add(car); /* '\u00D4' alt-0212 */
result.add(car); /* '\u00D5' alt-0213 */
result.add(car); /* '\u00D6' alt-0214 */
car = new String("*"); //$NON-NLS-1$
result.add(car); /* '\u00D7' alt-0215 */
car = new String("0"); //$NON-NLS-1$
result.add(car); /* '\u00D8' alt-0216 */
car = new String("U"); //$NON-NLS-1$
result.add(car); /* '\u00D9' alt-0217 */
result.add(car); /* '\u00DA' alt-0218 */
result.add(car); /* '\u00DB' alt-0219 */
result.add(car); /* '\u00DC' alt-0220 */
car = new String("Y"); //$NON-NLS-1$
result.add(car); /* '\u00DD' alt-0221 */
car = new String("_"); //$NON-NLS-1$
result.add(car); /* '\u00DE' alt-0222 */
car = new String("B"); //$NON-NLS-1$
result.add(car); /* '\u00DF' alt-0223 */
car = new String("a"); //$NON-NLS-1$
result.add(car); /* '\u00E0' alt-0224 */
result.add(car); /* '\u00E1' alt-0225 */
result.add(car); /* '\u00E2' alt-0226 */
result.add(car); /* '\u00E3' alt-0227 */
result.add(car); /* '\u00E4' alt-0228 */
result.add(car); /* '\u00E5' alt-0229 */
car = new String("ae"); //$NON-NLS-1$
result.add(car); /* '\u00E6' alt-0230 */
car = new String("c"); //$NON-NLS-1$
result.add(car); /* '\u00E7' alt-0231 */
car = new String("e"); //$NON-NLS-1$
result.add(car); /* '\u00E8' alt-0232 */
result.add(car); /* '\u00E9' alt-0233 */
result.add(car); /* '\u00EA' alt-0234 */
result.add(car); /* '\u00EB' alt-0235 */
car = new String("i"); //$NON-NLS-1$
result.add(car); /* '\u00EC' alt-0236 */
result.add(car); /* '\u00ED' alt-0237 */
result.add(car); /* '\u00EE' alt-0238 */
result.add(car); /* '\u00EF' alt-0239 */
car = new String("d"); //$NON-NLS-1$
result.add(car); /* '\u00F0' alt-0240 */
car = new String("n"); //$NON-NLS-1$
result.add(car); /* '\u00F1' alt-0241 */
car = new String("o"); //$NON-NLS-1$
result.add(car); /* '\u00F2' alt-0242 */
result.add(car); /* '\u00F3' alt-0243 */
result.add(car); /* '\u00F4' alt-0244 */
result.add(car); /* '\u00F5' alt-0245 */
result.add(car); /* '\u00F6' alt-0246 */
car = new String("/"); //$NON-NLS-1$
result.add(car); /* '\u00F7' alt-0247 */
car = new String("0"); //$NON-NLS-1$
result.add(car); /* '\u00F8' alt-0248 */
car = new String("u"); //$NON-NLS-1$
result.add(car); /* '\u00F9' alt-0249 */
result.add(car); /* '\u00FA' alt-0250 */
result.add(car); /* '\u00FB' alt-0251 */
result.add(car); /* '\u00FC' alt-0252 */
car = new String("y"); //$NON-NLS-1$
result.add(car); /* '\u00FD' alt-0253 */
car = new String("_"); //$NON-NLS-1$
result.add(car); /* '\u00FE' alt-0254 */
car = new String("y"); //$NON-NLS-1$
result.add(car); /* '\u00FF' alt-0255 */
result.add(car); /* '\u00FF' alt-0255 */
return result;
}
/**
* removeAccents: remove accents from the string given.
*
*
* {talendTypes} String
*
* {Category} TalendString
*
* {param} string("") text: Text to remove accents.
*
*
* {example} removeAccents("Accès à la base")
*/
public static String removeAccents(String text) {
StringBuffer result = new StringBuffer();
for (int bcl = 0; bcl < text.length(); bcl++) {
char carVal = text.charAt(bcl);
if (carVal >= 192 && carVal <= 255) {
String newVal = (String) map.get(carVal - 192);
result.append(newVal);
} else {
result.append(carVal);
}
}
return result.toString();
}
/**
* unionString: Union the variable number of arguments with separator String
*
* @param separator union arguments .
* @param objects variable number of arguments.
* @return A union string.
*
* {talendTypes} String
*
* {Category} TalendString
*
*/
public static String unionString(String separator,Object... objects){
if(objects!=null){
String value = "";
boolean isFirst = true;
for(Object obj:objects){
if(isFirst){
value = String.valueOf(obj);
isFirst = false;
}else{
value = value +separator + String.valueOf(obj);
}
}
return value;
}else{
return null;
}
}
}