1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
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 | |
|
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 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
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 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public final DayOfWeek getWeekday(Weekday weekday) { |
94 | 622 | return DayOfWeek.valueOf(weekday.value()); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
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 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
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 | |
} |