Search in sources :

Example 1 with ObjectFactory

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) MarshalException(javax.xml.bind.MarshalException) TrpObjectFactory(eu.transkribus.core.model.beans.pagecontent_trp.TrpObjectFactory) ObjectFactory(eu.transkribus.core.model.beans.pagecontent.ObjectFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ValidationEventCollector(javax.xml.bind.util.ValidationEventCollector) PcGtsType(eu.transkribus.core.model.beans.pagecontent.PcGtsType) MarshalException(javax.xml.bind.MarshalException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with ObjectFactory

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) TrpObjectFactory(eu.transkribus.core.model.beans.pagecontent_trp.TrpObjectFactory) ObjectFactory(eu.transkribus.core.model.beans.pagecontent.ObjectFactory) JAXBException(javax.xml.bind.JAXBException) ValidationEventCollector(javax.xml.bind.util.ValidationEventCollector) PcGtsType(eu.transkribus.core.model.beans.pagecontent.PcGtsType) File(java.io.File) MarshalException(javax.xml.bind.MarshalException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ObjectFactory (eu.transkribus.core.model.beans.pagecontent.ObjectFactory)2 PcGtsType (eu.transkribus.core.model.beans.pagecontent.PcGtsType)2 TrpObjectFactory (eu.transkribus.core.model.beans.pagecontent_trp.TrpObjectFactory)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 JAXBException (javax.xml.bind.JAXBException)2 MarshalException (javax.xml.bind.MarshalException)2 Marshaller (javax.xml.bind.Marshaller)2 ValidationEventCollector (javax.xml.bind.util.ValidationEventCollector)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 SAXException (org.xml.sax.SAXException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1