Search in sources :

Example 6 with PcGtsType

use of eu.transkribus.core.model.beans.pagecontent.PcGtsType in project TranskribusCore by Transkribus.

the class PageXmlUtils method createEmptyPcGtsType.

public static PcGtsType createEmptyPcGtsType(final URL imgUrl, Dimension dim) throws IOException {
    final String prot = imgUrl.getProtocol();
    PcGtsType pcGts;
    if (prot.startsWith("http")) {
        // fimagestore file
        FimgStoreImgMd md = FimgStoreReadConnection.getImgMd(imgUrl);
        pcGts = createEmptyPcGtsTypeForRemoteImg(imgUrl, md);
    } else {
        // try to deal with it as local file
        final File imgFile = FileUtils.toFile(imgUrl);
        pcGts = createEmptyPcGtsType(imgFile, dim);
    }
    return pcGts;
}
Also used : FimgStoreImgMd(org.dea.fimgstoreclient.beans.FimgStoreImgMd) PcGtsType(eu.transkribus.core.model.beans.pagecontent.PcGtsType) File(java.io.File)

Example 7 with PcGtsType

use of eu.transkribus.core.model.beans.pagecontent.PcGtsType in project TranskribusCore by Transkribus.

the class PageXmlUtils method unmarshal.

public static PcGtsType unmarshal(String pageStr) throws JAXBException {
    Unmarshaller u = createUnmarshaller();
    StringReader sr = new StringReader(pageStr);
    @SuppressWarnings("unchecked") PcGtsType pageData = ((JAXBElement<PcGtsType>) u.unmarshal(sr)).getValue();
    return pageData;
}
Also used : StringReader(java.io.StringReader) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) PcGtsType(eu.transkribus.core.model.beans.pagecontent.PcGtsType)

Example 8 with PcGtsType

use of eu.transkribus.core.model.beans.pagecontent.PcGtsType in project TranskribusCore by Transkribus.

the class PageXmlUtils method unmarshal.

public static PcGtsType unmarshal(File file) throws JAXBException {
    Unmarshaller u = createUnmarshaller();
    // Use FileInputStream because JaxB handles File objects via URL internally and thus does not
    // allow all POSIX compliant file names, e.g. containing "#"
    FileInputStream fis;
    try {
        fis = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        throw new JAXBException("File not found: " + file.getAbsolutePath(), e);
    }
    @SuppressWarnings("unchecked") PcGtsType pageData = ((JAXBElement<PcGtsType>) u.unmarshal(fis)).getValue();
    onPostConstruct(pageData);
    return pageData;
}
Also used : JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) PcGtsType(eu.transkribus.core.model.beans.pagecontent.PcGtsType) FileInputStream(java.io.FileInputStream)

Example 9 with PcGtsType

use of eu.transkribus.core.model.beans.pagecontent.PcGtsType 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)

Example 10 with PcGtsType

use of eu.transkribus.core.model.beans.pagecontent.PcGtsType in project TranskribusCore by Transkribus.

the class PageXmlUtils method unmarshal.

public static PcGtsType unmarshal(byte[] bytes) throws JAXBException {
    Unmarshaller u = createUnmarshaller();
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    @SuppressWarnings("unchecked") PcGtsType pageData = ((JAXBElement<PcGtsType>) u.unmarshal(bis)).getValue();
    onPostConstruct(pageData);
    return pageData;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) PcGtsType(eu.transkribus.core.model.beans.pagecontent.PcGtsType)

Aggregations

PcGtsType (eu.transkribus.core.model.beans.pagecontent.PcGtsType)36 File (java.io.File)16 IOException (java.io.IOException)16 JAXBException (javax.xml.bind.JAXBException)11 TrpPage (eu.transkribus.core.model.beans.TrpPage)8 TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)6 URL (java.net.URL)6 JAXBElement (javax.xml.bind.JAXBElement)6 Unmarshaller (javax.xml.bind.Unmarshaller)6 TextRegionType (eu.transkribus.core.model.beans.pagecontent.TextRegionType)5 FileNotFoundException (java.io.FileNotFoundException)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 TransformerException (javax.xml.transform.TransformerException)5 SAXException (org.xml.sax.SAXException)5 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)4 TrpPageType (eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)4 TrpTextLineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)4 Dimension (java.awt.Dimension)4 FimgStoreImgMd (org.dea.fimgstoreclient.beans.FimgStoreImgMd)4 XmlFormat (eu.transkribus.core.io.formats.XmlFormat)3