1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.common;
|
12
|
|
13
|
import java.io.BufferedReader;
|
14
|
import java.io.File;
|
15
|
import java.io.IOException;
|
16
|
import java.io.InputStream;
|
17
|
import java.io.InputStreamReader;
|
18
|
import java.lang.annotation.Annotation;
|
19
|
import java.lang.reflect.Field;
|
20
|
import java.lang.reflect.Modifier;
|
21
|
import java.net.HttpURLConnection;
|
22
|
import java.net.MalformedURLException;
|
23
|
import java.net.URI;
|
24
|
import java.net.URISyntaxException;
|
25
|
import java.net.URL;
|
26
|
import java.util.ArrayList;
|
27
|
import java.util.HashMap;
|
28
|
import java.util.Map;
|
29
|
import java.util.regex.Matcher;
|
30
|
import java.util.regex.Pattern;
|
31
|
|
32
|
import org.apache.log4j.Logger;
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
*
|
37
|
*/
|
38
|
public class CdmUtils {
|
39
|
private static final Logger logger = Logger.getLogger(CdmUtils.class);
|
40
|
|
41
|
|
42
|
static final String MUST_EXIST_FILE = "MUST-EXIST.txt";
|
43
|
|
44
|
//folder seperator
|
45
|
static String folderSeperator;
|
46
|
|
47
|
|
48
|
public static String getHomeDir() throws IOException{
|
49
|
String homeDirString = System.getenv("USERPROFILE") != null ? System.getenv("USERPROFILE") : System.getProperty("user.home");
|
50
|
|
51
|
if( ! new File(homeDirString).canWrite()){
|
52
|
throw new IOException("Can not write to home directory. Assumed path is: " + homeDirString);
|
53
|
}
|
54
|
|
55
|
return homeDirString;
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Returns the an InputStream for a read-only source
|
60
|
* @param resourceFileName the resources path within the classpath(!)
|
61
|
* @return
|
62
|
* @throws IOException
|
63
|
*/
|
64
|
public static InputStream getReadableResourceStream(String resourceFileName)
|
65
|
throws IOException{
|
66
|
InputStream urlStream = CdmUtils.class.getResourceAsStream("/"+ resourceFileName);
|
67
|
return urlStream;
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Returns the an InputStream for a read-only source
|
72
|
* @param resourceFileName the resources path within the classpath(!)
|
73
|
* @return
|
74
|
* @throws IOException
|
75
|
*/
|
76
|
public static InputStreamReader getUtf8ResourceReader(String resourceFileName)
|
77
|
throws IOException{
|
78
|
InputStream urlStream = CdmUtils.class.getResourceAsStream("/"+ resourceFileName);
|
79
|
InputStreamReader inputStreamReader = new InputStreamReader(urlStream, "UTF8");
|
80
|
return inputStreamReader;
|
81
|
}
|
82
|
|
83
|
|
84
|
/**
|
85
|
* @return
|
86
|
*/
|
87
|
static public String getFolderSeperator(){
|
88
|
if (folderSeperator == null){
|
89
|
URL url = CdmUtils.class.getResource("/"+ MUST_EXIST_FILE);
|
90
|
if ( url != null && ! urlIsJarOrBundle(url) ){
|
91
|
folderSeperator = File.separator;
|
92
|
}else{
|
93
|
folderSeperator = "/";
|
94
|
}
|
95
|
}
|
96
|
return folderSeperator;
|
97
|
}
|
98
|
|
99
|
|
100
|
/**
|
101
|
* @param url
|
102
|
* @return
|
103
|
*/
|
104
|
static private boolean urlIsJarOrBundle(URL url){
|
105
|
return url.getProtocol().startsWith("jar") || url.getProtocol().startsWith("bundleresource");
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Returns the file name for the file in which 'clazz' is to be found (helps finding according libraries)
|
110
|
* @param clazz
|
111
|
* @return
|
112
|
*/
|
113
|
static public String findLibrary(Class<?> clazz){
|
114
|
String result = null;
|
115
|
if (clazz != null){
|
116
|
String fullPackageName = clazz.getCanonicalName();
|
117
|
fullPackageName = fullPackageName.replace(".", "/");
|
118
|
URL url = CdmUtils.class.getResource("/" + fullPackageName + ".class" );
|
119
|
if (url != null){
|
120
|
result = url.getFile();
|
121
|
}else{
|
122
|
result = "";
|
123
|
}
|
124
|
logger.debug("LibraryURL for " + clazz.getCanonicalName() + " : " + result);
|
125
|
}
|
126
|
return result;
|
127
|
}
|
128
|
|
129
|
static public String testMe(){
|
130
|
String message = "This is a test";
|
131
|
System.out.println(message);
|
132
|
return message;
|
133
|
}
|
134
|
|
135
|
static public String readInputLine(String inputQuestion){
|
136
|
try {
|
137
|
|
138
|
System.out.print(inputQuestion);
|
139
|
BufferedReader in = new BufferedReader( new java.io.InputStreamReader( System.in ));
|
140
|
String input;
|
141
|
input = in.readLine();
|
142
|
return input;
|
143
|
} catch (IOException e) {
|
144
|
logger.warn("IOExeption");
|
145
|
return null;
|
146
|
}
|
147
|
}
|
148
|
|
149
|
|
150
|
/**
|
151
|
* Returns the trimmed value string if value is not <code>null</code>.
|
152
|
* Returns the empty string if value is <code>null</code>.
|
153
|
* @param value
|
154
|
* @return
|
155
|
*/
|
156
|
static public String NzTrim(String value){
|
157
|
return (value == null ? "" : value);
|
158
|
}
|
159
|
|
160
|
|
161
|
/**
|
162
|
* Returns value if value is not <code>null</code>. Returns empty string if value is <code>null</code>.
|
163
|
* @param value
|
164
|
* @return
|
165
|
*/
|
166
|
static public String Nz(String value){
|
167
|
return (value == null ? "" : value);
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Returns value if value is not <code>null</code>. Returns defaultValue if value is <code>null</code>.
|
172
|
* @param value
|
173
|
* @return
|
174
|
*/
|
175
|
static public String Nz(String value, String defaultValue){
|
176
|
return (value == null ? defaultValue : value);
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* Returns value if value is not <code>null</code>. Returns 0 if value is <code>null</code>.
|
181
|
* @param value
|
182
|
* @return
|
183
|
*/
|
184
|
static public Integer Nz(Integer value){
|
185
|
return (value == null ? 0 : value);
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Returns value if value is not <code>null</code>. Returns 0 if value is <code>null</code>.
|
190
|
* @param value
|
191
|
* @return
|
192
|
*/
|
193
|
static public Long Nz(Long value){
|
194
|
return (value == null ? 0 : value);
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Concatenates an array of strings using the defined seperator.<BR>
|
199
|
* <code>Null</code> values are interpreted as empty strings.<BR>
|
200
|
* If all strings are <code>null</code> then <code>null</code> is returned.
|
201
|
* @param strings
|
202
|
* @param seperator
|
203
|
* @return String
|
204
|
*/
|
205
|
static public String concat(CharSequence separator, String[] strings){
|
206
|
String result = "";
|
207
|
boolean allNull = true;
|
208
|
for (String string : strings){
|
209
|
if (string != null){
|
210
|
if (result.length() > 0 && string.length() > 0){
|
211
|
result += separator;
|
212
|
}
|
213
|
result += string;
|
214
|
allNull = false;
|
215
|
}
|
216
|
}
|
217
|
//if all strings are null result should be null, not ""
|
218
|
if (allNull){
|
219
|
return null;
|
220
|
}else {
|
221
|
return result;
|
222
|
}
|
223
|
}
|
224
|
|
225
|
/**
|
226
|
* Concatenates two strings, using the defined seperator.<BR>
|
227
|
* <code>Null</code> values are interpreted as empty Strings.<BR>
|
228
|
* If both strings are <code>null</code> then <code>null</code> is returned.
|
229
|
* @see #concat(CharSequence, String[])
|
230
|
* @param seperator
|
231
|
* @param string1
|
232
|
* @param string2
|
233
|
* @return String
|
234
|
*/
|
235
|
static public String concat(CharSequence separator, String string1, String string2){
|
236
|
String[] strings = {string1, string2};
|
237
|
return concat(separator, strings);
|
238
|
}
|
239
|
|
240
|
|
241
|
/** Returns a version of the input where all contiguous
|
242
|
* whitespace characters are replaced with a single
|
243
|
* space. Line terminators are treated like whitespace.
|
244
|
*
|
245
|
* @param inputStr
|
246
|
* @return
|
247
|
*/
|
248
|
public static CharSequence removeDuplicateWhitespace(CharSequence inputStr) {
|
249
|
|
250
|
String patternStr = "\\s+";
|
251
|
String replaceStr = " ";
|
252
|
Pattern pattern = Pattern.compile(patternStr);
|
253
|
Matcher matcher = pattern.matcher(inputStr);
|
254
|
return matcher.replaceAll(replaceStr);
|
255
|
}
|
256
|
|
257
|
|
258
|
/** Builds a list of strings by splitting an input string
|
259
|
* with delimiters whitespace, comma, or semicolon
|
260
|
* @param value
|
261
|
* @return
|
262
|
*/
|
263
|
public static ArrayList<String> buildList(String value) {
|
264
|
|
265
|
ArrayList<String> resultList = new ArrayList<String>();
|
266
|
for (String tag : value.split("[\\s,;]+")) {
|
267
|
resultList.add(tag);
|
268
|
}
|
269
|
return resultList;
|
270
|
}
|
271
|
|
272
|
|
273
|
static public boolean urlExists(String strUrl, boolean withWarning){
|
274
|
try {
|
275
|
HttpURLConnection.setFollowRedirects(false);
|
276
|
// note : you may also need
|
277
|
// HttpURLConnection.setInstanceFollowRedirects(false)
|
278
|
HttpURLConnection con =
|
279
|
(HttpURLConnection) new URL(strUrl).openConnection();
|
280
|
con.setRequestMethod("HEAD");
|
281
|
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
|
282
|
} catch (MalformedURLException e) {
|
283
|
if (withWarning) {
|
284
|
logger.warn(e);
|
285
|
}
|
286
|
} catch (IOException e) {
|
287
|
//
|
288
|
};
|
289
|
return false;
|
290
|
}
|
291
|
|
292
|
static public URI string2Uri(String string) {
|
293
|
URI uri = null;
|
294
|
try {
|
295
|
uri = new URI(string);
|
296
|
logger.debug("uri: " + uri.toString());
|
297
|
} catch (URISyntaxException ex) {
|
298
|
logger.error("Problem converting string " + string + " to URI " + uri);
|
299
|
return null;
|
300
|
}
|
301
|
return uri;
|
302
|
}
|
303
|
|
304
|
static public boolean isNumeric(String string){
|
305
|
if (string == null){
|
306
|
return false;
|
307
|
}
|
308
|
try {
|
309
|
Double.valueOf(string);
|
310
|
return true;
|
311
|
} catch (NumberFormatException e) {
|
312
|
return false;
|
313
|
}
|
314
|
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* Returns true if the passed string starts with an upper case letter.
|
319
|
* @param string
|
320
|
* @return
|
321
|
*/
|
322
|
static public boolean isCapital(String string){
|
323
|
if (isEmpty(string)){
|
324
|
return false;
|
325
|
}else{
|
326
|
Character firstChar = string.charAt(0);
|
327
|
if (firstChar.equals(Character.toUpperCase(firstChar))){
|
328
|
return true;
|
329
|
}else{
|
330
|
return false;
|
331
|
}
|
332
|
}
|
333
|
|
334
|
}
|
335
|
|
336
|
/**
|
337
|
* Returns true if string is null, "" or string.trim() is ""
|
338
|
* @see isNotEmpty(String string)
|
339
|
* @param string
|
340
|
* @return
|
341
|
*/
|
342
|
static public boolean isEmpty(String string){
|
343
|
if (string == null){
|
344
|
return true;
|
345
|
}
|
346
|
if ("".equals(string.trim())){
|
347
|
return true;
|
348
|
}
|
349
|
return false;
|
350
|
}
|
351
|
|
352
|
/**
|
353
|
* Tests if two objects are equal or both null. Otherwise returns false
|
354
|
* @param obj1
|
355
|
* @param obj2
|
356
|
* @return
|
357
|
*/
|
358
|
public static boolean nullSafeEqual(Object obj1, Object obj2) {
|
359
|
if (obj1 == null && obj2 == null){
|
360
|
return true;
|
361
|
}
|
362
|
if (obj1 == null && obj2 != null){
|
363
|
return false;
|
364
|
}
|
365
|
return (obj1.equals(obj2));
|
366
|
}
|
367
|
|
368
|
/**
|
369
|
* Returns false if string is null, "" or string.trim() is ""
|
370
|
* Else true.
|
371
|
* @see isEmpty(String string)
|
372
|
* @param string
|
373
|
* @return
|
374
|
*/
|
375
|
static public boolean isNotEmpty(String string){
|
376
|
return !isEmpty(string);
|
377
|
}
|
378
|
|
379
|
|
380
|
/**
|
381
|
* Computes all fields recursively
|
382
|
* @param clazz
|
383
|
* @return
|
384
|
*/
|
385
|
public static Map<String, Field> getAllFields(Class clazz, Class highestClass, boolean includeStatic, boolean includeTransient, boolean makeAccessible, boolean includeHighestClass) {
|
386
|
Map<String, Field> result = new HashMap<String, Field>();
|
387
|
if ( highestClass.isAssignableFrom(clazz) && (clazz != highestClass || includeHighestClass)){
|
388
|
//exclude static
|
389
|
for (Field field: clazz.getDeclaredFields()){
|
390
|
if (includeStatic || ! Modifier.isStatic(field.getModifiers())){
|
391
|
if (includeTransient || ! isTransient(field)){
|
392
|
field.setAccessible(makeAccessible);
|
393
|
result.put(field.getName(), field);
|
394
|
}
|
395
|
}
|
396
|
}
|
397
|
|
398
|
//include superclass fields
|
399
|
Class superclass = clazz.getSuperclass();
|
400
|
if (superclass != null){
|
401
|
result.putAll(getAllFields(superclass, highestClass, includeStatic, includeTransient, makeAccessible, includeHighestClass));
|
402
|
}
|
403
|
}
|
404
|
return result;
|
405
|
}
|
406
|
|
407
|
|
408
|
/**
|
409
|
* Returns true, if field has an annotation of type javax.persistence.Annotation
|
410
|
* @param field
|
411
|
* @return
|
412
|
*/
|
413
|
protected static boolean isTransient(Field field) {
|
414
|
for (Annotation annotation : field.getAnnotations()){
|
415
|
//if (Transient.class.isAssignableFrom(annotation.annotationType())){
|
416
|
if (annotation.annotationType().getSimpleName().equals("Transient")){
|
417
|
return true;
|
418
|
}
|
419
|
}
|
420
|
return false;
|
421
|
}
|
422
|
|
423
|
/**
|
424
|
* Trims the string and if the string ends with a dot removes it.
|
425
|
* @param string
|
426
|
* @return
|
427
|
*/
|
428
|
public static String removeTrailingDot(String string){
|
429
|
if (string == null){
|
430
|
return null;
|
431
|
}
|
432
|
if (string.trim().endsWith(".")){
|
433
|
return string.substring(0, string.length() -1);
|
434
|
}
|
435
|
return string;
|
436
|
}
|
437
|
}
|