1
|
|
package net.sf.flock.parser;
|
2
|
|
|
3
|
|
import java.net.URL;
|
4
|
|
import java.text.DateFormat;
|
5
|
|
import java.text.SimpleDateFormat;
|
6
|
|
import java.util.Date;
|
7
|
|
import java.util.Iterator;
|
8
|
|
import java.util.Locale;
|
9
|
|
|
10
|
|
import org.apache.log4j.LogManager;
|
11
|
|
import org.apache.log4j.Logger;
|
12
|
|
import org.jdom.Element;
|
13
|
|
import org.jdom.Namespace;
|
14
|
|
|
15
|
|
class ParserUtil {
|
16
|
|
|
17
|
|
private final static Logger LOGGER = LogManager.getLogger(ParserUtil.class);
|
18
|
|
|
19
|
0
|
private ParserUtil() {
|
20
|
|
}
|
21
|
|
|
22
|
0
|
public static URL getURL(String toURL) {
|
23
|
0
|
try {
|
24
|
0
|
return new URL(toURL);
|
25
|
|
} catch (java.net.MalformedURLException e) {
|
26
|
0
|
LOGGER.warn("Invalid URL '" + toURL + "' given.");
|
27
|
0
|
return null;
|
28
|
|
}
|
29
|
|
}
|
30
|
|
|
31
|
0
|
public static Namespace getDefaultNS(Element element) {
|
32
|
0
|
return getNamespace(element, "");
|
33
|
|
}
|
34
|
|
|
35
|
0
|
public static Namespace getNamespace(Element element, String prefix) {
|
36
|
0
|
Namespace ns = null;
|
37
|
0
|
Iterator it = element.getAdditionalNamespaces().iterator();
|
38
|
0
|
while (it.hasNext()) {
|
39
|
0
|
Namespace curNS = (Namespace) it.next();
|
40
|
0
|
if (curNS.getPrefix().equals(prefix)) {
|
41
|
0
|
ns = curNS;
|
42
|
0
|
break;
|
43
|
|
}
|
44
|
|
}
|
45
|
0
|
return ns;
|
46
|
|
}
|
47
|
|
|
48
|
0
|
public static Namespace getNamespaceFromURI(Element element, String uri) {
|
49
|
0
|
Iterator it = element.getAdditionalNamespaces().iterator();
|
50
|
0
|
while (it.hasNext()) {
|
51
|
0
|
Namespace curNS = (Namespace) it.next();
|
52
|
0
|
if (curNS.getURI().equals(uri)) {
|
53
|
0
|
return curNS;
|
54
|
|
}
|
55
|
|
}
|
56
|
0
|
return null;
|
57
|
|
}
|
58
|
|
|
59
|
|
/** @consider making dateformatters non-static, for thread-safety */
|
60
|
|
private final static DateFormat[] DATE_FORMATTERS = {
|
61
|
|
// "2002-09-19T02:51:16-05:00"
|
62
|
|
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sszzzzz", Locale.US),
|
63
|
|
|
64
|
|
// "2002-12-10T15:46-06:00"
|
65
|
|
new SimpleDateFormat("yyyy-MM-dd'T'HH:mmzzzzz", Locale.US),
|
66
|
|
|
67
|
|
// "Tue, 29 Oct 2002 21:35:25 GMT"
|
68
|
|
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzzzz", Locale.US),
|
69
|
|
|
70
|
|
// "Tue 29 Oct 2002 21:35:25 GMT"
|
71
|
|
new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss zzzzz", Locale.US),
|
72
|
|
|
73
|
|
// 1/7/2003
|
74
|
|
new SimpleDateFormat("MM/dd/yyyy", Locale.US),
|
75
|
|
// "2002-12-07"
|
76
|
|
new SimpleDateFormat("yyyy-MM-dd", Locale.US)
|
77
|
|
};
|
78
|
|
|
79
|
0
|
public static Date parseDate(Element dateElement) {
|
80
|
0
|
if (dateElement == null) {
|
81
|
0
|
return null;
|
82
|
|
}
|
83
|
|
|
84
|
0
|
String date = dateElement.getTextTrim();
|
85
|
0
|
for (int i = 0; i < DATE_FORMATTERS.length; i++) {
|
86
|
0
|
try {
|
87
|
0
|
return DATE_FORMATTERS[i].parse(date);
|
88
|
|
} catch (java.text.ParseException e) {
|
89
|
|
}
|
90
|
|
}
|
91
|
|
|
92
|
0
|
LOGGER.warn("Unable to parse date: '" + date + "'");
|
93
|
0
|
return null;
|
94
|
|
}
|
95
|
|
|
96
|
|
|
97
|
0
|
public static String elementValue(Element element, int maxlen) {
|
98
|
0
|
if (element == null)
|
99
|
0
|
return "null";
|
100
|
|
|
101
|
0
|
String value = element.getTextTrim();
|
102
|
0
|
if (value.length()>maxlen) {
|
103
|
0
|
return value.substring(0,maxlen)+"...";
|
104
|
|
} else {
|
105
|
0
|
return value;
|
106
|
|
}
|
107
|
|
}
|
108
|
|
|
109
|
|
}
|
110
|
|
|