Ivelin_1936

XmlParser

Apr 17th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package car.dealer.demo.utils.serialize;
  2.  
  3. import car.dealer.demo.utils.serialize.exeptions.SerializeExeption;
  4. import org.springframework.stereotype.Component;
  5.  
  6. import javax.xml.bind.JAXBContext;
  7. import javax.xml.bind.JAXBException;
  8. import javax.xml.bind.Marshaller;
  9. import javax.xml.bind.Unmarshaller;
  10. import java.io.*;
  11.  
  12. @Component(value = "XmlSerializer")
  13. public class XmlSerializer implements Serializer {
  14.  
  15.  
  16.     @Override
  17.     public <T> void serialize(T t, String fileName) {
  18.         try {
  19.             JAXBContext jaxbContext = JAXBContext.newInstance(t.getClass());
  20.             Marshaller marshaller = jaxbContext.createMarshaller();
  21.             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  22.  
  23.             String path = System.getProperty("user.dir") + File.separator + fileName;
  24.             File f = new File(path);
  25.             if (!f.exists()) {
  26.                 //getParentFile -> взима пътя до преди files.input/output.
  27.                 // mkdirs() -> създава директорийте които не съществуват
  28.                 f.getParentFile().mkdirs();
  29.                 f.createNewFile();
  30.             }
  31.  
  32.             try ( OutputStream outputStream = new FileOutputStream(fileName);
  33.                   BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(outputStream))) {
  34.                 marshaller.marshal(t, bfw);
  35.             }
  36.         } catch (JAXBException e) {
  37.             //log here...
  38.             throw  new SerializeExeption("Could not serialize " + t + "! ", e);
  39.         } catch (IOException ioe) {
  40.             //log here...
  41.             throw  new SerializeExeption("Could not serialize " + t + "! Unable to write file" + fileName + "!", ioe);
  42.         }
  43.     }
  44.  
  45.     @Override
  46.     public <T> T deserialize(Class<T> clazz, String fileName) {
  47.         try {
  48.             JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
  49.             Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  50.  
  51.             try ( InputStream inputStream = clazz.getResourceAsStream(fileName);
  52.                   BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
  53.                 T t = (T) unmarshaller.unmarshal(reader);
  54.                 return t;
  55.             }
  56.         } catch (JAXBException e) {
  57.             //log here...
  58.             throw  new SerializeExeption("Could not deserialize to class " + clazz + "! ", e);
  59.         } catch (IOException ioe) {
  60.             //log here...
  61.             throw  new SerializeExeption("Could not deserialize class " + clazz + "! Unable to read from file" + fileName + "!", ioe);
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment