1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| package com.inet.dd5.service1;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.MarshalException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.ValidationException;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import com.inet.dd5.erreur.ErreurFichierTraitement;
import de.kba.verwendungsnachweis.ContainerTyp;
/**
* Classe regroupant les methodes d'ecriture du fichier XML
*
*/
public class EcritureFichierXML
{
private Writer out;
private Marshaller marshaller;
public static final String FILE_ENCODING = "UTF-8"; // "ISO-8859-1" ou "ISO-8859-15" ou "UTF-16"
private static Logger log = Logger.getLogger(EcritureFichierXML.class);
/**
*
* Constructeur initialisant l'objet Marshall
* @param nomFichierSortie Le chemin complet du fichier de sortie
* @throws IOException
*/
public EcritureFichierXML(String nomFichierSortie) throws IOException
{
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(nomFichierSortie), FILE_ENCODING));
try {
JAXBContext jc = JAXBContext.newInstance( "de.kba.verwendungsnachweis" );
Schema schema = null ;
try {
// creation de l'instance chemin
File schemaFile = new File("Ressources/vwn_extern_V_1_0.xsd");
// creation de l'instance schema xsd
schema = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);
} catch (SAXException e) {
log.error("Impossible de lire le schéma XML (fichiers XSD)", e);
}
marshaller = jc.createMarshaller();
marshaller.setSchema(schema);
// Permet de tabuler
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.valueOf(true));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("Impossible de lire le schéma XML (fichiers XSD)", e);
}
}
/**
* Ecriture d'un enregistrement RECORD du fichier XML de sortie
* @param record Enregistrement du fichier
* @throws ErreurFichierTraitement
* @throws JAXBException
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public void writeServiceDMR(ContainerTyp newContainerTyp ) throws ErreurFichierTraitement, JAXBException, UnsupportedEncodingException, FileNotFoundException
{
try
{
marshaller.marshal(newContainerTyp, out);
}
catch (MarshalException e1)
{
log.fatal("Marshall impossible : (constitution du fichier XML)");
e1.printStackTrace();
throw new ErreurFichierTraitement("Marshall impossible : (constitution du fichier XML)");
}
catch (ValidationException e1)
{
log.fatal("Le fichier a un format non valide");
e1.printStackTrace();
throw new ErreurFichierTraitement("Le fichier a un format non valide)");
}
}
} |
Partager