1 : |
dgarcia |
1 |
package eu.smartlm.abs.portal.util;
|
2 : |
|
|
|
3 : |
|
|
import java.text.DateFormat;
|
4 : |
|
|
import java.text.Format;
|
5 : |
|
|
import java.text.ParseException;
|
6 : |
|
|
import java.text.SimpleDateFormat;
|
7 : |
|
|
import java.util.Calendar;
|
8 : |
|
|
import java.util.Date;
|
9 : |
|
|
import java.util.GregorianCalendar;
|
10 : |
|
|
import java.util.HashSet;
|
11 : |
|
|
import java.util.Set;
|
12 : |
|
|
import java.util.StringTokenizer;
|
13 : |
|
|
|
14 : |
|
|
import eu.smartlm.abs.portal.view.data.ABSQuery;
|
15 : |
|
|
|
16 : |
|
|
/**
|
17 : |
|
|
* Collection of repetitive string parse methods for the Portal
|
18 : |
|
|
* @author David García Pérez - CESGA
|
19 : |
|
|
*
|
20 : |
|
|
*/
|
21 : |
|
|
public class StringUtil {
|
22 : |
|
|
|
23 : |
|
|
public static String MARKER = "#######################";
|
24 : |
|
|
|
25 : |
|
|
/**
|
26 : |
|
|
* Takes a list of strings and gives back an unique string containing all the strings putting
|
27 : |
|
|
* a blank space between them
|
28 : |
|
|
* @param listOfStrings
|
29 : |
|
|
* @return
|
30 : |
|
|
*/
|
31 : |
|
|
public static String listToString(Set<String> listOfStrings) {
|
32 : |
|
|
|
33 : |
|
|
String result = "";
|
34 : |
|
|
|
35 : |
|
|
for(String piece : listOfStrings) {
|
36 : |
|
|
result = result + " " + piece;
|
37 : |
|
|
}
|
38 : |
|
|
// We remove the first blank space
|
39 : |
|
|
if(result.length() > 1)
|
40 : |
|
|
result = result.substring(1);
|
41 : |
|
|
|
42 : |
|
|
return result;
|
43 : |
|
|
}
|
44 : |
|
|
|
45 : |
|
|
public static String listToStringWithMarker(Set<String> listOfStrings) {
|
46 : |
|
|
|
47 : |
|
|
String result = "";
|
48 : |
|
|
|
49 : |
|
|
for(String piece : listOfStrings) {
|
50 : |
|
|
result = result + MARKER + piece;
|
51 : |
|
|
}
|
52 : |
|
|
// We remove the first blank space
|
53 : |
|
|
if(result.length() > 1)
|
54 : |
|
|
result = result.substring(MARKER.length());
|
55 : |
|
|
|
56 : |
|
|
return result;
|
57 : |
|
|
}
|
58 : |
|
|
|
59 : |
|
|
/**
|
60 : |
|
|
* Takes a list of strings and gives back an unique string containing all the strings putting
|
61 : |
|
|
* a comma space between them
|
62 : |
|
|
* @param listOfStrings
|
63 : |
|
|
* @return
|
64 : |
|
|
*/
|
65 : |
|
|
public static String listToStringWithCommas(Set<String> listOfStrings) {
|
66 : |
|
|
|
67 : |
|
|
String result = "";
|
68 : |
|
|
|
69 : |
|
|
for(String piece : listOfStrings) {
|
70 : |
|
|
result = result + ", " + piece;
|
71 : |
|
|
}
|
72 : |
|
|
// We remove the first blank space
|
73 : |
|
|
if(result.length() > 2)
|
74 : |
|
|
result = result.substring(2);
|
75 : |
|
|
|
76 : |
|
|
return result;
|
77 : |
|
|
}
|
78 : |
|
|
|
79 : |
|
|
/**
|
80 : |
|
|
* Takes a string separated by spaces and converts it back to a list of strings
|
81 : |
|
|
* @param toBeParsed String to be converted to a list of strings
|
82 : |
|
|
* @return
|
83 : |
|
|
*/
|
84 : |
|
|
public static Set<String> stringToList(String toBeParsed) {
|
85 : |
|
|
Set<String> result = new HashSet<String>();
|
86 : |
|
|
String pattern = " ";
|
87 : |
|
|
String[] fields = toBeParsed.split(pattern);
|
88 : |
|
|
|
89 : |
|
|
for(int i = 0; i < fields.length; i++) {
|
90 : |
|
|
if(fields[i] != "") result.add(fields[i]);
|
91 : |
|
|
}
|
92 : |
|
|
|
93 : |
|
|
Set<String> toRemove = new HashSet<String>();
|
94 : |
|
|
for(String text : result) {
|
95 : |
|
|
if(text.equals(""))
|
96 : |
|
|
toRemove.add(text);
|
97 : |
|
|
}
|
98 : |
|
|
result.removeAll(toRemove);
|
99 : |
|
|
|
100 : |
|
|
return result;
|
101 : |
|
|
}
|
102 : |
|
|
|
103 : |
|
|
/**
|
104 : |
|
|
* Takes a string separated by spaces and converts it back to a list of strings
|
105 : |
|
|
* @param toBeParsed String to be converted to a list of strings
|
106 : |
|
|
* @return
|
107 : |
|
|
*/
|
108 : |
|
|
public static Set<String> stringToListWithMarker(String toBeParsed) {
|
109 : |
|
|
Set<String> result = new HashSet<String>();
|
110 : |
|
|
String pattern = MARKER;
|
111 : |
|
|
String[] fields = toBeParsed.split(pattern);
|
112 : |
|
|
|
113 : |
|
|
for(int i = 0; i < fields.length; i++) {
|
114 : |
|
|
if(fields[i] != "") result.add(fields[i]);
|
115 : |
|
|
}
|
116 : |
|
|
|
117 : |
|
|
Set<String> toRemove = new HashSet<String>();
|
118 : |
|
|
for(String text : result) {
|
119 : |
|
|
if(text.equals(""))
|
120 : |
|
|
toRemove.add(text);
|
121 : |
|
|
}
|
122 : |
|
|
result.removeAll(toRemove);
|
123 : |
|
|
|
124 : |
|
|
return result;
|
125 : |
|
|
}
|
126 : |
|
|
|
127 : |
|
|
/**
|
128 : |
|
|
* Parses a Date from the main webpage to a Java.util.Date
|
129 : |
|
|
* @param date String to be parsed
|
130 : |
|
|
* @param add24 adds 24 hours to the date
|
131 : |
|
|
* @return the date object
|
132 : |
|
|
*/
|
133 : |
|
|
public static Date getDate(String date, boolean add24) {
|
134 : |
|
|
|
135 : |
|
|
try { // Parse with a custom format
|
136 : |
|
|
DateFormat formatter = new SimpleDateFormat(ABSQuery.DATE_FORMAT);
|
137 : |
|
|
|
138 : |
|
|
Date newDate = (Date) formatter.parse(date);
|
139 : |
|
|
|
140 : |
|
|
System.out.println("DATE 1 - " + newDate);
|
141 : |
|
|
|
142 : |
|
|
if(add24) {
|
143 : |
|
|
Calendar calendar = new GregorianCalendar();
|
144 : |
|
|
calendar.setTime(newDate);
|
145 : |
|
|
calendar.add(Calendar.HOUR, 23);
|
146 : |
|
|
calendar.add(Calendar.MINUTE, 59);
|
147 : |
|
|
calendar.add(Calendar.SECOND, 59);
|
148 : |
|
|
|
149 : |
|
|
newDate = calendar.getTime();
|
150 : |
|
|
}
|
151 : |
|
|
|
152 : |
|
|
System.out.println("DATE 2 - " + newDate);
|
153 : |
|
|
|
154 : |
|
|
return newDate;
|
155 : |
|
|
} catch (ParseException e) {
|
156 : |
|
|
return new Date();
|
157 : |
|
|
}
|
158 : |
|
|
}
|
159 : |
|
|
|
160 : |
|
|
/**
|
161 : |
|
|
* String list copy
|
162 : |
|
|
* @param originalList Original list to be copied
|
163 : |
|
|
* @return a copied list
|
164 : |
|
|
*/
|
165 : |
|
|
public static Set<String> copyStringList(Set<String> originalList) {
|
166 : |
|
|
Set<String> copyList = new HashSet<String>();
|
167 : |
|
|
|
168 : |
|
|
for(String string : originalList)
|
169 : |
|
|
copyList.add(string);
|
170 : |
|
|
|
171 : |
|
|
return copyList;
|
172 : |
|
|
}
|
173 : |
|
|
|
174 : |
|
|
public static String removeSpaces(String s) {
|
175 : |
|
|
StringTokenizer st = new StringTokenizer(s," ",false);
|
176 : |
|
|
String t="";
|
177 : |
|
|
while (st.hasMoreElements()) t += st.nextElement();
|
178 : |
|
|
return t;
|
179 : |
|
|
}
|
180 : |
|
|
|
181 : |
|
|
public static String sortString(String toSort) {
|
182 : |
|
|
String result = "";
|
183 : |
|
|
|
184 : |
|
|
StringTokenizer st = new StringTokenizer(toSort);
|
185 : |
|
|
while (st.hasMoreTokens()) {
|
186 : |
|
|
String token = st.nextToken();
|
187 : |
|
|
if(token.length() > 20) token = token.substring(0, 12) + "...";
|
188 : |
|
|
result = result + " " + token;
|
189 : |
|
|
}
|
190 : |
|
|
|
191 : |
|
|
return result.substring(1, result.length());
|
192 : |
|
|
}
|
193 : |
|
|
|
194 : |
|
|
public static String dateToString(Date date) {
|
195 : |
|
|
Format formatter = new SimpleDateFormat(ABSQuery.DATE_FORMAT);
|
196 : |
|
|
return formatter.format(date);
|
197 : |
|
|
}
|
198 : |
|
|
}
|