/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package com.megatim.fdxconvert.util; import com.megatim.fdxconvert.model.JsonStructure; import com.megatim.fdxconvert.model.TypeFichierJson; import com.megatim.fdxconvert.model.jaxb.TypeFichierJsonsList; import java.io.File; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; /** * * @author ASUS */ public class TypeFichierJsonMarshaller { public static void marshall(List typeFichierJsons, String destinationDir) throws JAXBException { if (typeFichierJsons == null) { typeFichierJsons = new ArrayList<>(); } JAXBContext context = JAXBContext.newInstance(TypeFichierJsonsList.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); String date = formatter.format(LocalDateTime.now()); marshaller.marshal(new TypeFichierJsonsList(typeFichierJsons), new File(destinationDir, "typeFichierJsons" + date + ".xml")); } public static void marshall(String destinationDir, TypeFichierJson typeFichierJson) throws JAXBException { if (typeFichierJson == null) { return; } JAXBContext context = JAXBContext.newInstance(TypeFichierJson.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); String date = formatter.format(LocalDateTime.now()); marshaller.marshal(typeFichierJson, new File(destinationDir, typeFichierJson.getTypeFichier().getCode() + date + ".xml")); } public static TypeFichierJsonsList unMarshall(String filePath) throws JAXBException { JAXBContext context = JAXBContext.newInstance(TypeFichierJsonsList.class); Unmarshaller unmarshaller = context.createUnmarshaller(); TypeFichierJsonsList typeFichierJsonsList = (TypeFichierJsonsList) unmarshaller.unmarshal(new File(filePath)); typeFichierJsonsList.getTypeFichierJson().forEach(t -> setParent(t.getJsonStructure())); return typeFichierJsonsList; } public static TypeFichierJson unMarshallOne(String filePath) throws JAXBException { JAXBContext context = JAXBContext.newInstance(TypeFichierJson.class); Unmarshaller unmarshaller = context.createUnmarshaller(); TypeFichierJson typeFichierJson = (TypeFichierJson) unmarshaller.unmarshal(new File(filePath)); setParent(typeFichierJson.getJsonStructure()); return typeFichierJson; } private static void setParent(JsonStructure jsonStruct) { jsonStruct.getFields().forEach(f -> { f.setParent(jsonStruct); setParent(f); }); } }