Coverage Report - de.jollyday.util.XMLUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLUtil
92%
37/40
94%
17/18
8,4
XMLUtil$1
100%
2/2
N/A
8,4
XMLUtil$JAXBContextCreator
100%
2/2
N/A
8,4
 
 1  
 /**
 2  
  * Copyright 2010 Sven Diedrichsen
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an
 12  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 13  
  * express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  *
 16  
  * @author sven
 17  
  * @version $Id: $
 18  
  */
 19  
 package de.jollyday.util;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.time.DayOfWeek;
 24  
 import java.util.logging.Logger;
 25  
 
 26  
 import javax.xml.bind.JAXBContext;
 27  
 import javax.xml.bind.JAXBElement;
 28  
 import javax.xml.bind.JAXBException;
 29  
 import javax.xml.bind.Unmarshaller;
 30  
 
 31  
 import de.jollyday.HolidayType;
 32  
 import de.jollyday.config.Configuration;
 33  
 import de.jollyday.config.Month;
 34  
 import de.jollyday.config.ObjectFactory;
 35  
 import de.jollyday.config.Weekday;
 36  
 import de.jollyday.holidaytype.LocalizedHolidayType;
 37  
 
 38  421
 public class XMLUtil {
 39  
 
 40  
         /**
 41  
          * the package name to search for the generated java classes.
 42  
          */
 43  
         public static final String PACKAGE = "de.jollyday.config";
 44  
 
 45  1
         private static Logger LOG = Logger.getLogger(XMLUtil.class.getName());
 46  
 
 47  421
         private JAXBContextCreator contextCreator = new JAXBContextCreator();
 48  421
         private ClassLoadingUtil classLoadingUtil = new ClassLoadingUtil();
 49  
 
 50  
         /**
 51  
          * Unmarshalls the configuration from the stream. Uses <code>JAXB</code> for
 52  
          * this.
 53  
          * 
 54  
          * @param stream
 55  
          *            a {@link java.io.InputStream} object.
 56  
          * @return The unmarshalled configuration.
 57  
          * @throws java.io.IOException
 58  
          *             Could not close the provided stream.
 59  
          */
 60  
         public Configuration unmarshallConfiguration(InputStream stream) throws IOException {
 61  78
                 if (stream == null) {
 62  1
                         throw new IllegalArgumentException("Stream is NULL. Cannot read XML.");
 63  
                 }
 64  
                 try {
 65  77
                         JAXBContext ctx = null;
 66  
                         try {
 67  77
                                 ctx = contextCreator.create(XMLUtil.PACKAGE, classLoadingUtil.getClassloader());
 68  1
                         } catch (JAXBException e) {
 69  1
                                 LOG.warning("Could not create JAXB context using the current threads context classloader. Defaulting to ObjectFactory classloader.");
 70  1
                                 ctx = null;
 71  76
                         }
 72  77
                         if (ctx == null) {
 73  2
                                 ctx = contextCreator.create(XMLUtil.PACKAGE, ObjectFactory.class.getClassLoader());
 74  
                         }
 75  76
                         Unmarshaller um = ctx.createUnmarshaller();
 76  
                         @SuppressWarnings("unchecked")
 77  76
                         JAXBElement<Configuration> el = (JAXBElement<Configuration>) um.unmarshal(stream);
 78  76
                         return el.getValue();
 79  1
                 } catch (JAXBException ue) {
 80  1
                         throw new IllegalStateException("Cannot parse holidays XML file.", ue);
 81  
                 } finally {
 82  77
                         stream.close();
 83  
                 }
 84  
         }
 85  
 
 86  
         /**
 87  
          * Returns the <code>DateTimeConstants</code> value for the given weekday.
 88  
          * 
 89  
          * @param weekday
 90  
          *            a {@link de.jollyday.config.Weekday} object.
 91  
          * @return a DayOfWeek instance.
 92  
          */
 93  
         public final DayOfWeek getWeekday(Weekday weekday) {
 94  622
                 return DayOfWeek.valueOf(weekday.value());
 95  
                 }
 96  
 
 97  
         /**
 98  
          * Returns the <code>DateTimeConstants</code> value for the given month.
 99  
          * 
 100  
          * @param month
 101  
          *            a {@link de.jollyday.config.Month} object.
 102  
          * @return DateTimeConstants value.
 103  
          */
 104  
         public int getMonth(Month month) {
 105  2492
                 switch (month) {
 106  
                 case JANUARY:
 107  444
                         return 1;
 108  
                 case FEBRUARY:
 109  115
                         return 2;
 110  
                 case MARCH:
 111  53
                         return 3;
 112  
                 case APRIL:
 113  149
                         return 4;
 114  
                 case MAY:
 115  358
                         return 5;
 116  
                 case JUNE:
 117  41
                         return 6;
 118  
                 case JULY:
 119  141
                         return 7;
 120  
                 case AUGUST:
 121  90
                         return 8;
 122  
                 case SEPTEMBER:
 123  133
                         return 9;
 124  
                 case OCTOBER:
 125  158
                         return 10;
 126  
                 case NOVEMBER:
 127  375
                         return 11;
 128  
                 case DECEMBER:
 129  435
                         return 12;
 130  
                 default:
 131  0
                         throw new IllegalArgumentException("Unknown month " + month);
 132  
                 }
 133  
         }
 134  
 
 135  
         /**
 136  
          * Gets the type.
 137  
          * 
 138  
          * @param type
 139  
          *            the type of holiday in the config
 140  
          * @return the type of holiday
 141  
          */
 142  
         public HolidayType getType(de.jollyday.config.HolidayType type) {
 143  2825
                 switch (type) {
 144  
                 case OFFICIAL_HOLIDAY:
 145  2825
                         return LocalizedHolidayType.OFFICIAL_HOLIDAY;
 146  
                 case UNOFFICIAL_HOLIDAY:
 147  0
                         return LocalizedHolidayType.UNOFFICIAL_HOLIDAY;
 148  
                 default:
 149  0
                         throw new IllegalArgumentException("Unknown type " + type);
 150  
                 }
 151  
         }
 152  
 
 153  421
         public class JAXBContextCreator {
 154  
                 public JAXBContext create(String packageName, ClassLoader classLoader) throws JAXBException {
 155  75
                         return JAXBContext.newInstance(packageName, classLoader);
 156  
                 }
 157  
         }
 158  
 
 159  
 }