| 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 | 1812 | final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
| 59 | |
|
| 60 | 1812 | int iDotPosition = NORMAL_IDOT_POSITION; |
| 61 | 1812 | if (sDateTime.charAt(0) == '-') { |
| 62 | 0 | iDotPosition = IDOT_POSITION_IFNEG; |
| 63 | |
} |
| 64 | |
Date result; |
| 65 | 1812 | if (sDateTime.length() <= iDotPosition) { |
| 66 | 0 | return format.parse(sDateTime + "Z"); |
| 67 | |
} |
| 68 | |
|
| 69 | 1812 | String millis = null; |
| 70 | 1812 | char c = sDateTime.charAt(iDotPosition); |
| 71 | 1812 | if (c == '.') { |
| 72 | |
|
| 73 | 1812 | int eoms = iDotPosition + 1; |
| 74 | 12684 | while (Character.isDigit(sDateTime.charAt(eoms))) { |
| 75 | 10872 | eoms += 1; |
| 76 | |
} |
| 77 | 1812 | millis = sDateTime.substring(iDotPosition, eoms); |
| 78 | 1812 | sDateTime = sDateTime.substring(0, iDotPosition) + sDateTime.substring(eoms); |
| 79 | 1812 | c = sDateTime.charAt(iDotPosition); |
| 80 | |
} |
| 81 | 1812 | if (c == '+' || c == '-') { |
| 82 | 0 | format.setTimeZone(TimeZone.getTimeZone("GMT" + sDateTime.substring(iDotPosition))); |
| 83 | 0 | sDateTime = sDateTime.substring(0, iDotPosition) + "Z"; |
| 84 | 1812 | } else if (c != 'Z') { |
| 85 | 0 | throw new ParseException("Illegal timezone specification.", iDotPosition); |
| 86 | |
} |
| 87 | |
|
| 88 | 1812 | result = format.parse(sDateTime); |
| 89 | 1812 | if (millis != null) { |
| 90 | 1812 | result.setTime(result.getTime() + Math.round(Float.parseFloat(millis) * ONE_SEC_IN_MILLISECS)); |
| 91 | |
} |
| 92 | |
|
| 93 | 1812 | result = offsetDateFromGMT(result); |
| 94 | 1812 | return result; |
| 95 | |
} |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public static Date offsetDateFromGMT(final Date date) { |
| 105 | |
|
| 106 | 1812 | final GregorianCalendar gc = new GregorianCalendar(); |
| 107 | |
|
| 108 | |
|
| 109 | 1812 | final int totalOffset = gc.get(Calendar.ZONE_OFFSET) + gc.get(Calendar.DST_OFFSET); |
| 110 | |
|
| 111 | |
|
| 112 | 1812 | final long localTime = date.getTime() + totalOffset; |
| 113 | |
|
| 114 | |
|
| 115 | 1812 | final Date localDate = new Date(localTime); |
| 116 | |
|
| 117 | 1812 | 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 | 6 | final Source source = new DOMSource(doc); |
| 132 | |
|
| 133 | |
|
| 134 | 6 | final File file = new File(filename); |
| 135 | 6 | final Result result = new StreamResult(file); |
| 136 | |
|
| 137 | |
|
| 138 | 6 | final Transformer xformer = TransformerFactory.newInstance().newTransformer(); |
| 139 | 6 | xformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 140 | 6 | 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 | 6 | } |
| 146 | 6 | } |
| 147 | |
} |