View Javadoc

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   * Utilities class to faciliate XML management.
29   * 
30   * @author Jason Kealey <jkealey@shade.ca>
31   * @author Gunter Mussbacher <gunterm@site.uottawa.ca>
32   * 
33   * @version $Id: XMLUtil.java 351 2008-03-28 18:46:26Z benoitx $
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  	 * A utility class (only static methods) should be final and have
44  	 * a private constructor.
45  	 */
46  	private XMLUtil() {
47  	}
48  
49  	/**
50  	 * For some reason, can't find this utility method in the java framework.
51  	 * 
52  	 * @param sDateTime
53  	 *            an xsd:dateTime string
54  	 * @return an equivalent java.util.Date
55  	 * @throws ParseException
56  	 */
57  	public static Date parseXsdDateTime(String sDateTime) throws ParseException {
58  		final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
59  
60  		int iDotPosition = NORMAL_IDOT_POSITION;
61  		if (sDateTime.charAt(0) == '-') {
62  			iDotPosition = IDOT_POSITION_IFNEG;
63  		}
64  		Date result;
65  		if (sDateTime.length() <= iDotPosition) {
66  			return format.parse(sDateTime + "Z");
67  		}
68  
69  		String millis = null;
70  		char c = sDateTime.charAt(iDotPosition);
71  		if (c == '.') {
72  			// if datetime has milliseconds, separate them
73  			int eoms = iDotPosition + 1;
74  			while (Character.isDigit(sDateTime.charAt(eoms))) {
75  				eoms += 1;
76  			}
77  			millis = sDateTime.substring(iDotPosition, eoms);
78  			sDateTime = sDateTime.substring(0, iDotPosition) + sDateTime.substring(eoms);
79  			c = sDateTime.charAt(iDotPosition);
80  		}
81  		if (c == '+' || c == '-') {
82  			format.setTimeZone(TimeZone.getTimeZone("GMT" + sDateTime.substring(iDotPosition)));
83  			sDateTime = sDateTime.substring(0, iDotPosition) + "Z";
84  		} else if (c != 'Z') {
85  			throw new ParseException("Illegal timezone specification.", iDotPosition);
86  		}
87  
88  		result = format.parse(sDateTime);
89  		if (millis != null) {
90  			result.setTime(result.getTime() + Math.round(Float.parseFloat(millis) * ONE_SEC_IN_MILLISECS));
91  		}
92  
93  		result = offsetDateFromGMT(result);
94  		return result;
95  	}
96  
97  	/**
98  	 * This method converts from GMT to local timezone
99  	 * 
100 	 * @param date
101 	 * 		date in GMT timezone
102 	 * @return the date in local timezone
103 	 */
104 	public static Date offsetDateFromGMT(final Date date) {
105 		// Create a calendar - it will default to the current OS timezone.
106 		final GregorianCalendar gc = new GregorianCalendar();
107 
108 		// Calculate the total offset from GMT
109 		final int totalOffset = gc.get(Calendar.ZONE_OFFSET) + gc.get(Calendar.DST_OFFSET);
110 
111 		// Calculate the time in GMT
112 		final long localTime = date.getTime() + totalOffset;
113 
114 		// Create a date using the calculated GMT time
115 		final Date localDate = new Date(localTime);
116 
117 		return localDate;
118 	}
119 
120 	/**
121 	 * This method writes a DOM document to a file
122 	 * 
123 	 * @param doc
124 	 *            DOM document.
125 	 * @param filename
126 	 *            the target file.
127 	 */
128 	public static void writeXmlFile(final Document doc, final String filename) {
129 		try {
130 			// Prepare the DOM document for writing
131 			final Source source = new DOMSource(doc);
132 
133 			// Prepare the output file
134 			final File file = new File(filename);
135 			final Result result = new StreamResult(file);
136 
137 			// Write the DOM document to the file
138 			final Transformer xformer = TransformerFactory.newInstance().newTransformer();
139 			xformer.setOutputProperty(OutputKeys.INDENT, "yes");
140 			xformer.transform(source, result);
141 		} catch (final TransformerConfigurationException e) {
142 			SvnConfigurationOptions.getTaskLogger().error(e.toString());
143 		} catch (final TransformerException e) {
144 			SvnConfigurationOptions.getTaskLogger().error(e.toString());
145 		}
146 	}
147 }