Search in sources :

Example 1 with AltoExporter

use of eu.transkribus.core.model.builder.alto.AltoExporter 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)

Aggregations

JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)1 TrpDoc (eu.transkribus.core.model.beans.TrpDoc)1 TrpPage (eu.transkribus.core.model.beans.TrpPage)1 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)1 Mets (eu.transkribus.core.model.beans.mets.Mets)1 MetadataType (eu.transkribus.core.model.beans.pagecontent.MetadataType)1 TranskribusMetadataType (eu.transkribus.core.model.beans.pagecontent.TranskribusMetadataType)1 AltoExporter (eu.transkribus.core.model.builder.alto.AltoExporter)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URL (java.net.URL)1 JAXBException (javax.xml.bind.JAXBException)1 FimgStoreGetClient (org.dea.fimgstoreclient.FimgStoreGetClient)1 ImgType (org.dea.fimgstoreclient.beans.ImgType)1 FimgStoreUriBuilder (org.dea.fimgstoreclient.utils.FimgStoreUriBuilder)1