Skip to content

Commit 8dd3123

Browse files
code refactoring, metanorma/iso-2533#20
1 parent a580dae commit 8dd3123

File tree

3 files changed

+79
-30
lines changed

3 files changed

+79
-30
lines changed

src/main/java/org/metanorma/fop/PDFGenerator.java

+46-21
Original file line numberDiff line numberDiff line change
@@ -1188,48 +1188,73 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi
11881188
debugSaveXML(xmlTablesOnly, pdf.getAbsolutePath() + ".tablesonly.xml");
11891189

11901190
SourceXMLDocument sourceXMLDocumentTablesOnly = new SourceXMLDocument(xmlTablesOnly);
1191-
List<String> tablesIds = sourceXMLDocumentTablesOnly.readElementsIds("//*[local-name() = 'table' or local-name() = 'dl']");
1192-
1193-
List<String> xmlTablesIF = new ArrayList<>();
1194-
// process each table separatery for memory consumption optimization
1195-
for (String tableId: tablesIds) {
1196-
1197-
logger.info("[INFO] Generation of XSL-FO with information about the table widths with id " + tableId + " ...");
1198-
1199-
// process table with id=tableId only
1200-
xsltConverter.setParam("table_only_with_id", tableId);
12011191

1192+
int countTableCells = sourceXMLDocumentTablesOnly.getCountTableCells();
1193+
if (countTableCells < 30000) {
12021194
// transform XML to XSL-FO (XML .fo file)
12031195
xsltConverter.transform(sourceXMLDocumentTablesOnly, false);
12041196

12051197
String xmlFO = sourceXMLDocumentTablesOnly.getXMLFO();
12061198

12071199
//debug
1208-
debugSaveXML(xmlFO, pdf.getAbsolutePath() + "." + tableId + ".fo.tables.xml");
1200+
debugSaveXML(xmlFO, pdf.getAbsolutePath() + ".fo.tables.xml");
12091201

1210-
fontcfg.outputFontManifestLog(Paths.get(pdf.getAbsolutePath() + "." + tableId + ".tables.fontmanifest.log.txt"));
1202+
fontcfg.outputFontManifestLog(Paths.get(pdf.getAbsolutePath() + ".tables.fontmanifest.log.txt"));
12111203

12121204
fontcfg.setSourceDocumentFontList(sourceXMLDocumentTablesOnly.getDocumentFonts());
12131205

12141206
Source sourceFO = new StreamSource(new StringReader(xmlFO));
12151207

1216-
logger.info("[INFO] Generation of Intermediate Format with information about the table's widths with id " + tableId + " ...");
1217-
String xmlIF = generateFOPIntermediateFormat(sourceFO, fontcfg.getConfig(), pdf, true, "." + tableId + ".tables");
1208+
logger.info("[INFO] Generation of Intermediate Format with information about the table's widths ...");
1209+
String xmlIF = generateFOPIntermediateFormat(sourceFO, fontcfg.getConfig(), pdf, true, ".tables");
12181210

12191211
xmlTableIF = createTableIF(xmlIF);
12201212

1221-
debugSaveXML(xmlTableIF, pdf.getAbsolutePath() + "." + tableId + ".tables.xml");
1213+
} else { // for large tables, or large number of tables
12221214

1223-
xmlTableIF = tableWidthsCleanup(xmlTableIF);
1215+
List<String> tablesIds = sourceXMLDocumentTablesOnly.readElementsIds("//*[local-name() = 'table' or local-name() = 'dl']");
12241216

1225-
xmlTablesIF.add(xmlTableIF);
1226-
}
1217+
List<String> xmlTablesIF = new ArrayList<>();
1218+
// process each table separatery for memory consumption optimization
1219+
int tableCounter = 0;
1220+
int tableCount = tablesIds.size();
1221+
for (String tableId : tablesIds) {
1222+
tableCounter++;
1223+
logger.info("[INFO] Generation of XSL-FO (" + tableCounter + "/" + tableCount + ") with information about the table widths with id='" + tableId + "'...");
12271224

1225+
// process table with id=tableId only
1226+
xsltConverter.setParam("table_only_with_id", tableId);
12281227

1229-
xmlTableIF = tablesWidthsUnion(xmlTablesIF);
1230-
debugSaveXML(xmlTableIF, pdf.getAbsolutePath() + ".tables.xml");
1228+
// transform XML to XSL-FO (XML .fo file)
1229+
xsltConverter.transform(sourceXMLDocumentTablesOnly, false);
1230+
1231+
String xmlFO = sourceXMLDocumentTablesOnly.getXMLFO();
1232+
1233+
//debug
1234+
debugSaveXML(xmlFO, pdf.getAbsolutePath() + "." + tableId + ".fo.tables.xml");
1235+
1236+
fontcfg.outputFontManifestLog(Paths.get(pdf.getAbsolutePath() + "." + tableId + ".tables.fontmanifest.log.txt"));
1237+
1238+
fontcfg.setSourceDocumentFontList(sourceXMLDocumentTablesOnly.getDocumentFonts());
12311239

1232-
xsltConverter.setParam("table_only_with_id", ""); // further process all tables
1240+
Source sourceFO = new StreamSource(new StringReader(xmlFO));
1241+
1242+
logger.info("[INFO] Generation of Intermediate Format (" + tableCounter + "/" + tableCount + ") with information about the table's widths with id='" + tableId + "'...");
1243+
String xmlIF = generateFOPIntermediateFormat(sourceFO, fontcfg.getConfig(), pdf, true, "." + tableId + ".tables");
1244+
1245+
xmlTableIF = createTableIF(xmlIF);
1246+
1247+
debugSaveXML(xmlTableIF, pdf.getAbsolutePath() + "." + tableId + ".tables.xml");
1248+
1249+
xmlTableIF = tableWidthsCleanup(xmlTableIF);
1250+
1251+
xmlTablesIF.add(xmlTableIF);
1252+
}
1253+
xmlTableIF = tablesWidthsUnion(xmlTablesIF);
1254+
xsltConverter.setParam("table_only_with_id", ""); // further process all tables
1255+
}
1256+
1257+
debugSaveXML(xmlTableIF, pdf.getAbsolutePath() + ".tables.xml");
12331258

