Search in sources :

Example 1 with TrpTranscriptMetadata

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

the class DocExporter method exportDoc.

/**
 * Export current document with the provided parameters.
 * @param doc current document
 * @param pars export settings
 * @return directory to which the export files were written
 * @throws IOException
 * @throws IllegalArgumentException
 * @throws URISyntaxException
 * @throws JAXBException
 * @throws TransformerException
 */
public File exportDoc(TrpDoc doc, CommonExportPars pars) throws IOException, IllegalArgumentException, URISyntaxException, JAXBException, TransformerException {
    FimgStoreGetClient getter = null;
    FimgStoreUriBuilder uriBuilder = null;
    ImgType imgType = pars.getRemoteImgQuality() == null ? ImgType.orig : pars.getRemoteImgQuality();
    if (doc.isRemoteDoc()) {
        // FIXME fimagestore path should be read from docMd!
        getter = new FimgStoreGetClient("dbis-thure.uibk.ac.at", "f");
        final String scheme = pars.isUseHttps() ? "https" : "http";
        final int port = pars.isUseHttps() ? 443 : 80;
        uriBuilder = new FimgStoreUriBuilder(scheme, getter.getHost(), port, getter.getServerContext());
    }
    // create copy of object, as we alter it here while exporting
    TrpDoc doc2;
    doc2 = new TrpDoc(doc);
    // check and create output directory
    File outputDir = new File(pars.getDir());
    if (!pars.isDoOverwrite() && outputDir.exists()) {
        throw new IOException("File path already exists.");
    }
    outputDir.mkdir();
    // decide where to put the images
    final File imgOutputDir;
    if (pars.isUseOcrMasterDir()) {
        imgOutputDir = new File(outputDir.getAbsolutePath() + File.separatorChar + LocalDocConst.OCR_MASTER_DIR);
        imgOutputDir.mkdir();
    } else {
        imgOutputDir = outputDir;
    }
    File pageOutputDir = null, altoOutputDir = null;
    // check PAGE export settings and create output directory
    String pageDirName = pars.getPageDirName();
    if (pars.isDoExportPageXml() && !StringUtils.isEmpty(pageDirName)) {
        pageOutputDir = new File(outputDir.getAbsolutePath() + File.separatorChar + pageDirName);
        if (pageOutputDir.mkdir()) {
            logger.debug("pageOutputDir created successfully ");
        } else {
            logger.debug("pageOutputDir could not be created!");
        }
    } else {
        // if pageDirName is not set, export the PAGE XMLs to imgOutputDir
        pageOutputDir = imgOutputDir;
    }
    // check Alto export settings and create output directory
    AltoExporter altoEx = new AltoExporter();
    if (pars.isDoExportAltoXml()) {
        altoOutputDir = altoEx.createAltoOuputDir(doc2, outputDir.getAbsolutePath());
    }
    // check and write metadata
    if (doc2.getMd() != null) {
        File fileOut = new File(outputDir.getAbsolutePath() + File.separatorChar + LocalDocConst.METADATA_FILENAME);
        try {
            JaxbUtils.marshalToFile(doc2.getMd(), fileOut);
        } catch (JAXBException e) {
            throw new IOException("Could not marshal metadata to file.", e);
        }
    }
    List<TrpPage> pages = doc2.getPages();
    Set<Integer> pageIndices = pars.getPageIndices(doc.getNPages());
    // do export for all defined pages
    for (int i = 0; i < pages.size(); ++i) {
        if (pageIndices != null && !pageIndices.contains(i)) {
            continue;
        }
        TrpPage p = pages.get(i);
        File imgFile = null, xmlFile = null, altoFile = null;
        URL imgUrl = p.getUrl();
        final String baseFileName = ExportFilePatternUtils.buildBaseFileName(pars.getFileNamePattern(), p);
        final String imgExt = "." + FilenameUtils.getExtension(p.getImgFileName());
        final String xmlExt = ".xml";
        // gather remote files and export document
        if (doc2.isRemoteDoc()) {
            if (pars.isDoWriteImages()) {
                final String msg = "Downloading " + imgType.toString() + " image for page nr. " + p.getPageNr();
                logger.debug(msg);
                updateStatus(msg);
                final URI imgUri = uriBuilder.getImgUri(p.getKey(), imgType);
                imgFile = getter.saveFile(imgUri, imgOutputDir.getAbsolutePath(), baseFileName + imgExt);
                p.setUrl(imgFile.toURI().toURL());
                p.setKey(null);
            }
            if (pars.isDoExportPageXml()) {
                // old
                // TrpTranscriptMetadata t = p.getCurrentTranscript();
                /*
					 * new: to get the previously stored chosen version
					 */
                TrpTranscriptMetadata transcriptMd;
                JAXBPageTranscript transcript = cache.getPageTranscriptAtIndex(i);
                // set up transcript metadata
                if (transcript == null) {
                    transcriptMd = p.getCurrentTranscript();
                    logger.warn("Have to unmarshall transcript in DocExporter for transcript " + transcriptMd + " - should have been built before using ExportUtils::storePageTranscripts4Export!");
                    transcript = new JAXBPageTranscript(transcriptMd);
                    transcript.build();
                } else {
                    transcriptMd = transcript.getMd();
                }
                URL xmlUrl = transcriptMd.getUrl();
                if (pars.isExportTranscriptMetadata()) {
                    MetadataType md = transcript.getPage().getPcGtsType().getMetadata();
                    if (md == null) {
                        throw new JAXBException("Transcript does not contain a metadata element: " + transcriptMd);
                    }
                    String imgUrlStr = CoreUtils.urlToString(imgUrl);
                    String xmlUrlStr = CoreUtils.urlToString(xmlUrl);
                    String status = transcriptMd.getStatus() == null ? null : transcriptMd.getStatus().toString();
                    TranskribusMetadataType tmd = new TranskribusMetadataType();
                    tmd.setDocId(doc.getId());
                    tmd.setPageId(p.getPageId());
                    tmd.setPageNr(p.getPageNr());
                    tmd.setTsid(transcriptMd.getTsId());
                    tmd.setStatus(status);
                    tmd.setUserId(transcriptMd.getUserId());
                    tmd.setImgUrl(imgUrlStr);
                    tmd.setXmlUrl(xmlUrlStr);
                    tmd.setImageId(p.getImageId());
                    md.setTranskribusMetadata(tmd);
                }
                // write transcript to file
                xmlFile = new File(FilenameUtils.normalizeNoEndSeparator(pageOutputDir.getAbsolutePath()) + File.separator + baseFileName + xmlExt);
                logger.debug("PAGE XMl output file: " + xmlFile.getAbsolutePath());
                transcript.write(xmlFile);
                // old code: save file by just downloading to disk
                // xmlFile = getter.saveFile(transcriptMd.getUrl().toURI(), pageOutputDir.getAbsolutePath(), baseFileName + xmlExt);
                // make sure (for other exports) that the transcript that is exported is the only one set in the transcripts list of TrpPage
                p.getTranscripts().clear();
                TrpTranscriptMetadata tCopy = new TrpTranscriptMetadata(transcriptMd, p);
                tCopy.setUrl(xmlFile.toURI().toURL());
                p.getTranscripts().add(tCopy);
            }
        } else {
            updateStatus("Copying local files for page nr. " + p.getPageNr());
            // copy local files during export
            if (pars.isDoWriteImages()) {
                imgFile = LocalDocWriter.copyImgFile(p, p.getUrl(), imgOutputDir.getAbsolutePath(), baseFileName + imgExt);
            }
            if (pars.isDoExportPageXml()) {
                xmlFile = LocalDocWriter.copyTranscriptFile(p, pageOutputDir.getAbsolutePath(), baseFileName + xmlExt, cache);
            }
        }
        // export alto:
        if (pars.isDoExportAltoXml()) {
            altoFile = altoEx.exportAltoFile(p, baseFileName + xmlExt, altoOutputDir, pars.isSplitIntoWordsInAltoXml());
        }
        if (imgFile != null)
            logger.debug("Written image file " + imgFile.getAbsolutePath());
        if (xmlFile != null) {
            logger.debug("Written transcript xml file " + xmlFile.getAbsolutePath());
        } else {
            logger.warn("No transcript was exported for page ");
        }
        if (altoFile != null) {
            logger.debug("Written ALTO xml file " + altoFile.getAbsolutePath());
        } else {
            logger.warn("No alto was exported for page ");
        }
        setChanged();
        notifyObservers(Integer.valueOf(p.getPageNr()));
    }
    if (pars.isDoWriteMets()) {
        // load the exported doc from its new location
        // FIXME this does not work for export of PAGE XMLs only!
        // final TrpDoc localDoc = LocalDocReader.load(outputDir.getAbsolutePath(), false);
        // set local folder or else TrpMetsBuilder will treat this as remote doc!
        doc2.getMd().setLocalFolder(outputDir);
        // write mets with file pointers to local files
        Mets mets = TrpMetsBuilder.buildMets(doc2, pars.isDoExportPageXml(), pars.isDoExportAltoXml(), pars.isDoWriteImages(), pageIndices);
        File metsFile = new File(outputDir.getAbsolutePath() + File.separator + TrpMetsBuilder.METS_FILE_NAME);
        try {
            JaxbUtils.marshalToFile(mets, metsFile, TrpDocMetadata.class);
        } catch (JAXBException e) {
            throw new IOException("Could not marshal METS to file!", e);
        }
    }
    return outputDir;
}
Also used : JAXBPageTranscript(eu.transkribus.core.model.beans.JAXBPageTranscript) TrpPage(eu.transkribus.core.model.beans.TrpPage) ImgType(org.dea.fimgstoreclient.beans.ImgType) JAXBException(javax.xml.bind.JAXBException) TranskribusMetadataType(eu.transkribus.core.model.beans.pagecontent.TranskribusMetadataType) TranskribusMetadataType(eu.transkribus.core.model.beans.pagecontent.TranskribusMetadataType) MetadataType(eu.transkribus.core.model.beans.pagecontent.MetadataType) TrpTranscriptMetadata(eu.transkribus.core.model.beans.TrpTranscriptMetadata) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) AltoExporter(eu.transkribus.core.model.builder.alto.AltoExporter) FimgStoreGetClient(org.dea.fimgstoreclient.FimgStoreGetClient) Mets(eu.transkribus.core.model.beans.mets.Mets) TrpDoc(eu.transkribus.core.model.beans.TrpDoc) File(java.io.File) FimgStoreUriBuilder(org.dea.fimgstoreclient.utils.FimgStoreUriBuilder)

