Coverage Report - net.sf.statsvn.util.XMLUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLUtil
75%
33/44
56%
9/16
3.75
 
 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  0
         private XMLUtil() {
 47  0
         }
 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  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  
                         // if datetime has milliseconds, separate them
 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  
          * 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  18120
                 final GregorianCalendar gc = new GregorianCalendar();
 107  
 
 108  
                 // Calculate the total offset from GMT
 109  18120
                 final int totalOffset = gc.get(Calendar.ZONE_OFFSET) + gc.get(Calendar.DST_OFFSET);
 110  
 
 111  
                 // Calculate the time in GMT
 112  18120
                 final long localTime = date.getTime() + totalOffset;
 113  
 
 114  
                 // Create a date using the calculated GMT time
 115  18120
                 final Date localDate = new Date(localTime);
 116  
 
 117  18120
                 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  60
                         final Source source = new DOMSource(doc);
 132  
 
 133  
                         // Prepare the output file
 134  60
                         final File file = new File(filename);
 135  60
                         final Result result = new StreamResult(file);
 136  
 
 137  
                         // Write the DOM document to the file
 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  
 }