Eclipseplugins
JaxbUnmarshallerGeneric.java
1 package com.proalpha.pds.paconnector.utils;
2 
3 import java.io.File;
4 import java.util.Locale;
5 
6 import javax.xml.bind.JAXBContext;
7 import javax.xml.bind.JAXBException;
8 import javax.xml.bind.Unmarshaller;
9 
10 public class JaxbUnmarshallerGeneric<T> {
11 
12  private final Class<T> clazz;
13 
14  public JaxbUnmarshallerGeneric(final Class<T> clazz) {
15  this.clazz = clazz;
16 
17  }
18 
19  @SuppressWarnings("unchecked")
20  public T readXMLFile(File xmlFile) {
21 
22  Locale defaultLocale = Locale.getDefault();
23  try {
24  final JAXBContext context = JAXBContext.newInstance(clazz);
25  Unmarshaller unmarshaller = context.createUnmarshaller();
26  return (T) unmarshaller.unmarshal(xmlFile);
27 
28  } catch (JAXBException e) {
29  // Jaxb only returns useful error messages if the locale is set to ENGLISH
30  Locale.setDefault(Locale.ENGLISH);
31  throw new IllegalStateException(e);
32  } finally {
33  // Set locale to default afterwards
34  Locale.setDefault(defaultLocale);
35  }
36 
37  }
38 }