use of eu.transkribus.core.model.beans.JAXBPageTranscript in project TranskribusCore by Transkribus.
the class ExportCache method storePageTranscripts4Export.
public void storePageTranscripts4Export(TrpDoc doc, Set<Integer> pageIndices, IProgressMonitor monitor, String versionStatus, int pageIdx, TrpTranscriptMetadata loadedTranscript) throws Exception {
pageTranscripts = new ArrayList<JAXBPageTranscript>();
List<TrpPage> pages = doc.getPages();
int totalPages = pages.size();
int c = 0;
for (int i = 0; i < totalPages; ++i) {
if (pageIndices != null && !pageIndices.contains(i)) {
// fill up with null to have the proper index of each page later on
// logger.debug(" add null to transcripts " + i);
pageTranscripts.add(null);
continue;
}
if (monitor != null && monitor.isCanceled()) {
throw new Exception("User canceled the export");
}
TrpPage page = pages.get(i);
TrpTranscriptMetadata md = null;
if (versionStatus.contains("Latest")) {
// current transcript
md = page.getCurrentTranscript();
} else if (versionStatus.contains("Loaded")) {
// if loaded page idx == i than we can export the loaded version and for all other pages the latest
if (i == pageIdx && loadedTranscript != null) {
md = loadedTranscript;
// String loadedStatus = loadedTranscript.getStatus().getStr();
// md = page.getTranscriptWithStatus(loadedStatus);
} else {
md = page.getCurrentTranscript();
}
} else {
// logger.debug("We want to export pages with status: " + versionStatus);
md = page.getTranscriptWithStatusOrNull(versionStatus);
}
/*
* for pages where we have not found versions with the defined status -> remove from the page list so that
* they will not exported
*/
if (md == null) {
// pageIndices contained but has not the desired status -> add transcript 0;
if (pageIndices != null && pageIndices.contains(i)) {
// logger.debug("remove page index " + i);
pageIndices.remove(new Integer(i));
pageTranscripts.add(null);
}
continue;
}
JAXBPageTranscript tr = new JAXBPageTranscript(md);
tr.build();
pageTranscripts.add(tr);
logger.debug("Loaded Transcript from page " + (i + 1));
if (monitor != null) {
monitor.setTaskName("Loaded Transcript from page " + (i + 1));
monitor.worked(++c);
}
}
}
use of eu.transkribus.core.model.beans.JAXBPageTranscript in project TranskribusCore by Transkribus.
the class PageXmlDaoTest method main.
public static void main(String[] args) {
TrpDoc doc = FakeDocProvider.create(false);
TrpTranscriptMetadata md = doc.getPages().get(0).getTranscripts().get(0);
try {
JAXBPageTranscript transcript = new JAXBPageTranscript(md);
transcript.build();
// JAXBPageTranscript transcript = TrpPageTranscriptBuilder.build(md);
// get Source Document as String
// DOMSource domSource = new DOMSource(transcript.getSourceDoc());
// StringWriter writer = new StringWriter();
// StreamResult result = new StreamResult(writer);
// TransformerFactory tf = TransformerFactory.newInstance();
// Transformer transformer = tf.newTransformer();
// transformer.transform(domSource, result);
// System.out.println("XML IN String format is: \n" + writer.toString());
// check JaxB Element
PcGtsType page = transcript.getPageData();
if (page == null) {
System.out.println("page XML is null");
System.exit(0);
}
PageType pageType = page.getPage();
System.out.println(page.getMetadata());
if (pageType == null) {
System.out.println("pagetype element is null");
System.exit(0);
}
if (pageType.getTextRegionOrImageRegionOrLineDrawingRegion() == null) {
System.out.println("Region list is null");
System.exit(0);
}
int i = 0;
for (TextRegionType tr : transcript.getPage().getTextRegions(true)) {
tr.setId("" + i++);
}
List<TrpRegionType> regions = pageType.getTextRegionOrImageRegionOrLineDrawingRegion();
for (RegionType r : regions) {
if (r instanceof TextRegionType) {
TextRegionType t = (TextRegionType) r;
System.out.println(t.getId());
}
}
} catch (IllegalArgumentException | IOException e) {
e.printStackTrace();
}
// try {
// PrimaPageTranscript ppt = PageXmlDao.getPrimaPageTranscript(md);
// Page page = ppt.getPageData();
//
// System.out.println(page.getImageFilename());
// Region r = page.getLayout().getRegion("tempReg357564684568544579089");
// System.out.println(r.getType().getName());
// // System.out.println(page.getLayout().getParentChildRelation(r.getType(), r.getId().toString()).getRelationType());
//
// IdRegister idr = r.getIdRegister();
// // idr.
// System.out.println(idr);
// } catch (IllegalArgumentException e) {
//
// e.printStackTrace();
// } catch (MalformedURLException e) {
//
// e.printStackTrace();
// } catch (UnsupportedFormatVersionException e) {
//
// e.printStackTrace();
// }
}
use of eu.transkribus.core.model.beans.JAXBPageTranscript in project TranskribusCore by Transkribus.
the class TrpXlsxBuilder method writeXlsxForDoc.
public static void writeXlsxForDoc(TrpDoc doc, boolean wordBased, File exportFile, Set<Integer> pageIndices, IProgressMonitor monitor, ExportCache cache) throws NoTagsException, Exception {
if (cache == null) {
throw new IllegalArgumentException("ExportCache must not be null.");
}
if (cache.getCustomTagMapForDoc().isEmpty()) {
logger.info("No tags to store -> Xlsx export cancelled");
throw new NoTagsException("No tags available to store into Xlsx");
}
List<TrpPage> pages = doc.getPages();
String exportPath = exportFile.getPath();
Set<String> selectedTags = cache.getOnlySelectedTagnames(ExportUtils.getOnlyWantedTagnames(CustomTagFactory.getRegisteredTagNames()));
int totalPages = pageIndices == null ? pages.size() : pageIndices.size();
if (monitor != null) {
monitor.beginTask("Exporting to Excel", totalPages);
}
wb = new XSSFWorkbook();
int c = 0;
for (int i = 0; i < pages.size(); ++i) {
if (pageIndices != null && !pageIndices.contains(i))
continue;
if (monitor != null) {
if (monitor.isCanceled()) {
throw new InterruptedException("Export was canceled by user");
// logger.debug("Xlsx export cancelled!");
// return;
}
monitor.subTask("Processing page " + (c + 1));
}
TrpPage page = pages.get(i);
// try to get previously loaded JAXB transcript
JAXBPageTranscript tr = null;
if (cache != null) {
tr = cache.getPageTranscriptAtIndex(i);
}
if (tr == null) {
TrpTranscriptMetadata md = page.getCurrentTranscript();
tr = new JAXBPageTranscript(md);
tr.build();
}
// old version
// TrpPage page = pages.get(i);
// TrpTranscriptMetadata md = page.getCurrentTranscript();
// JAXBPageTranscript tr = new JAXBPageTranscript(md);
// tr.build();
TrpPageType trpPage = tr.getPage();
logger.debug("writing xlsx for page " + (i + 1) + "/" + doc.getNPages());
List<TrpTextRegionType> textRegions = trpPage.getTextRegions(true);
for (int j = 0; j < textRegions.size(); ++j) {
TrpTextRegionType r = textRegions.get(j);
List<TextLineType> lines = r.getTextLine();
for (int k = 0; k < lines.size(); ++k) {
TrpTextLineType trpL = (TrpTextLineType) lines.get(k);
List<WordType> words = trpL.getWord();
if (wordBased) {
for (int l = 0; l < words.size(); ++l) {
TrpWordType w = (TrpWordType) words.get(l);
writeTagsForShapeElement(w, trpL.getUnicodeText(), String.valueOf(doc.getId()), String.valueOf(page.getPageNr()), r.getId(), trpL.getId(), w.getId(), selectedTags);
}
} else {
writeTagsForShapeElement(trpL, trpL.getUnicodeText(), String.valueOf(doc.getId()), String.valueOf(page.getPageNr()), r.getId(), trpL.getId(), "", selectedTags);
}
}
}
++c;
if (monitor != null) {
monitor.worked(c);
}
}
/*
* auto size the columns
*/
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
int numberOfCells = 0;
Iterator rowIterator = wb.getSheetAt(i).rowIterator();
/**
* Escape the header row *
*/
if (rowIterator.hasNext()) {
Row headerRow = (Row) rowIterator.next();
// get the number of cells in the header row
numberOfCells = headerRow.getPhysicalNumberOfCells();
for (int j = 0; j < numberOfCells; j++) {
wb.getSheetAt(i).autoSizeColumn(j);
}
}
}
FileOutputStream fOut;
try {
// means no tags at all
if (wb.getNumberOfSheets() == 0) {
throw new IOException("Sorry - No tags available for export");
}
fOut = new FileOutputStream(exportPath);
wb.write(fOut);
fOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}
logger.info("wrote xlsx to: " + exportPath);
}
use of eu.transkribus.core.model.beans.JAXBPageTranscript in project TranskribusCore by Transkribus.
the class DocxBuilder method writeDocxForDoc.
public static void writeDocxForDoc(TrpDoc doc, boolean wordBased, boolean writeTags, boolean doBlackeningSensibleData, File file, Set<Integer> pageIndices, IProgressMonitor monitor, boolean createTitle, boolean markUnclear, boolean expandAbbreviations, boolean replaceAbbrevs, boolean keepLineBreaks, boolean showSuppliedInBrackets, boolean ignoreSuppliedTags, ExportCache cache) throws JAXBException, IOException, Docx4JException, InterruptedException {
// ch.qos.logback.classic.Logger root = logger.getClass().get(ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
((ch.qos.logback.classic.Logger) logger).setLevel(ch.qos.logback.classic.Level.DEBUG);
exportTags = writeTags;
doBlackening = doBlackeningSensibleData;
tagnames = cache.getOnlySelectedTagnames(ExportUtils.getOnlyWantedTagnames(CustomTagFactory.getRegisteredTagNames()));
markUnclearWords = markUnclear;
expandAbbrevs = expandAbbreviations;
preserveLineBreaks = keepLineBreaks;
substituteAbbrevs = replaceAbbrevs;
showSuppliedWithBrackets = showSuppliedInBrackets;
ignoreSupplied = ignoreSuppliedTags;
/*
* get all names of tags
*/
// tagnames = CustomTagFactory.getRegisteredTagNames();
// main document part
wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
List<TrpPage> pages = doc.getPages();
int totalPages = pageIndices == null ? pages.size() : pageIndices.size();
if (monitor != null) {
monitor.beginTask("Exporting to docx", totalPages);
}
int c = 0;
boolean atLeastOnePageWritten = false;
// can be used as page break every time we need one
Br objBr = new Br();
objBr.setType(STBrType.PAGE);
P pageBreakP = factory.createP();
pageBreakP.getContent().add(objBr);
for (int i = 0; i < pages.size(); ++i) {
if (pageIndices != null && !pageIndices.contains(i))
continue;
if (!atLeastOnePageWritten && createTitle) {
addTitlePage(doc, mdp);
// add page break
mdp.addObject(pageBreakP);
}
if (monitor != null) {
if (monitor.isCanceled()) {
throw new InterruptedException("Export canceled by the user");
// logger.debug("docx export cancelled!");
// return;
}
monitor.subTask("Processing page " + (c + 1));
}
// TrpPage page = pages.get(i);
// TrpTranscriptMetadata md = page.getCurrentTranscript();
// JAXBPageTranscript tr = new JAXBPageTranscript(md);
// tr.build();
JAXBPageTranscript tr = null;
if (cache != null) {
tr = cache.getPageTranscriptAtIndex(i);
}
if (tr == null) {
TrpPage page = pages.get(i);
TrpTranscriptMetadata md = page.getCurrentTranscript();
// md.getStatus().equals("Done");
tr = new JAXBPageTranscript(md);
tr.build();
}
TrpPageType trpPage = tr.getPage();
logger.debug("writing docx for the page " + (i + 1) + "/" + doc.getNPages());
writeDocxForTranscriptWithTables(mdp, trpPage, wordBased, preserveLineBreaks);
atLeastOnePageWritten = true;
++c;
if (monitor != null) {
monitor.worked(c);
}
}
P p = factory.createP();
mdp.getContent().add(p);
addComplexField(p, " INDEX \\e \"", "\" \\c \"1\" \\z \"1031\"");
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
// write tags at end of last page
if (false) {
// RtfText headline = RtfText.text("Person names in this document (amount of found persons: " + persons.size() + ")", "\n");
logger.debug("export tags ");
boolean firstExport = true;
// tagnames = all user choosen tags via export dialog
for (String currTagname : tagnames) {
// logger.debug("curr tagname " + currTagname);
// get all custom tags with currTagname and text
HashMap<CustomTag, String> allTagsOfThisTagname = cache.getTags(currTagname);
// one paragraph for each tagname
org.docx4j.wml.P p4Tag = factory.createP();
if (allTagsOfThisTagname.size() > 0 && !currTagname.equals("textStyle") && !currTagname.equals("gap") && !currTagname.equals("comment")) {
// new page if tag export starts
if (firstExport) {
// Br objBr = new Br();
// objBr.setType(STBrType.PAGE);
p4Tag.getContent().add(objBr);
firstExport = false;
}
// logger.debug("allTagsOfThisTagname " + allTagsOfThisTagname.size());
// one run for headline and thanfor each entry
org.docx4j.wml.Text t = factory.createText();
t.setValue(currTagname + " tags in this document: " + allTagsOfThisTagname.size());
t.setSpace("preserve");
org.docx4j.wml.R run = factory.createR();
run.getContent().add(t);
org.docx4j.wml.RPr rpr = factory.createRPr();
org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
b.setVal(true);
U u = factory.createU();
u.setVal(UnderlineEnumeration.SINGLE);
rpr.setB(b);
rpr.setU(u);
run.setRPr(rpr);
// this Br element is used break the current and go for next line
Br br = factory.createBr();
run.getContent().add(br);
p4Tag.getContent().add(run);
// ArrayList<RtfText> tagTexts = new ArrayList<RtfText>();
Collection<String> valueSet = allTagsOfThisTagname.values();
int l = 0;
for (String currEntry : valueSet) {
org.docx4j.wml.R currRun = factory.createR();
org.docx4j.wml.Text currText = factory.createText();
currText.setValue(currEntry);
currText.setSpace("preserve");
currRun.getContent().add(currText);
// reuse linebreak
currRun.getContent().add(br);
p4Tag.getContent().add(currRun);
}
}
mdp.getContent().add(p4Tag);
}
}
// finally save the file
wordMLPackage.save(file);
logger.info("Saved " + file.getAbsolutePath());
}
use of eu.transkribus.core.model.beans.JAXBPageTranscript in project TranskribusCore by Transkribus.
the class TrpXlsxTableBuilder method writeXlsxForTables.
public static void writeXlsxForTables(TrpDoc doc, File exportFile, Set<Integer> pageIndices, IProgressMonitor monitor, ExportCache cache) throws NoTablesException, IOException, InterruptedException {
// TrpTableRegionType is contained in the regions too
List<TrpPage> pages = doc.getPages();
String exportPath = exportFile.getPath();
int totalPages = pageIndices == null ? pages.size() : pageIndices.size();
if (monitor != null) {
monitor.beginTask("Exporting tables to Excel", totalPages);
}
wb = new XSSFWorkbook();
int c = 0;
int tableId = 0;
for (int i = 0; i < pages.size(); ++i) {
if (pageIndices != null && !pageIndices.contains(i))
continue;
if (monitor != null) {
if (monitor.isCanceled()) {
throw new InterruptedException("Export was canceled by user");
// logger.debug("Xlsx export cancelled!");
// return;
}
monitor.subTask("Processing page " + (c + 1));
}
TrpPage page = pages.get(i);
// try to get previously loaded JAXB transcript
JAXBPageTranscript tr = null;
if (cache != null) {
tr = cache.getPageTranscriptAtIndex(i);
}
if (tr == null) {
TrpTranscriptMetadata md = page.getCurrentTranscript();
tr = new JAXBPageTranscript(md);
tr.build();
}
TrpPageType trpPage = tr.getPage();
List<TrpRegionType> regions = trpPage.getRegions();
for (int j = 0; j < regions.size(); ++j) {
TrpRegionType r = regions.get(j);
if (r instanceof TrpTableRegionType) {
tableId++;
logger.debug("is table");
TrpTableRegionType table = (TrpTableRegionType) r;
int cols = table.getNCols();
int rows = table.getNRows();
// double maxX = PageXmlUtils.buildPolygon(table.getCoords().getPoints()).getBounds().getMaxX();
// double minX = PageXmlUtils.buildPolygon(table.getCoords().getPoints()).getBounds().getMinX();
// int tablesize = (int) (maxX - minX);
List<List<TrpTableCellType>> allRowCells = new ArrayList<List<TrpTableCellType>>();
for (int k = 0; k < rows; k++) {
allRowCells.add(table.getRowCells(k));
}
List<HashMap<Integer, TrpTableCellType>> allRows = new ArrayList<HashMap<Integer, TrpTableCellType>>();
HashMap<Integer, TrpTableCellType> nextRowMap = new HashMap<Integer, TrpTableCellType>();
for (List<TrpTableCellType> rowCells : allRowCells) {
HashMap<Integer, TrpTableCellType> currRowMap = new HashMap<Integer, TrpTableCellType>();
/*
* fill up all cells which are not set in TRP (needed for vertical cell merge)
* the nextRowMap contains already all cells which span vertically with the cells above - means they got merged
* in the table but have to be considered here
*/
currRowMap.putAll(nextRowMap);
nextRowMap.clear();
for (TrpTableCellType cell : rowCells) {
// logger.debug("table cell text " + cell.getUnicodeTextFromLines());
currRowMap.put(cell.getCol(), cell);
// only one row or col span is considered -> FIXME: do it for all spans, but may happens never?
if (cell.getRowSpan() > 1) {
nextRowMap.put(cell.getCol(), null);
}
if (cell.getColSpan() > 1) {
currRowMap.put(cell.getCol() + 1, null);
}
}
allRows.add(currRowMap);
}
createTable(rows, cols, allRows, tableId);
}
logger.debug("writing xlsx for page " + (i + 1) + "/" + doc.getNPages());
++c;
if (monitor != null) {
monitor.worked(c);
}
}
}
/*
* auto size the columns
*/
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
int numberOfCells = 0;
Iterator rowIterator = wb.getSheetAt(i).rowIterator();
/**
* Escape the header row *
*/
if (rowIterator.hasNext()) {
Row headerRow = (Row) rowIterator.next();
// get the number of cells in the header row
numberOfCells = headerRow.getPhysicalNumberOfCells();
for (int j = 0; j < numberOfCells; j++) {
wb.getSheetAt(i).autoSizeColumn(j, true);
}
}
}
FileOutputStream fOut;
try {
// means no tables at all
if (wb.getNumberOfSheets() == 0) {
throw new NoTablesException("Sorry - No tables available for export");
}
fOut = new FileOutputStream(exportPath);
wb.write(fOut);
fOut.close();
} catch (IOException e) {
if (!(e instanceof NoTablesException)) {
logger.error(e.getMessage(), e);
}
throw e;
}
logger.info("wrote xlsx to: " + exportPath);
}
Aggregations