1 | |
package net.sf.statsvn.util; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.text.DateFormat; |
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.TimeZone; |
11 | |
|
12 | |
import javax.xml.transform.OutputKeys; |
13 | |
import javax.xml.transform.Result; |
14 | |
import javax.xml.transform.Source; |
15 | |
import javax.xml.transform.Transformer; |
16 | |
import javax.xml.transform.TransformerConfigurationException; |
17 | |
import javax.xml.transform.TransformerException; |
18 | |
import javax.xml.transform.TransformerFactory; |
19 | |
import javax.xml.transform.dom.DOMSource; |
20 | |
import javax.xml.transform.stream.StreamResult; |
21 | |
|
22 | |
import net.sf.statsvn.output.SvnConfigurationOptions; |
23 | |
|
24 | |
import org.w3c.dom.Document; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public final class XMLUtil { |
36 | |
private static final int ONE_SEC_IN_MILLISECS = 1000; |
37 | |
|
38 | |
private static final int IDOT_POSITION_IFNEG = 20; |
39 | |
|
40 | |
private static final int NORMAL_IDOT_POSITION = 19; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | private XMLUtil() { |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public static Date parseXsdDateTime(String sDateTime) throws ParseException { |
58 | 18120 | final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
59 | |
|
60 | 18120 | int iDotPosition = NORMAL_IDOT_POSITION; |
61 | 18120 | if (sDateTime.charAt(0) == '-') { |
62 | 0 | iDotPosition = IDOT_POSITION_IFNEG; |
63 | |
} |
64 | |
Date result; |
65 | 18120 | if (sDateTime.length() <= iDotPosition) { |
66 | 0 | return format.parse(sDateTime + "Z"); |
67 | |
} |
68 | |
|
69 | 18120 | String millis = null; |
70 | 18120 | char c = sDateTime.charAt(iDotPosition); |
71 | 18120 | if (c == '.') { |
72 | |
|
73 | 18120 | int eoms = iDotPosition + 1; |
74 | 126840 | while (Character.isDigit(sDateTime.charAt(eoms))) { |
75 | 108720 | eoms += 1; |
76 | |
} |
77 | 18120 | millis = sDateTime.substring(iDotPosition, eoms); |
78 | 18120 | sDateTime = sDateTime.substring(0, iDotPosition) + sDateTime.substring(eoms); |
79 | 18120 | c = sDateTime.charAt(iDotPosition); |
80 | |
} |
81 | 18120 | if (c == '+' || c == '-') { |
82 | 0 | format.setTimeZone(TimeZone.getTimeZone("GMT" + sDateTime.substring(iDotPosition))); |
83 | 0 | sDateTime = sDateTime.substring(0, iDotPosition) + "Z"; |
84 | 18120 | } else if (c != 'Z') { |
85 | 0 | throw new ParseException("Illegal timezone specification.", iDotPosition); |
86 | |
} |
87 | |
|
88 | 18120 | result = format.parse(sDateTime); |
89 | 18120 | if (millis != null) { |
90 | 18120 | result.setTime(result.getTime() + Math.round(Float.parseFloat(millis) * ONE_SEC_IN_MILLISECS)); |
91 | |
} |
92 | |
|
93 | 18120 | result = offsetDateFromGMT(result); |
94 | 18120 | return result; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public static Date offsetDateFromGMT(final Date date) { |
105 | |
|
106 | 18120 | final GregorianCalendar gc = new GregorianCalendar(); |
107 | |
|
108 | |
|
109 | 18120 | final int totalOffset = gc.get(Calendar.ZONE_OFFSET) + gc.get(Calendar.DST_OFFSET); |
110 | |
|
111 | |
|
112 | 18120 | final long localTime = date.getTime() + totalOffset; |
113 | |
|
114 | |
|
115 | 18120 | final Date localDate = new Date(localTime); |
116 | |
|
117 | 18120 | return localDate; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public static void writeXmlFile(final Document doc, final String filename) { |
129 | |
try { |
130 | |
|
131 | 60 | final Source source = new DOMSource(doc); |
132 | |
|
133 | |
|
134 | 60 | final File file = new File(filename); |
135 | 60 | final Result result = new StreamResult(file); |
136 | |
|
137 | |
|
138 | 60 | final Transformer xformer = TransformerFactory.newInstance().newTransformer(); |
139 | 60 | xformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
140 | 60 | xformer.transform(source, result); |
141 | 0 | } catch (final TransformerConfigurationException e) { |
142 | 0 | SvnConfigurationOptions.getTaskLogger().error(e.toString()); |
143 | 0 | } catch (final TransformerException e) { |
144 | 0 | SvnConfigurationOptions.getTaskLogger().error(e.toString()); |
145 | 60 | } |
146 | 60 | } |
147 | |
} |