use of eu.transkribus.core.model.beans.TrpPage 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;
}
use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.
the class LocalDocWriter method updateImageDimension.
public static void updateImageDimension(JAXBPageTranscript transcript) throws FileNotFoundException, IOException {
TrpPage page = transcript.getMd().getPagePageReferenceForLocalDocs();
if (page == null) {
logger.warn("Could not update transcript image dimensions - page object is null - should not happen for local docs: " + transcript.getMd().getUrl());
return;
}
// update img file name and dimensions in page XML
Dimension dim = ImgUtils.readImageDimensions(FileUtils.toFile(page.getUrl()));
final String imgFileName = page.getImgFileName();
transcript.getPage().setImageFilename(imgFileName);
transcript.getPage().setImageHeight(dim.height);
transcript.getPage().setImageWidth(dim.width);
logger.debug("Updated PAGE XML: ImgFileName = " + imgFileName + ", dim = " + dim.width + "x" + dim.height);
}
use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.
the class LocalDocWriter method createThumbsForDoc.
// public static String getThumbFileName(TrpPage page) throws IOException {
// File imgFile = FileUtils.toFile(page.getUrl());
// if (imgFile == null)
// throw new IOException("Cannot retrieve image url from: "+page.getUrl());
//
// File thmbsDir = new File(FilenameUtils.getFullPath(imgFile.getAbsolutePath())+"/"+LocalDocConst.THUMBS_FILE_SUB_FOLDER);
// File outFile = new File(thmbsDir.getAbsolutePath()+"/"+FilenameUtils.getBaseName(imgFile.getName())+".jpg");
//
// return outFile.getAbsolutePath();
// }
/* @deprecated Does not work on mac */
@Deprecated
public static void createThumbsForDoc(TrpDoc doc, boolean overwrite) throws Exception {
checkIfLocalDoc(doc);
File thmbsDir = new File(doc.getMd().getLocalFolder().getAbsolutePath() + File.separator + LocalDocConst.THUMBS_FILE_SUB_FOLDER);
FileUtils.forceMkdir(thmbsDir);
int newHeight = LocalDocConst.THUMB_SIZE_HEIGHT;
for (TrpPage p : doc.getPages()) {
File imgFile = FileUtils.toFile(p.getUrl());
if (imgFile == null)
throw new IOException("Cannot retrieve image url from: " + p.getUrl());
File thumbsFile = FileUtils.toFile(p.getThumbUrl());
if (thumbsFile == null)
throw new IOException("Cannot retrieve thumbs url from: " + p.getThumbUrl());
if (// skip if already there and overwrite not specified
thumbsFile.exists() && !overwrite)
continue;
logger.debug("creating thumb file: " + thumbsFile);
long st = System.currentTimeMillis();
if (true) {
}
if (false) {
BufferedImage originalImage = ImgUtils.readImage(imgFile);
if (originalImage == null)
throw new IOException("Cannot load image " + imgFile.getAbsolutePath());
double sf = (double) newHeight / (double) originalImage.getHeight();
int newWidth = (int) (sf * originalImage.getWidth());
BufferedImage thmbImg = new BufferedImage(newWidth, newHeight, originalImage.getType());
Graphics2D g = thmbImg.createGraphics();
RenderingHints rh = new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHints(rh);
g.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
g.dispose();
if (!ImageIO.write(thmbImg, FilenameUtils.getExtension(thumbsFile.getName()), thumbsFile))
throw new Exception("Could not write thumb file - no appropriate writer found!");
}
logger.debug("created thumb file: " + thumbsFile.getAbsolutePath() + " time = " + (System.currentTimeMillis() - st));
}
}
use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.
the class LocalDocWriter method writeTrpDoc.
@Deprecated
public static /*
* Use {@link DocExporter#writeRawDoc}
* @deprecated
*/
void writeTrpDoc(TrpDoc doc, String path, ExportCache cache) throws Exception {
if (doc == null) {
throw new Exception("Null document given!");
}
// if (doc.getMd().getLocalFolder()==null)
// throw new Exception("This is not a local document!");
File dir = new File(path);
FileUtils.forceMkdir(dir);
String pathN = FilenameUtils.normalize(path);
// write metadata:
if (doc.getMd() != null) {
writeTrpDocMetadata(doc, path);
}
// write all images and page xml:
for (TrpPage p : doc.getPages()) {
File imgFile = copyImgFile(p, p.getUrl(), pathN);
logger.debug("Written image file " + imgFile.getAbsolutePath());
File xmlFile = copyTranscriptFile(p, pathN + "/" + LocalDocConst.PAGE_FILE_SUB_FOLDER, cache);
if (xmlFile != null)
logger.debug("Written transcript xml file " + xmlFile.getAbsolutePath());
else
logger.debug("No transcript found for page " + p.getPageNr());
}
}
use of eu.transkribus.core.model.beans.TrpPage in project TranskribusCore by Transkribus.
the class TrpDocUploadBuilder method build.
public static DocumentUploadDescriptor build(TrpDoc doc) {
if (doc == null) {
throw new IllegalArgumentException("doc is null.");
}
if (doc.getMd().getLocalFolder() == null) {
throw new IllegalArgumentException("doc is no localdocument.");
}
if (doc.getNPages() < 1) {
throw new IllegalArgumentException("doc has no pages.");
}
DocumentUploadDescriptor s = new DocumentUploadDescriptor();
s.setMd(doc.getMd());
for (TrpPage p : doc.getPages()) {
s.getPages().add(buildPageUploadDescriptor(p));
}
return s;
}
Aggregations