2 * Description: operations for the a text string.
4 * @ Author Create/Modi Note
5 * Xiaofeng Xie Feb 22, 2001
6 * Xiaofeng Xie May 12, 2004
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * Please acknowledge the author(s) if you use this code in any way.
24 package net
.adaptivebox
.global
;
29 public class GlobalString
{
30 public static final String NEGLECT_TAG
= "#$@";
31 public static final String EQUAL_TAG
= "=";
34 * Tokenize a String with given key.
35 * @param input the String to be tokenized.
36 * @param tokenKey the delimiters.
37 * @return a String array that include the elements of input string that
38 * divided by the tokenKey.
40 public static String
[] tokenize(String input
, String tokenKey
) {
41 ArrayList
<String
> v
= new ArrayList
<String
>();
42 StringTokenizer t
= new StringTokenizer(input
, tokenKey
);
43 while (t
.hasMoreTokens())
45 return v
.toArray(new String
[v
.size()]);
48 public static String
[] getMeaningfulLines(String srcStr
) throws Exception
{
49 return getMeaningfulLines(srcStr
, NEGLECT_TAG
);
52 public static String
getMeaningfulLine(BufferedReader outReader
) throws Exception
{
53 return getMeaningfulLine(outReader
, NEGLECT_TAG
);
56 public static int getCharLoc(char data
, String str
) {
57 for(int i
=0; i
<str
.length(); i
++) {
58 if(str
.charAt(i
)==data
) return i
;
62 public static String
trim(String origStr
, String discardStr
) {
65 if(str
.length()==0) return str
;
66 if(getCharLoc(str
.charAt(0), discardStr
)!=-1) str
= str
.substring(1);
67 else if(getCharLoc(str
.charAt(str
.length()-1), discardStr
)!=-1) str
= str
.substring(0, str
.length()-1);
72 public static boolean getFirstCharExist(String str
, String chars
) throws Exception
{
73 int neglectFirstCharLength
= chars
.length();
74 for(int i
=0; i
<neglectFirstCharLength
; i
++) {
75 if(str
.startsWith(chars
.substring(i
, i
+1))) {
82 public static String
getMeaningfulLine(BufferedReader outReader
, String neglectFirstChars
) throws Exception
{
84 boolean isNeglect
= true;
87 str
= outReader
.readLine();
91 str
= trim(str
, " \t");
93 isNeglect
= getFirstCharExist(str
, neglectFirstChars
);
99 public static String
[] getMeaningfulLines(String srcStr
, String neglectFirstChars
) throws Exception
{
100 StringReader outStringReader
= new StringReader(srcStr
);
101 BufferedReader outReader
= new BufferedReader(outStringReader
);
102 ArrayList
<String
> origData
= new ArrayList
<String
>();
104 String str
= getMeaningfulLine(outReader
, neglectFirstChars
);
110 return convert1DVectorToStringArray(origData
);
114 * convert vector to 1D String array
116 public static String
[] convert1DVectorToStringArray(ArrayList
<String
> toToConvert
) {
117 if (toToConvert
==null) return null;
118 String
[] objs
= new String
[toToConvert
.size()];
119 for (int i
=0; i
<toToConvert
.size(); i
++) {
120 objs
[i
] = getObjString(toToConvert
.get(i
));
125 public static String
getObjString(Object nObj
) {
126 if(nObj
instanceof String
) return (String
)nObj
;
127 return nObj
.toString();
130 static public int toInteger(Object oVal
) throws Exception
{
131 if(oVal
==null) throw new Exception("Null string");
132 return new Integer(oVal
.toString()).intValue();
135 static public double toDouble(Object oVal
) throws Exception
{
136 if(oVal
==null) throw new Exception("Null string");
137 return new Double(oVal
.toString()).doubleValue();
140 public static Object
toObject(String key
) throws Exception
{
141 Class cls
= Class
.forName(key
);
142 return cls
.newInstance();