use of eu.transkribus.core.model.beans.pagecontent.ObjectFactory in project TranskribusCore by Transkribus.
the class PageXmlUtils method marshalToBytes.
public static byte[] marshalToBytes(PcGtsType page) throws JAXBException {
ValidationEventCollector vec = new ValidationEventCollector();
Marshaller marshaller = createMarshaller(vec);
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<PcGtsType> je = objectFactory.createPcGts(page);
byte[] data;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
try {
marshaller.marshal(je, out);
data = out.toByteArray();
} finally {
out.close();
}
} catch (Exception e) {
throw new MarshalException(e);
}
String msg = buildMsg(vec, page);
if (!msg.startsWith(NO_EVENTS_MSG))
logger.info(msg);
return data;
}
use of eu.transkribus.core.model.beans.pagecontent.ObjectFactory in project TranskribusCore by Transkribus.
the class PageXmlUtils method marshalToFile.
// public static File marshalToFile(PcGtsType page, File fileOut) throws JAXBException, IOException {
// return marshalToFile(page, fileOut, true);
// }
public static File marshalToFile(PcGtsType page, File fileOut) throws JAXBException, IOException {
ValidationEventCollector vec = new ValidationEventCollector();
Marshaller marshaller = createMarshaller(vec);
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<PcGtsType> je = objectFactory.createPcGts(page);
File backup = null;
if (fileOut.exists()) {
logger.debug("file exists: " + fileOut.getAbsolutePath() + " - backing up!");
backup = CoreUtils.backupFile(fileOut);
}
try {
marshaller.marshal(je, fileOut);
} catch (Exception e) {
if (backup != null) {
logger.debug("restoring backup: " + backup.getAbsolutePath());
FileUtils.copyFile(backup, fileOut);
}
if (e instanceof JAXBException)
throw e;
else
throw new JAXBException(e.getMessage(), e);
} finally {
if (backup != null)
backup.delete();
}
String msg = buildMsg(vec, page);
if (!msg.startsWith(NO_EVENTS_MSG))
logger.info(msg);
return fileOut;
}
Aggregations