10
10
import java .util .*;
11
11
import java .util .logging .Level ;
12
12
import java .util .logging .Logger ;
13
+ import java .util .stream .Collectors ;
13
14
import javax .xml .parsers .*;
14
15
import javax .xml .transform .OutputKeys ;
15
16
import javax .xml .transform .Result ;
@@ -277,7 +278,8 @@ public boolean process() {
277
278
isAddMathAsText = xsltConverter .hasParamAddMathAsText () && isMathExists ;
278
279
isAddMathAsAttachment = xsltConverter .hasParamAddMathAsAttachment ();
279
280
280
- isApplyAutolayoutAlgorithm = xsltConverter .isApplyAutolayoutAlgorithm ();
281
+ isApplyAutolayoutAlgorithm = xsltConverter .isApplyAutolayoutAlgorithm ();
282
+
281
283
282
284
if (isSyntaxHighlight ) {
283
285
xsltParams .put ("syntax-highlight" , "true" );
@@ -1173,6 +1175,8 @@ private void readEncryptionParameters(File fEncryptionParameters) {
1173
1175
1174
1176
private void setTablesWidths (fontConfig fontcfg , XSLTconverter xsltConverter , File pdf ) {
1175
1177
1178
+ int TABLE_CELLS_COUNT_MAX = 30000 ;
1179
+
1176
1180
String methodName = getClass ().getSimpleName () + "." + (new Object (){}.getClass ().getEnclosingMethod ().getName ());
1177
1181
Profiler .addMethodCall (methodName );
1178
1182
long startMethodTime = System .currentTimeMillis ();
@@ -1196,7 +1200,7 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi
1196
1200
SourceXMLDocument sourceXMLDocumentTablesOnly = new SourceXMLDocument (xmlTablesOnly );
1197
1201
1198
1202
int countTableCells = sourceXMLDocumentTablesOnly .getCountTableCells ();
1199
- if (countTableCells < 30000 ) {
1203
+ if (countTableCells < TABLE_CELLS_COUNT_MAX ) {
1200
1204
// transform XML to XSL-FO (XML .fo file)
1201
1205
xsltConverter .transform (sourceXMLDocumentTablesOnly , false );
1202
1206
@@ -1218,46 +1222,79 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi
1218
1222
1219
1223
} else { // for large tables, or large number of tables
1220
1224
1221
- List <String > tablesIds = sourceXMLDocumentTablesOnly .readElementsIds ("//*[local-name() = 'table' or local-name() = 'dl']" );
1222
-
1223
1225
List <String > xmlTablesIF = new ArrayList <>();
1224
- // process each table separatery for memory consumption optimization
1225
- int tableCounter = 0 ;
1226
- int tableCount = tablesIds .size ();
1227
- for (String tableId : tablesIds ) {
1228
- tableCounter ++;
1229
- logger .info ("[INFO] Generation of XSL-FO (" + tableCounter + "/" + tableCount + ") with information about the table widths with id='" + tableId + "'..." );
1230
1226
1231
- // process table with id=tableId only
1232
- xsltConverter .setParam ("table_only_with_id" , tableId );
1227
+ Map <String ,Integer > tablesCellsCountMap = sourceXMLDocumentTablesOnly .getTablesCellsCountMap ();
1228
+
1229
+ int portion = 1 ;
1230
+ while (!tablesCellsCountMap .isEmpty ()) {
1231
+ int totalCells = 0 ;
1232
+ List <String > tablesProcessed = new ArrayList <>();
1233
+
1234
+ Iterator <Map .Entry <String , Integer >> iterator = tablesCellsCountMap .entrySet ().iterator ();
1235
+ while (iterator .hasNext () && totalCells < TABLE_CELLS_COUNT_MAX ) {
1236
+ Map .Entry <String , Integer > entry = iterator .next ();
1237
+ if (totalCells == 0 || totalCells + entry .getValue () < TABLE_CELLS_COUNT_MAX ) {
1238
+ totalCells += entry .getValue ();
1239
+ tablesProcessed .add (entry .getKey ());
1240
+ }
1241
+ }
1242
+
1243
+ /*for (Map.Entry<String, Integer> entry : tablesCellsCountMap.entrySet()) {
1244
+ else {
1245
+ break;
1246
+ }
1247
+ }*/
1248
+ logger .info ("[INFO] Generation of XSL-FO (portion " + portion + ") with information about the table widths..." );
1249
+
1250
+ // "table1 table2 table3 " (with space at the end)
1251
+ String tableIds = tablesProcessed .stream ().collect (Collectors .joining (" " )) + " " ;
1252
+ // call XSLT and pass the tables ids
1253
+
1254
+ // process table with ids=tableIds only
1255
+ xsltConverter .setParam ("table_only_with_ids" , tableIds );
1233
1256
1234
1257
// transform XML to XSL-FO (XML .fo file)
1235
1258
xsltConverter .transform (sourceXMLDocumentTablesOnly , false );
1236
1259
1237
1260
String xmlFO = sourceXMLDocumentTablesOnly .getXMLFO ();
1238
1261
1239
1262
//debug
1240
- debugSaveXML (xmlFO , pdf .getAbsolutePath () + "." + tableId + ".fo.tables.xml" );
1263
+ debugSaveXML (xmlFO , pdf .getAbsolutePath () + ".portion_ " + portion + ".fo.tables.xml" );
1241
1264
1242
- fontcfg .outputFontManifestLog (Paths .get (pdf .getAbsolutePath () + "." + tableId + ".tables.fontmanifest.log.txt" ));
1265
+ fontcfg .outputFontManifestLog (Paths .get (pdf .getAbsolutePath () + ".portion_ " + portion + ".tables.fontmanifest.log.txt" ));
1243
1266
1244
1267
fontcfg .setSourceDocumentFontList (sourceXMLDocumentTablesOnly .getDocumentFonts ());
1245
1268
1246
1269
Source sourceFO = new StreamSource (new StringReader (xmlFO ));
1247
1270
1248
- logger .info ("[INFO] Generation of Intermediate Format (" + tableCounter + "/" + tableCount + ") with information about the table's widths with id=' " + tableId + "' ..." );
1249
- String xmlIF = generateFOPIntermediateFormat (sourceFO , fontcfg .getConfig (), pdf , true , "." + tableId + ".tables" );
1271
+ logger .info ("[INFO] Generation of Intermediate Format with information about the table's widths (portion " + portion + ") ..." );
1272
+ String xmlIF = generateFOPIntermediateFormat (sourceFO , fontcfg .getConfig (), pdf , true , ".portion_ " + portion + ".tables" );
1250
1273
1251
1274
xmlTableIF = createTableIF (xmlIF );
1252
1275
1253
- debugSaveXML (xmlTableIF , pdf .getAbsolutePath () + "." + tableId + ".tables.xml" );
1276
+ debugSaveXML (xmlTableIF , pdf .getAbsolutePath () + ".portion_ " + portion + ".tables.xml" );
1254
1277
1255
1278
xmlTableIF = tableWidthsCleanup (xmlTableIF );
1256
1279
1257
1280
xmlTablesIF .add (xmlTableIF );
1281
+
1282
+ // remove processed tables
1283
+ tablesCellsCountMap .keySet ().removeAll (tablesProcessed );
1284
+ portion ++;
1258
1285
}
1286
+
1287
+ /*List<String> tablesIds = sourceXMLDocumentTablesOnly.readElementsIds("//*[local-name() = 'table' or local-name() = 'dl']");
1288
+ // process each table separatery for memory consumption optimization
1289
+ int tableCounter = 0;
1290
+ int tableCount = tablesIds.size();
1291
+ for (String tableId : tablesIds) {
1292
+ tableCounter++;
1293
+ logger.info("[INFO] Generation of XSL-FO (" + tableCounter + "/" + tableCount + ") with information about the table widths with id='" + tableId + "'...");
1294
+ }*/
1259
1295
xmlTableIF = tablesWidthsUnion (xmlTablesIF );
1260
1296
xsltConverter .setParam ("table_only_with_id" , "" ); // further process all tables
1297
+ xsltConverter .setParam ("table_only_with_ids" , "" ); // further process all tables
1261
1298
}
1262
1299
1263
1300
debugSaveXML (xmlTableIF , pdf .getAbsolutePath () + ".tables.xml" );
@@ -1330,11 +1367,17 @@ private void saveDebugFO(String debugXSLFO) {
1330
1367
}
1331
1368
1332
1369
private String tableWidthsCleanup (String table ) {
1333
- int startPos = table .indexOf ("<table " );
1370
+ try {
1371
+ table = applyXSLT ("table_if_clean.xsl" , table , false );
1372
+ } catch (Exception ex ) {
1373
+ logger .severe ("Can't simplify the tables width information XML." );
1374
+ ex .printStackTrace ();
1375
+ }
1376
+ /*int startPos = table.indexOf("<table ");
1334
1377
int endPos = table.indexOf("</tables>");
1335
1378
table = table.substring(startPos, endPos);
1336
1379
int startPosTbody = table.indexOf("<tbody>");
1337
- table = table .substring (0 ,startPosTbody ) + "</table>" ;
1380
+ table = table.substring(0,startPosTbody) + "</table>";*/
1338
1381
return table ;
1339
1382
}
1340
1383
@@ -1344,6 +1387,9 @@ private String tablesWidthsUnion(List<String> tables) {
1344
1387
sbTablesIF .append ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?><tables>" );
1345
1388
}
1346
1389
for (String itemTableIF : tables ) {
1390
+ int startPos = itemTableIF .indexOf ("<table " );
1391
+ int endPos = itemTableIF .indexOf ("</tables>" );
1392
+ itemTableIF = itemTableIF .substring (startPos , endPos );
1347
1393
sbTablesIF .append (itemTableIF );
1348
1394
}
1349
1395
if (!tables .isEmpty ()) {
0 commit comments