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
| public class FILEProvider extends XMLProvider{
private static Logger logger = Logger.getLogger (FILEProvider.class);
private XMLInputFactory myfactory;
private XMLEventReader myfile;
private InputStream in = null;
private XMLOutputFactory outfactory;
private XMLEventWriter outputfile;
private FileOutputStream out = null;
private HashMap<String, String> path=new HashMap<String, String>();
public FILEProvider(String file, String output) throws FileNotFoundException, XMLStreamException, ProviderException {
super(file);
this.myfactory = XMLInputFactory.newInstance();
this.outfactory = XMLOutputFactory.newInstance();
try {
this.in = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("Erreur, fichier : "+file+" introuvable");
e.printStackTrace();
}
try {
this.myfile=myfactory.createXMLEventReader(in);
} catch (XMLStreamException e) {
System.out.println("");
e.printStackTrace();
}
try {
this.out = new FileOutputStream(output);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
this.outputfile = outfactory.createXMLEventWriter(out);
}
@SuppressWarnings("unchecked")
public void write(Manager manager) {
try {
XMLEventWriter eventWriter = outfactory.createXMLEventWriter(this.out);
// Create a EventFactory
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
startwrite(eventWriter,eventFactory);
for(Class<Entity> mapKey : manager.getClasses().keySet()){
for(Entity entity : manager.getObjects(mapKey)){
try {
XMLConverter conv = this.getConverter(entity.getClass());
conv.write(outputfile, entity);
System.out.println("Printing to output "+entity.getId());
} catch (ProviderException e) {
logger.error("Error while trying to get Converter "+entity.getClass());
}
}
}
eventWriter.flush();
eventWriter.close();
} catch (XMLStreamException e) {
logger.error("error reading stream while trying to write file");
}
}
private void startwrite(XMLEventWriter eventWriter, XMLEventFactory eventFactory) throws XMLStreamException {
//end = eventFactory.createDTD("\n");
// Create and write Start Tag
StartDocument startDocument = eventFactory.createStartDocument();
eventWriter.add(startDocument);
// Create config open tag
StartElement configStartElement = eventFactory.createStartElement("", "", "package");
eventWriter.add(configStartElement);
}
} |
Partager