Eclipseplugins
JaxUnmarshallerGeneric.java
1 package com.proalpha.pds.templates.jaxb;
2 
3 import java.io.File;
4 
5 import javax.xml.bind.JAXBContext;
6 import javax.xml.bind.JAXBException;
7 import javax.xml.bind.Unmarshaller;
8 
9 public class JaxUnmarshallerGeneric<T> {
10 
11  private final Class<T> clazz;
12 
13  public JaxUnmarshallerGeneric(final Class<T> clazz) {
14  this.clazz = clazz;
15 
16  }
17 
18  @SuppressWarnings("unchecked")
19  public T readXMLFile(String filePath) throws JAXBException {
20 
21  final JAXBContext context = JAXBContext.newInstance(clazz);
22  Unmarshaller unmarshaller = context.createUnmarshaller();
23 
24  return (T) unmarshaller.unmarshal(new File(filePath));
25 
26  }
27  @SuppressWarnings("unchecked")
28  public T readXMLFile(File xmlFile) throws JAXBException {
29 
30  final JAXBContext context = JAXBContext.newInstance(clazz);
31  Unmarshaller unmarshaller = context.createUnmarshaller();
32 
33  return (T) unmarshaller.unmarshal(xmlFile);
34 
35  }
36 }