Search in sources :

Example 6 with TrpDoc

use of eu.transkribus.core.model.beans.TrpDoc 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 7 with TrpDoc

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

the class LocalDocWriter method main.

public static void main(String[] args) {
    try {
        // TrpDoc doc = LocalDocReader.load("/media/dea_scratch/TRP/TrpTestDoc_20140127");
        TrpDoc doc = LocalDocReader.load("C:/TrpTestDoc_20140127", true);
        LocalDocWriter.createThumbsForDoc(doc, true);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    if (true) {
        return;
    }
    URL url;
    try {
        url = new URL("https://dbis-thure.uibk.ac.at/fimagestore/Get?id=PZOFOVIFVQPMXDGPKJNLQVUH");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        // conn.setAllowUserInteraction(false);
        // conn.setDoInput(true);
        // conn.setDoOutput(true);
        conn.connect();
        String raw = conn.getHeaderField("Content-Disposition");
        if (raw != null && raw.indexOf("=") != -1) {
            String fileName = raw.split("=")[1];
            System.out.println("filename = " + fileName);
        } else {
        // fall back to random generated file name?
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TrpDoc(eu.transkribus.core.model.beans.TrpDoc) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL)

Example 8 with TrpDoc

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

the class FinereaderUtils method main.

public static void main(String[] args) throws JAXBException, UnsupportedFormatException, IOException {
    // File f = new File("/mnt/dea_scratch/TRP/sample_buch_done/page/bsb00087391_00009.xml");
    // 
    // PcGtsType pc = PageXmlUtils.unmarshal(f);
    // try{
    // pc = replaceBadChars(pc);
    // } catch (IllegalArgumentException e){
    // logger.debug(f.getName());
    // }
    TrpDoc doc = LocalDocReader.load("/mnt/dea_scratch/tmp_philip/Transidee/bsb00087908_hebbel_bernauer", false);
    addTextStyleToWords(doc);
// final String dirPath = "/mnt/dea_scratch/TRP/sample_buch_done/page";
// 
// File dir = new File(dirPath);
// File[] files = dir.listFiles(new FilenameFilter(){
// @Override
// public boolean accept(File dir, String name) {
// return name.endsWith("xml");
// }
// });
// 
// for(File f : files){
// PcGtsType pc = PageXmlUtils.unmarshal(f);
// try{
// replaceBadChars(pc);
// } catch (IllegalArgumentException e){
// logger.debug(f.getName());
// }
// }
// String softHyphen = "\u00AD";
// System.out.println( " ---" + softHyphen + "--- ");
}
Also used : TrpDoc(eu.transkribus.core.model.beans.TrpDoc)

Example 9 with TrpDoc

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

the class TrpDocPackerTest method main.

public static void main(String[] args) {
    // final String BASE = "/mnt/dea_scratch/TRP/";
    // //	String docPath = BASE + "Bentham_box_002/";
    // String shitDoc = BASE + "Schauplatz";
    // String shitDoc = BASE + "TRPTestDoc_I_ZvS_1901_1Q";
    String shitDoc = "/mnt/iza_retro/P6080-029-019_transcriptorium/master_images/14_bozen_stadtarchiv/Ratsprotokolle Bozen 1470-1684 - Lieferung USB Platte 9-7-2013/HS 37/HS 37a";
    String zipPath = "/tmp/test.zip";
    try {
        TrpDocPacker packer = new TrpDocPacker();
        TrpDoc doc = LocalDocReader.load(shitDoc, true);
        Md5SumComputer md5Comp = new Md5SumComputer();
        // md5Comp.addObserver(passthroughObserver);
        doc = md5Comp.computeAndSetMd5Sums(doc);
        File zipFile = packer.packDocFiles(doc, zipPath);
    // 
    // TrpDoc doc2 = packer.unpackDoc(new File(unzipPath), null);
    // System.out.println(doc2);
    // TrpDoc doc = LocalDocReader.load(docPath, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Md5SumComputer(eu.transkribus.core.io.util.Md5SumComputer) TrpDoc(eu.transkribus.core.model.beans.TrpDoc) TrpDocPacker(eu.transkribus.core.io.TrpDocPacker) File(java.io.File)

Example 10 with TrpDoc

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

the class PdfExporterTest method main.

public static void main(String[] args) throws MalformedURLException, DocumentException, IOException, JAXBException, URISyntaxException, InterruptedException {
    // final String path = "/mnt/dea_scratch/TRP/Bentham_baselines";
    final String path = "/mnt/dea_scratch/TRP/Bentham_box_002_GT";
    // final String path = "/mnt/dea_scratch/TRP/Schauplatz_Small";
    TrpDoc doc = LocalDocReader.load(path);
    (new PdfExporter()).export(doc, "/tmp/test.pdf", null, false, false, false, false, false, false, null);
}
Also used : TrpDoc(eu.transkribus.core.model.beans.TrpDoc) PdfExporter(eu.transkribus.core.model.builder.pdf.PdfExporter)

Aggregations

TrpDoc (eu.transkribus.core.model.beans.TrpDoc)21 File (java.io.File)12 IOException (java.io.IOException)11 TrpDocMetadata (eu.transkribus.core.model.beans.TrpDocMetadata)9 TrpPage (eu.transkribus.core.model.beans.TrpPage)6 JAXBException (javax.xml.bind.JAXBException)6 Mets (eu.transkribus.core.model.beans.mets.Mets)5 FileNotFoundException (java.io.FileNotFoundException)4 PcGtsType (eu.transkribus.core.model.beans.pagecontent.PcGtsType)3 CorruptImageException (eu.transkribus.core.exceptions.CorruptImageException)2 JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)2 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)2 ExportCache (eu.transkribus.core.model.builder.ExportCache)2 Dimension (java.awt.Dimension)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 TrpDocPacker (eu.transkribus.core.io.TrpDocPacker)1 XmlFormat (eu.transkribus.core.io.formats.XmlFormat)1 Md5SumComputer (eu.transkribus.core.io.util.Md5SumComputer)1 PageUploadDescriptor (eu.transkribus.core.model.beans.DocumentUploadDescriptor.PageUploadDescriptor)1