7
7
import java .nio .file .Files ;
8
8
import java .nio .file .Path ;
9
9
import java .nio .file .Paths ;
10
- import java .util .ArrayList ;
11
- import java .util .Arrays ;
12
- import java .util .HashMap ;
13
- import java .util .Map ;
14
- import java .util .Properties ;
10
+ import java .util .*;
15
11
import java .util .logging .Level ;
16
12
import java .util .logging .Logger ;
17
13
import javax .xml .parsers .*;
@@ -1193,24 +1189,70 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi
1193
1189
1194
1190
SourceXMLDocument sourceXMLDocumentTablesOnly = new SourceXMLDocument (xmlTablesOnly );
1195
1191
1196
- // transform XML to XSL-FO (XML .fo file)
1197
- xsltConverter .transform (sourceXMLDocumentTablesOnly , false );
1192
+ int countTableCells = sourceXMLDocumentTablesOnly .getCountTableCells ();
1193
+ if (countTableCells < 30000 ) {
1194
+ // transform XML to XSL-FO (XML .fo file)
1195
+ xsltConverter .transform (sourceXMLDocumentTablesOnly , false );
1198
1196
1199
- String xmlFO = sourceXMLDocumentTablesOnly .getXMLFO ();
1197
+ String xmlFO = sourceXMLDocumentTablesOnly .getXMLFO ();
1200
1198
1201
- //debug
1202
- debugSaveXML (xmlFO , pdf .getAbsolutePath () + ".fo.tables.xml" );
1199
+ //debug
1200
+ debugSaveXML (xmlFO , pdf .getAbsolutePath () + ".fo.tables.xml" );
1203
1201
1204
- fontcfg .outputFontManifestLog (Paths .get (pdf .getAbsolutePath () + ".tables.fontmanifest.log.txt" ));
1202
+ fontcfg .outputFontManifestLog (Paths .get (pdf .getAbsolutePath () + ".tables.fontmanifest.log.txt" ));
1205
1203
1206
- fontcfg .setSourceDocumentFontList (sourceXMLDocumentTablesOnly .getDocumentFonts ());
1204
+ fontcfg .setSourceDocumentFontList (sourceXMLDocumentTablesOnly .getDocumentFonts ());
1207
1205
1208
- Source sourceFO = new StreamSource (new StringReader (xmlFO ));
1206
+ Source sourceFO = new StreamSource (new StringReader (xmlFO ));
1209
1207
1210
- logger .info ("[INFO] Generation of Intermediate Format with information about the table's widths ..." );
1211
- String xmlIF = generateFOPIntermediateFormat (sourceFO , fontcfg .getConfig (), pdf , true , ".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" );
1212
1210
1213
- xmlTableIF = createTableIF (xmlIF );
1211
+ xmlTableIF = createTableIF (xmlIF );
1212
+
1213
+ } else { // for large tables, or large number of tables
1214
+
1215
+ List <String > tablesIds = sourceXMLDocumentTablesOnly .readElementsIds ("//*[local-name() = 'table' or local-name() = 'dl']" );
1216
+
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 + "'..." );
1224
+
1225
+ // process table with id=tableId only
1226
+ xsltConverter .setParam ("table_only_with_id" , tableId );
1227
+
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 ());
1239
+
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
+ }
1214
1256
1215
1257
debugSaveXML (xmlTableIF , pdf .getAbsolutePath () + ".tables.xml" );
1216
1258
@@ -1261,6 +1303,7 @@ private void debugSaveXML(String xmlString, String pathTo) {
1261
1303
}
1262
1304
}
1263
1305
1306
+
1264
1307
private int getIFPageCount (String xmlIF ) {
1265
1308
int pagecount = 0 ;
1266
1309
if (xmlIF != null ) {
@@ -1280,4 +1323,27 @@ private void saveDebugFO(String debugXSLFO) {
1280
1323
}
1281
1324
}
1282
1325
1326
+ private String tableWidthsCleanup (String table ) {
1327
+ int startPos = table .indexOf ("<table " );
1328
+ int endPos = table .indexOf ("</tables>" );
1329
+ table = table .substring (startPos , endPos );
1330
+ int startPosTbody = table .indexOf ("<tbody>" );
1331
+ table = table .substring (0 ,startPosTbody ) + "</table>" ;
1332
+ return table ;
1333
+ }
1334
+
1335
+ private String tablesWidthsUnion (List <String > tables ) {
1336
+ StringBuilder sbTablesIF = new StringBuilder ();
1337
+ if (!tables .isEmpty ()) {
1338
+ sbTablesIF .append ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?><tables>" );
1339
+ }
1340
+ for (String itemTableIF : tables ) {
1341
+ sbTablesIF .append (itemTableIF );
1342
+ }
1343
+ if (!tables .isEmpty ()) {
1344
+ sbTablesIF .append ("</tables>" );
1345
+ }
1346
+ return sbTablesIF .toString ();
1347
+ }
1348
+
1283
1349
}
0 commit comments