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
|
/**
* Description: operations for the a text string.
*
* @ Author Create/Modi Note
* Xiaofeng Xie Feb 22, 2001
* Xiaofeng Xie May 12, 2004
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Please acknowledge the author(s) if you use this code in any way.
*
* @version 1.0
* @Since MAOS1.0
*/
package net.adaptivebox.global;
import java.io.*;
import java.util.*;
public class GlobalString {
private static final String NEGLECT_TAG = "#$@";
/**
* Tokenize a String with given key.
* @param input the String to be tokenized.
* @param tokenKey the delimiters.
* @return a String array that include the elements of input string that
* divided by the tokenKey.
*/
public static String[] tokenize(String input , String tokenKey) {
ArrayList<String> v = new ArrayList<String>();
StringTokenizer t = new StringTokenizer(input, tokenKey);
while (t.hasMoreTokens())
v.add(t.nextToken());
return v.toArray(new String[v.size()]);
}
private static int getCharLoc(char data, String str) {
for(int i=0; i<str.length(); i++) {
if(str.charAt(i)==data) return i;
}
return -1;
}
private static String trim(String origStr, String discardStr) {
String str = origStr;
do {
if(str.length()==0) return str;
if(getCharLoc(str.charAt(0), discardStr)!=-1) str = str.substring(1);
else if(getCharLoc(str.charAt(str.length()-1), discardStr)!=-1) str = str.substring(0, str.length()-1);
else {return str;}
} while(true);
}
private static boolean getFirstCharExist(String str, String chars) throws Exception {
int neglectFirstCharLength = chars.length();
for(int i=0; i<neglectFirstCharLength; i++) {
if(str.startsWith(chars.substring(i, i+1))) {
return true;
}
}
return false;
}
private static String getMeaningfulLine(BufferedReader outReader, String neglectFirstChars) throws Exception {
String str;
boolean isNeglect = true;
do {
str = outReader.readLine();
if (str==null) {
return null;
}
str = trim(str, " \t");
if(str.length()>0) {
isNeglect = getFirstCharExist(str, neglectFirstChars);
}
} while (isNeglect);
return str;
}
private static String[] getMeaningfulLines(String srcStr, String neglectFirstChars) throws Exception {
StringReader outStringReader = new StringReader(srcStr);
BufferedReader outReader = new BufferedReader(outStringReader);
ArrayList<String> origData = new ArrayList<String>();
while(true) {
String str = getMeaningfulLine(outReader, neglectFirstChars);
if (str==null) {
break;
}
origData.add(str);
}
return convert1DVectorToStringArray(origData);
}
/**
* convert vector to 1D String array
*/
private static String[] convert1DVectorToStringArray(ArrayList<String> toToConvert) {
if (toToConvert==null) return null;
String[] objs = new String[toToConvert.size()];
for (int i=0; i<toToConvert.size(); i++) {
objs[i] = getObjString(toToConvert.get(i));
}
return(objs);
}
private static String getObjString(Object nObj) {
if(nObj instanceof String) return (String)nObj;
return nObj.toString();
}
}
|