1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.common;
|
11
|
|
12
|
import java.io.BufferedReader;
|
13
|
import java.io.File;
|
14
|
import java.io.IOException;
|
15
|
import java.io.InputStream;
|
16
|
import java.net.HttpURLConnection;
|
17
|
import java.net.MalformedURLException;
|
18
|
import java.net.URL;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
|
22
|
/**
|
23
|
* @author a.mueller
|
24
|
*
|
25
|
*/
|
26
|
public class CdmUtils {
|
27
|
private static final Logger logger = Logger.getLogger(CdmUtils.class);
|
28
|
|
29
|
|
30
|
static final String MUST_EXIST_FILE = "MUST-EXIST.txt";
|
31
|
|
32
|
//folder seperator
|
33
|
static String folderSeperator;
|
34
|
|
35
|
|
36
|
/**
|
37
|
* Returns the an InputStream for a read-only source
|
38
|
* @param resourceFileName the resources path within the classpath(!)
|
39
|
* @return
|
40
|
* @throws IOException
|
41
|
*/
|
42
|
public static InputStream getReadableResourceStream(String resourceFileName)
|
43
|
throws IOException{
|
44
|
InputStream urlStream = CdmUtils.class.getResourceAsStream("/"+ resourceFileName);
|
45
|
return urlStream;
|
46
|
}
|
47
|
|
48
|
|
49
|
/**
|
50
|
* @return
|
51
|
*/
|
52
|
static public String getFolderSeperator(){
|
53
|
if (folderSeperator == null){
|
54
|
URL url = CdmUtils.class.getResource("/"+ MUST_EXIST_FILE);
|
55
|
if ( url != null && ! urlIsJarOrBundle(url) ){
|
56
|
folderSeperator = File.separator;
|
57
|
}else{
|
58
|
folderSeperator = "/";
|
59
|
}
|
60
|
}
|
61
|
return folderSeperator;
|
62
|
}
|
63
|
|
64
|
|
65
|
/**
|
66
|
* @param url
|
67
|
* @return
|
68
|
*/
|
69
|
static private boolean urlIsJarOrBundle(URL url){
|
70
|
return url.getProtocol().startsWith("jar") || url.getProtocol().startsWith("bundleresource");
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Returns the file name for the file in which 'clazz' is to be found (helps finding according libraries)
|
75
|
* @param clazz
|
76
|
* @return
|
77
|
*/
|
78
|
static public String findLibrary(Class clazz){
|
79
|
String result = null;
|
80
|
if (clazz != null){
|
81
|
String fullPackageName = clazz.getCanonicalName();
|
82
|
fullPackageName = fullPackageName.replace(".", "/");
|
83
|
URL url = CdmUtils.class.getResource("/" + fullPackageName + ".class" );
|
84
|
if (url != null){
|
85
|
result = url.getFile();
|
86
|
}else{
|
87
|
result = "";
|
88
|
}
|
89
|
logger.debug("LibraryURL for " + clazz.getCanonicalName() + " : " + result);
|
90
|
}
|
91
|
return result;
|
92
|
}
|
93
|
|
94
|
static public String testMe(){
|
95
|
System.out.println("Shit");
|
96
|
return "Oje";
|
97
|
}
|
98
|
|
99
|
static public String readInputLine(String inputQuestion){
|
100
|
try {
|
101
|
|
102
|
System.out.print(inputQuestion);
|
103
|
BufferedReader in = new BufferedReader( new java.io.InputStreamReader( System.in ));
|
104
|
String input;
|
105
|
input = in.readLine();
|
106
|
return input;
|
107
|
} catch (IOException e) {
|
108
|
logger.warn("IOExeption");
|
109
|
return null;
|
110
|
}
|
111
|
}
|
112
|
|
113
|
static public String Nz(String value){
|
114
|
return (value == null ? "" : value);
|
115
|
}
|
116
|
|
117
|
|
118
|
static public Integer Nz(Integer value){
|
119
|
return (value == null ? 0 : value);
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Concatenates the an Array of Strings, using the defined seperator.
|
124
|
* Null values are interpreted as empty Strings
|
125
|
* If all Strings are null a null is returned
|
126
|
* @param strings
|
127
|
* @param seperator
|
128
|
* @return String
|
129
|
*/
|
130
|
static public String concat(CharSequence seperator, String[] strings){
|
131
|
String result = "";
|
132
|
boolean allNull = true;
|
133
|
for (String string : strings){
|
134
|
if (string != null){
|
135
|
if (result.length() > 0 && string.length() > 0){
|
136
|
result += seperator;
|
137
|
}
|
138
|
result += string;
|
139
|
allNull = false;
|
140
|
}
|
141
|
}
|
142
|
//if all strings are null result should be null, not ""
|
143
|
if (allNull){
|
144
|
return null;
|
145
|
}else {
|
146
|
return result;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
static public String concat(CharSequence seperator, String string1, String string2){
|
151
|
String[] strings = {string1, string2};
|
152
|
return concat(seperator, strings);
|
153
|
}
|
154
|
|
155
|
static public boolean urlExists(String strUrl, boolean withWarning){
|
156
|
try {
|
157
|
HttpURLConnection.setFollowRedirects(false);
|
158
|
// note : you may also need
|
159
|
// HttpURLConnection.setInstanceFollowRedirects(false)
|
160
|
HttpURLConnection con =
|
161
|
(HttpURLConnection) new URL(strUrl).openConnection();
|
162
|
con.setRequestMethod("HEAD");
|
163
|
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
|
164
|
} catch (MalformedURLException e) {
|
165
|
if (withWarning) {
|
166
|
logger.warn(e);
|
167
|
}
|
168
|
} catch (IOException e) {
|
169
|
//
|
170
|
};
|
171
|
return false;
|
172
|
}
|
173
|
|
174
|
static public boolean isNumeric(String string){
|
175
|
if (string == null){
|
176
|
return false;
|
177
|
}
|
178
|
try {
|
179
|
Double.valueOf(string);
|
180
|
return true;
|
181
|
} catch (NumberFormatException e) {
|
182
|
return false;
|
183
|
}
|
184
|
|
185
|
}
|
186
|
|
187
|
}
|