Example 2 with TrpTranscriptMetadata

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

the class LocalDocWriter method copyTranscriptFile.

public static File copyTranscriptFile(TrpPage p, String path, ExportCache cache) throws IOException, JAXBException {
    if (!p.getTranscripts().isEmpty()) {
        String xmlFn = "Transcript_" + p.getPageNr() + ".xml";
        TrpTranscriptMetadata tmd = p.getTranscripts().get(p.getTranscripts().size() - 1);
        URL u = tmd.getUrl();
        if (u.getProtocol().toLowerCase().contains("file")) {
            logger.debug("path: " + u.getPath());
            xmlFn = FilenameUtils.getName(FileUtils.toFile(u).getAbsolutePath());
        } else if (u.getProtocol().toLowerCase().contains("http")) {
            // parse filename from
            String fn = getFilenameFromUrl(u);
            if (fn != null)
                xmlFn = fn;
        }
        return copyTranscriptFile(p, path, xmlFn, cache);
    } else
        return null;
}
Also used : TrpTranscriptMetadata(eu.transkribus.core.model.beans.TrpTranscriptMetadata) URL(java.net.URL)

Example 3 with TrpTranscriptMetadata

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

the class ATeiBuilder method getPcGtsTypeForPage.

// protected abstract void setTextRegion(TextRegionType r, int pageNr);
protected PcGtsType getPcGtsTypeForPage(TrpPage p) throws JAXBException {
    PcGtsType pc;
    if (transcrBuffer.containsKey(p.getPageNr())) {
        pc = transcrBuffer.get(p.getPageNr());
    } else {
        TrpTranscriptMetadata tMd = p.getCurrentTranscript();
        try {
            JAXBPageTranscript tr = new JAXBPageTranscript(tMd);
            tr.build();
            pc = tr.getPageData();
        } catch (IOException je) {
            throw new JAXBException("Could not unmarshal page " + p.getPageNr(), je);
        }
        transcrBuffer.put(p.getPageNr(), pc);
    }
    return pc;
}
Also used : JAXBPageTranscript(eu.transkribus.core.model.beans.JAXBPageTranscript) JAXBException(javax.xml.bind.JAXBException) TrpTranscriptMetadata(eu.transkribus.core.model.beans.TrpTranscriptMetadata) IOException(java.io.IOException) PcGtsType(eu.transkribus.core.model.beans.pagecontent.PcGtsType)

