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 private ParserUtil() {
20 }
21
22 public static URL getURL(String toURL) {
23 try {
24 return new URL(toURL);
25 } catch (java.net.MalformedURLException e) {
26 LOGGER.warn("Invalid URL '" + toURL + "' given.");
27 return null;
28 }
29 }
30
31 public static Namespace getDefaultNS(Element element) {
32 return getNamespace(element, "");
33 }
34
35 public static Namespace getNamespace(Element element, String prefix) {
36 Namespace ns = null;
37 Iterator it = element.getAdditionalNamespaces().iterator();
38 while (it.hasNext()) {
39 Namespace curNS = (Namespace) it.next();
40 if (curNS.getPrefix().equals(prefix)) {
41 ns = curNS;
42 break;
43 }
44 }
45 return ns;
46 }
47
48 public static Namespace getNamespaceFromURI(Element element, String uri) {
49 Iterator it = element.getAdditionalNamespaces().iterator();
50 while (it.hasNext()) {
51 Namespace curNS = (Namespace) it.next();
52 if (curNS.getURI().equals(uri)) {
53 return curNS;
54 }
55 }
56 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 public static Date parseDate(Element dateElement) {
80 if (dateElement == null) {
81 return null;
82 }
83
84 String date = dateElement.getTextTrim();
85 for (int i = 0; i < DATE_FORMATTERS.length; i++) {
86 try {
87 return DATE_FORMATTERS[i].parse(date);
88 } catch (java.text.ParseException e) {
89 }
90 }
91
92 LOGGER.warn("Unable to parse date: '" + date + "'");
93 return null;
94 }
95
96
97 public static String elementValue(Element element, int maxlen) {
98 if (element == null)
99 return "null";
100
101 String value = element.getTextTrim();
102 if (value.length()>maxlen) {
103 return value.substring(0,maxlen)+"...";
104 } else {
105 return value;
106 }
107 }
108
109 }
This page was automatically generated by Maven