12341259
xsltConverter.setParam("table_if", "false");
12351260
logger.info("[INFO] Generated successfully!");

src/main/java/org/metanorma/fop/SourceXMLDocument.java

+31-9
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,13 @@ public SourceXMLDocument(File fXML) {
7575
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
7676
InputStream xmlstream = new FileInputStream(fXML);
7777
sourceXML = dBuilder.parse(xmlstream);
78-
79-
String element_review = readValue("//*[local-name() = 'review'][1]");
80-
this.hasAnnotations = element_review.length() != 0;
81-
String element_table = readValue("//*[local-name() = 'table' or local-name() = 'dl'][1]");
82-
this.hasTables = element_table.length() != 0;
83-
String element_math = readValue("//*[local-name() = 'math'][1]");
84-
this.hasMath = element_math.length() != 0;
78+
readMetaInformation();
8579
} catch (Exception ex) {
8680
logger.severe("Can't read source XML.");
8781
ex.printStackTrace();
8882
}
8983
}
90-
84+
9185
public SourceXMLDocument(String strXML) {
9286
try {
9387
this.sourceXMLstr = strXML;
@@ -100,7 +94,17 @@ public SourceXMLDocument(String strXML) {
10094
ex.printStackTrace();
10195
}
10296
}
103-
97+
98+
private void readMetaInformation() {
99+
String element_review = readValue("//*[local-name() = 'review'][1]");
100+
this.hasAnnotations = element_review.length() != 0;
101+
String element_table = readValue("//*[local-name() = 'table' or local-name() = 'dl'][1]");
102+
this.hasTables = element_table.length() != 0;
103+
String element_math = readValue("//*[local-name() = 'math'][1]");
104+
this.hasMath = element_math.length() != 0;
105+
}
106+
107+
104108
public StreamSource getStreamSource() {
105109
if (sourceXMLstr.isEmpty()) {
106110
try {
@@ -410,6 +414,19 @@ private String readValue(String xpath) {
410414
return value;
411415
}
412416

417+
private int readTableCellsCount(){
418+
int count = 0;
419+
try {
420+
XPath xPath = XPathFactory.newInstance().newXPath();
421+
XPathExpression query = xPath.compile("//*[local-name() = 'td' or local-name() = 'th' or local-name() = 'dt' or local-name() = 'dd']");
422+
NodeList nodes = (NodeList)query.evaluate(sourceXML, XPathConstants.NODESET);
423+
count = nodes.getLength();
424+
} catch (Exception ex) {
425+
logger.severe(ex.toString());
426+
}
427+
return count;
428+
}
429+
413430
public List<String> readElementsIds(String xpath) {
414431
List<String> values = new ArrayList<>();
415432
try {
@@ -442,6 +459,11 @@ public boolean hasMath() {
442459
return hasMath;
443460
}
444461

462+
public int getCountTableCells() {
463+
int countTableCells = readTableCellsCount();
464+
return countTableCells;
465+
}
466+
445467
public void flushResources() {
446468
sourceXML = null;
447469
sourceXMLstr = "";

src/main/resources/tables_only.xsl

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110

111111
<xsl:template match="*[local-name() = 'table'][@type = 'sourcecode']" priority="2"/>
112112

113+
<xsl:template match="*[local-name() = 'image'][not(ancestor::*[local-name() = 'table']) and not(ancestor::*[local-name() = 'dl'])]" priority="2"/>
114+
113115
<xsl:template match="@*|node()" mode="simple_td">
114116
<xsl:copy>
115117
<xsl:apply-templates select="@*|node()" mode="simple_td"/>

0 commit comments

Comments
 (0)