Example 4 with TrpTranscriptMetadata

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

the class TrpDocUploadBuilder method buildPageUploadDescriptor.

private static PageUploadDescriptor buildPageUploadDescriptor(TrpPage p) {
    PageUploadDescriptor i = new PageUploadDescriptor();
    i.setFileName(p.getImgFileName());
    i.setPageNr(p.getPageNr());
    if (!StringUtils.isEmpty(p.getMd5Sum())) {
        i.setImgChecksum(p.getMd5Sum());
    }
    // add transcript if any
    if (!p.getTranscripts().isEmpty()) {
        TrpTranscriptMetadata t = p.getCurrentTranscript();
        i.setPageXmlName(t.getXmlFileName());
        if (!StringUtils.isEmpty(t.getMd5Sum())) {
            i.setPageXmlChecksum(t.getMd5Sum());
        }
    }
    return i;
}
Also used : TrpTranscriptMetadata(eu.transkribus.core.model.beans.TrpTranscriptMetadata) PageUploadDescriptor(eu.transkribus.core.model.beans.DocumentUploadDescriptor.PageUploadDescriptor)

Example 5 with TrpTranscriptMetadata

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

the class MetsUtil method buildPage.

private static TrpPage buildPage(DivType div, List<FileType> imgGrp, List<FileType> xmlGrp, File parentDir) throws IOException {
    TrpPage page = new TrpPage();
    int nr = div.getORDER().intValue();
    page.setPageNr(nr);
    File imgFile = null;
    File xmlFile = null;
    // FIXME this will only work for local files
    for (Fptr ptr : div.getFptr()) {
        FileType type = (FileType) ptr.getArea().getFILEID();
        if (imgGrp.contains(type)) {
            imgFile = MetsUtil.getFile(type, parentDir);
        } else if (xmlGrp.contains(type)) {
            xmlFile = MetsUtil.getFile(type, parentDir);
        }
    }
    if (imgFile == null) {
        logger.error("No master image mapped for page " + nr + " in the structmap!");
    } else {
        logger.info("Page " + page.getPageNr() + " image: " + imgFile.getAbsolutePath());
    }
    // FIXME NullpointerException if imgFile == null!
    page.setUrl(imgFile.toURI().toURL());
    page.setKey(null);
    page.setDocId(-1);
    page.setImgFileName(imgFile.getName());
    if (xmlFile == null) {
        logger.error("No master xml mapped for page " + nr + " in the structmap!");
    } else {
        logger.info("Page " + page.getPageNr() + " xml: " + xmlFile.getAbsolutePath());
    }
    // FIXME NullpointerException if xmlFile == null!
    TrpTranscriptMetadata tmd = new TrpTranscriptMetadata();
    tmd.setPageReferenceForLocalDocs(page);
    tmd.setPageId(page.getPageId());
    tmd.setUrl(xmlFile.toURI().toURL());
    tmd.setKey(null);
    tmd.setStatus(EditStatus.NEW);
    tmd.setTimestamp(new Date().getTime());
    tmd.setUserName("LocalDocReader");
    page.getTranscripts().add(tmd);
    return page;
}
Also used : TrpPage(eu.transkribus.core.model.beans.TrpPage) FileType(eu.transkribus.core.model.beans.mets.FileType) Fptr(eu.transkribus.core.model.beans.mets.DivType.Fptr) TrpTranscriptMetadata(eu.transkribus.core.model.beans.TrpTranscriptMetadata) File(java.io.File) Date(java.util.Date)

Aggregations

TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)21 TrpPage (eu.transkribus.core.model.beans.TrpPage)14 JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)11 TrpPageType (eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)7 IOException (java.io.IOException)7 File (java.io.File)6 URL (java.net.URL)4 PcGtsType (eu.transkribus.core.model.beans.pagecontent.PcGtsType)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 JAXBException (javax.xml.bind.JAXBException)3 Rtf (com.tutego.jrtf.Rtf)2 TrpDoc (eu.transkribus.core.model.beans.TrpDoc)2 CustomTag (eu.transkribus.core.model.beans.customtags.CustomTag)2 Fptr (eu.transkribus.core.model.beans.mets.DivType.Fptr)2 FileType (eu.transkribus.core.model.beans.mets.FileType)2 Mets (eu.transkribus.core.model.beans.mets.Mets)2 TextLineType (eu.transkribus.core.model.beans.pagecontent.TextLineType)2 WordType (eu.transkribus.core.model.beans.pagecontent.WordType)2