From 9ad381ff5d1fa84444b9c59aabef88109c6fb965 Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Thu, 8 Dec 2022 22:51:48 +0300 Subject: [PATCH 01/10] log refactoring, metanorma/mn-native-pdf#393 --- Makefile | 2 +- README.adoc | 10 +++---- pom.xml | 2 +- .../java/org/metanorma/fop/PDFGenerator.java | 29 +++++++------------ src/main/java/org/metanorma/fop/Util.java | 12 +++++++- .../java/org/metanorma/fop/XSLTconverter.java | 11 +------ src/main/java/org/metanorma/fop/mn2pdf.java | 6 +++- 7 files changed, 35 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index bd1784b0..a6e91811 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ SHELL ?= /bin/bash endif #JAR_VERSION := $(shell mvn -q -Dexec.executable="echo" -Dexec.args='$${project.version}' --non-recursive exec:exec -DforceStdout) -JAR_VERSION := 1.54 +JAR_VERSION := 1.55 JAR_FILE := mn2pdf-$(JAR_VERSION).jar all: target/$(JAR_FILE) diff --git a/README.adoc b/README.adoc index 33028c5e..2dea0cc7 100644 --- a/README.adoc +++ b/README.adoc @@ -17,14 +17,14 @@ You will need the Java Development Kit (JDK) version 8, Update 241 (8u241) or hi [source,sh] ---- -java -Xss5m -Xmx2048m -jar target/mn2pdf-1.54.jar --xml-file --xsl-file --pdf-file [--syntax-highlight] +java -Xss5m -Xmx2048m -jar target/mn2pdf-1.55.jar --xml-file --xsl-file --pdf-file [--syntax-highlight] ---- e.g. [source,sh] ---- -java -Xss5m -Xmx2048m -jar target/mn2pdf-1.54.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf +java -Xss5m -Xmx2048m -jar target/mn2pdf-1.55.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf ---- === PDF encryption features @@ -100,7 +100,7 @@ Update version in `pom.xml`, e.g.: ---- org.metanorma.fop mn2pdf -1.54 +1.55 Metanorma XML to PDF converter ---- @@ -111,8 +111,8 @@ Tag the same version in Git: [source,xml] ---- -git tag v1.54 -git push origin v1.54 +git tag v1.55 +git push origin v1.55 ---- Then the corresponding GitHub release will be automatically created at: diff --git a/pom.xml b/pom.xml index 431eef3b..4d3e2463 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.metanorma.fop mn2pdf - 1.54 + 1.55 Metanorma XML to PDF converter jar https://www.metanorma.org diff --git a/src/main/java/org/metanorma/fop/PDFGenerator.java b/src/main/java/org/metanorma/fop/PDFGenerator.java index 788c479b..c757c90d 100644 --- a/src/main/java/org/metanorma/fop/PDFGenerator.java +++ b/src/main/java/org/metanorma/fop/PDFGenerator.java @@ -600,7 +600,7 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOExceptio } } - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); } private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontcfg, Properties xslparams, XSLTconverter xsltConverter, File pdf) throws Exception, IOException, FOPException, SAXException, TransformerException, ParserConfigurationException { @@ -637,7 +637,7 @@ private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontc src = new StreamSource(new StringReader(xmlFO)); } - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); return src; } @@ -700,8 +700,8 @@ private String generateFOPIntermediateFormat(Source src, File fontConfig, File p } finally { out.close(); } - - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime); + + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); return xmlIF; } @@ -734,7 +734,7 @@ private void createIndexFile(String indexxmlFilePath, String intermediateXML, Fi logger.severe("Can't save index.xml into temporary folder"); ex.printStackTrace(); } - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); } private String addHiddenMath(String sourceXML) { @@ -746,7 +746,7 @@ private String addHiddenMath(String sourceXML) { InputSource inputSource = new InputSource( new StringReader(sourceXML)); saxParser.parse(inputSource, fopIFHiddenMathHandler); String result = fopIFHiddenMathHandler.getResultedXML(); - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); return result; } catch (Exception ex) { @@ -765,7 +765,7 @@ private String flatIFforXFDF(String sourceXML) { InputSource inputSource = new InputSource( new StringReader(sourceXML)); saxParser.parse(inputSource, fopIFFlatHandler); String result = fopIFFlatHandler.getResultedXML(); - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); return result; } catch (Exception ex) { @@ -784,7 +784,7 @@ private String createTableIF(String intermediateXML) { logger.severe("Can't generate information about tables from Intermediate Format."); ex.printStackTrace(); } - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); return xmlTableIF; } @@ -829,7 +829,7 @@ private String applyXSLT(String xsltfile, Object sourceXML, boolean fixSurrogate transformer.transform(src, sr); String xmlResult = resultWriter.toString(); - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime, xsltfile); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime, xsltfile); return xmlResult; } @@ -896,7 +896,7 @@ private String applyXSLTExtended(String xsltfile, StreamSource sourceXML, String transformer.transform(sourceXML, sr); String xmlResult = resultWriter.toString(); - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime, xsltfile); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime, xsltfile); return xmlResult; } @@ -1110,7 +1110,7 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi } catch (Exception e) { logger.log(Level.SEVERE, "Can''t obtain table's widths information: {0}", e.toString()); } - printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime); + Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); } private void debugSaveXML(String xmlString, String pathTo) { @@ -1142,11 +1142,4 @@ private int getIFPageCount(String xmlIF) { return pagecount; } - private void printProcessingTime(Method method, long startTime, String ... params) { - if (DEBUG) { - long endTime = System.currentTimeMillis(); - String addon = Arrays.toString(params); - logger.log(Level.INFO, "Method '" + method.getName() + "(" + addon + ")' processing time: {0} milliseconds", endTime - startTime); - } - } } diff --git a/src/main/java/org/metanorma/fop/Util.java b/src/main/java/org/metanorma/fop/Util.java index b2ead521..15a4c125 100644 --- a/src/main/java/org/metanorma/fop/Util.java +++ b/src/main/java/org/metanorma/fop/Util.java @@ -701,5 +701,15 @@ public static String innerXml(Node node) { } return sb.toString(); } - + + public static void printProcessingTime(String methodName, long startTime, String ... params) { + if (DEBUG) { + long endTime = System.currentTimeMillis(); + String addon = Arrays.toString(params); + if (addon.isEmpty()) { + addon = "(" + addon + ")"; + } + logger.log(Level.INFO, methodName + addon + " processing time: {0} milliseconds", endTime - startTime); + } + } } diff --git a/src/main/java/org/metanorma/fop/XSLTconverter.java b/src/main/java/org/metanorma/fop/XSLTconverter.java index e75f218b..ba093dcf 100644 --- a/src/main/java/org/metanorma/fop/XSLTconverter.java +++ b/src/main/java/org/metanorma/fop/XSLTconverter.java @@ -95,15 +95,6 @@ public void transform(SourceXMLDocument sourceXMLDocument) throws TransformerExc sourceXMLDocument.setXMLFO(xmlFO); - printProcessingTime(methodName); - - } - - - private void printProcessingTime(String methodName) { - if (DEBUG) { - long endTime = System.currentTimeMillis(); - logger.log(Level.INFO, methodName + " processing time: {0} milliseconds", endTime - startTime); - } + Util.printProcessingTime(methodName, startTime); } } diff --git a/src/main/java/org/metanorma/fop/mn2pdf.java b/src/main/java/org/metanorma/fop/mn2pdf.java index 8b2b4f9c..5b354f1d 100644 --- a/src/main/java/org/metanorma/fop/mn2pdf.java +++ b/src/main/java/org/metanorma/fop/mn2pdf.java @@ -25,6 +25,8 @@ public class mn2pdf { protected static final Logger logger = Logger.getLogger(LoggerHelper.LOGGER_NAME); + private static long startTime; + static final String CMD = "java -Xss5m -Xmx2048m -jar " + APP_NAME + ".jar"; static final Options optionsInfo = new Options() { @@ -224,6 +226,8 @@ public static void main(String[] args) throws ParseException { LoggerHelper.setupLogger(); + startTime = System.currentTimeMillis(); + CommandLineParser parser = new DefaultParser(); String ver = Util.getAppVersion(); @@ -347,6 +351,7 @@ public static void main(String[] args) throws ParseException { } LoggerHelper.closeFileHandler(); + Util.printProcessingTime("Total mn2pdf", startTime); } private static String getUsage() { @@ -357,5 +362,4 @@ private static String getUsage() { pw.flush(); return stringWriter.toString(); } - } From cf6b7abc2dab29d2b04e4d26655bdce2c2716897 Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Fri, 9 Dec 2022 21:54:28 +0300 Subject: [PATCH 02/10] log refactoring, metanorma/mn-native-pdf#393 --- .../java/org/metanorma/fop/PDFGenerator.java | 99 +++++++++++++++---- src/main/java/org/metanorma/fop/Profiler.java | 62 ++++++++++++ src/main/java/org/metanorma/fop/Util.java | 15 +-- .../java/org/metanorma/fop/XSLTconverter.java | 15 +-- src/main/java/org/metanorma/fop/mn2pdf.java | 10 +- 5 files changed, 158 insertions(+), 43 deletions(-) create mode 100644 src/main/java/org/metanorma/fop/Profiler.java diff --git a/src/main/java/org/metanorma/fop/PDFGenerator.java b/src/main/java/org/metanorma/fop/PDFGenerator.java index c757c90d..99e94505 100644 --- a/src/main/java/org/metanorma/fop/PDFGenerator.java +++ b/src/main/java/org/metanorma/fop/PDFGenerator.java @@ -208,8 +208,12 @@ public PDFGenerator (String inputXMLFilePath, String inputXSLFilePath, String ou public boolean process() { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + try { - + logger.info("Preparing..."); File fXML = new File(inputXMLFilePath); @@ -346,6 +350,7 @@ public boolean process() { e.printStackTrace(System.err); return false; } + Profiler.removeMethodCall(); return true; } @@ -360,7 +365,10 @@ public boolean process() { * @throws FOPException, SAXException In case of a FOP problem */ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File pdf) throws IOException, FOPException, SAXException, TransformerException, ParserConfigurationException { - + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + String imagesxml = sourceXMLDocument.getImageFilePath(); String indexxml = sourceXMLDocument.getIndexFilePath(); @@ -431,12 +439,16 @@ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File e.printStackTrace(System.err); System.exit(ERROR_EXIT_CODE); } - - + + Profiler.removeMethodCall(); } private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOException, FOPException, SAXException, TransformerException { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + OutputStream out = null; String xmlIF = null; long startMethodTime = System.currentTimeMillis(); @@ -600,10 +612,15 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOExceptio } } - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontcfg, Properties xslparams, XSLTconverter xsltConverter, File pdf) throws Exception, IOException, FOPException, SAXException, TransformerException, ParserConfigurationException { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + Source src = sourceFO; File fileXmlIF = new File(indexxml); @@ -637,12 +654,17 @@ private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontc src = new StreamSource(new StringReader(xmlFO)); } - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); return src; } private String generateFOPIntermediateFormat(Source src, File fontConfig, File pdf, boolean isSecondPass, String sfx) throws SAXException, IOException, TransformerConfigurationException, TransformerException { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + String xmlIF = ""; long startMethodTime = System.currentTimeMillis(); @@ -701,13 +723,17 @@ private String generateFOPIntermediateFormat(Source src, File fontConfig, File p out.close(); } - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); - + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); + return xmlIF; } private void createIndexFile(String indexxmlFilePath, String intermediateXML, File pdf) { - + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); try { @@ -734,10 +760,15 @@ private void createIndexFile(String indexxmlFilePath, String intermediateXML, Fi logger.severe("Can't save index.xml into temporary folder"); ex.printStackTrace(); } - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } private String addHiddenMath(String sourceXML) { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); @@ -746,17 +777,23 @@ private String addHiddenMath(String sourceXML) { InputSource inputSource = new InputSource( new StringReader(sourceXML)); saxParser.parse(inputSource, fopIFHiddenMathHandler); String result = fopIFHiddenMathHandler.getResultedXML(); - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.removeMethodCall(); return result; } catch (Exception ex) { logger.severe("Can't update IF for hidden math."); ex.printStackTrace(); } + Profiler.removeMethodCall(); return sourceXML; } private String flatIFforXFDF(String sourceXML) { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); @@ -765,17 +802,23 @@ private String flatIFforXFDF(String sourceXML) { InputSource inputSource = new InputSource( new StringReader(sourceXML)); saxParser.parse(inputSource, fopIFFlatHandler); String result = fopIFFlatHandler.getResultedXML(); - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.removeMethodCall(); return result; } catch (Exception ex) { logger.severe("Can't flat IF."); ex.printStackTrace(); } + Profiler.removeMethodCall(); return sourceXML; } private String createTableIF(String intermediateXML) { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + String xmlTableIF = ""; long startMethodTime = System.currentTimeMillis(); try { @@ -784,7 +827,8 @@ private String createTableIF(String intermediateXML) { logger.severe("Can't generate information about tables from Intermediate Format."); ex.printStackTrace(); } - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); return xmlTableIF; } @@ -813,6 +857,10 @@ private String createTableIF(String intermediateXML) { // Apply XSL tranformation (file xsltfile) for XML String or StreamSource private String applyXSLT(String xsltfile, Object sourceXML, boolean fixSurrogatePairs) throws Exception { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); Source srcXSL = new StreamSource(getStreamFromResources(getClass().getClassLoader(), xsltfile)); @@ -828,9 +876,10 @@ private String applyXSLT(String xsltfile, Object sourceXML, boolean fixSurrogate StreamResult sr = new StreamResult(resultWriter); transformer.transform(src, sr); String xmlResult = resultWriter.toString(); - - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime, xsltfile); - + + Profiler.printProcessingTime(methodName, startMethodTime, xsltfile); + Profiler.removeMethodCall(); + return xmlResult; } @@ -873,6 +922,10 @@ private String applyXSLT(String xsltfile, Object sourceXML, boolean fixSurrogate // Apply XSL tranformation (file xsltfile) for the source xml and IF string (parameter 'if_xml') private String applyXSLTExtended(String xsltfile, StreamSource sourceXML, String xmlIFStr, boolean fixSurrogatePairs) throws Exception { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); Source srcXSL = new StreamSource(getStreamFromResources(getClass().getClassLoader(), xsltfile)); @@ -895,9 +948,10 @@ private String applyXSLTExtended(String xsltfile, StreamSource sourceXML, String StreamResult sr = new StreamResult(resultWriter); transformer.transform(sourceXML, sr); String xmlResult = resultWriter.toString(); - - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime, xsltfile); - + + Profiler.printProcessingTime(methodName, startMethodTime, xsltfile); + Profiler.removeMethodCall(); + return xmlResult; } @@ -1054,6 +1108,10 @@ private void readEncryptionParameters(File fEncryptionParameters) { } private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, File pdf) { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); try { if (isTableExists && xmlTableIF.isEmpty()) { @@ -1110,7 +1168,8 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi } catch (Exception e) { logger.log(Level.SEVERE, "Can''t obtain table's widths information: {0}", e.toString()); } - Util.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } private void debugSaveXML(String xmlString, String pathTo) { diff --git a/src/main/java/org/metanorma/fop/Profiler.java b/src/main/java/org/metanorma/fop/Profiler.java new file mode 100644 index 00000000..b439a52d --- /dev/null +++ b/src/main/java/org/metanorma/fop/Profiler.java @@ -0,0 +1,62 @@ +package org.metanorma.fop; + +import org.metanorma.utils.LoggerHelper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Stack; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static org.metanorma.Constants.DEBUG; + +public class Profiler { + + protected static final Logger logger = Logger.getLogger(LoggerHelper.LOGGER_NAME); + + + static ArrayList arraysMethodCalls = new ArrayList<>(); + + static Stack stackMethods = new Stack<>(); + + public static void addMethodCall(String methodName) { + stackMethods.push(methodName); + String msg = String.join("", Collections.nCopies(stackMethods.size() * 2, " ")) + methodName; + arraysMethodCalls.add(msg); + } + + public static void removeMethodCall() { + try { + stackMethods.pop(); + } catch (Exception ex) {}; + } + + public static void printProcessingTime(String methodName, long startTime, String ... params) { + if (DEBUG) { + long endTime = System.currentTimeMillis(); + String addon = Arrays.toString(params); + if (addon.isEmpty()) { + addon = "(" + addon + ")"; + } + String msg = String.format(methodName + addon + " processing time: {0} milliseconds", endTime - startTime); + logger.log(Level.INFO, msg); + arraysMethodCalls.add(String.join("", Collections.nCopies(stackMethods.size() * 2, " ")) + msg); + } + } + + public static void printFullProcessingTime() { + if (DEBUG) { + logger.log(Level.INFO, "============================"); + logger.log(Level.INFO, "============================"); + logger.log(Level.INFO, "============================"); + for (String msg: arraysMethodCalls) { + logger.log(Level.INFO, msg); + } + logger.log(Level.INFO, "============================"); + logger.log(Level.INFO, "============================"); + logger.log(Level.INFO, "============================"); + } + } + +} diff --git a/src/main/java/org/metanorma/fop/Util.java b/src/main/java/org/metanorma/fop/Util.java index 15a4c125..83965331 100644 --- a/src/main/java/org/metanorma/fop/Util.java +++ b/src/main/java/org/metanorma/fop/Util.java @@ -697,19 +697,8 @@ public static String innerXml(Node node) { NodeList childNodes = node.getChildNodes(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < childNodes.getLength(); i++) { - sb.append(lsSerializer.writeToString(childNodes.item(i))); - } - return sb.toString(); - } - - public static void printProcessingTime(String methodName, long startTime, String ... params) { - if (DEBUG) { - long endTime = System.currentTimeMillis(); - String addon = Arrays.toString(params); - if (addon.isEmpty()) { - addon = "(" + addon + ")"; - } - logger.log(Level.INFO, methodName + addon + " processing time: {0} milliseconds", endTime - startTime); + sb.append(lsSerializer.writeToString(childNodes.item(i))); } + return sb.toString(); } } diff --git a/src/main/java/org/metanorma/fop/XSLTconverter.java b/src/main/java/org/metanorma/fop/XSLTconverter.java index ba093dcf..adbd3001 100644 --- a/src/main/java/org/metanorma/fop/XSLTconverter.java +++ b/src/main/java/org/metanorma/fop/XSLTconverter.java @@ -74,11 +74,13 @@ public Object getParam(String name) { } public void transform(SourceXMLDocument sourceXMLDocument) throws TransformerException { - - startTime = System.currentTimeMillis(); - + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - + + Profiler.addMethodCall(methodName); + + startTime = System.currentTimeMillis(); + //Setup input for XSLT transformation Source src = sourceXMLDocument.getStreamSource(); @@ -94,7 +96,8 @@ public void transform(SourceXMLDocument sourceXMLDocument) throws TransformerExc String xmlFO = resultWriter.toString(); sourceXMLDocument.setXMLFO(xmlFO); - - Util.printProcessingTime(methodName, startTime); + + Profiler.printProcessingTime(methodName, startTime); + Profiler.removeMethodCall(); } } diff --git a/src/main/java/org/metanorma/fop/mn2pdf.java b/src/main/java/org/metanorma/fop/mn2pdf.java index 5b354f1d..c1f58576 100644 --- a/src/main/java/org/metanorma/fop/mn2pdf.java +++ b/src/main/java/org/metanorma/fop/mn2pdf.java @@ -1,7 +1,6 @@ package org.metanorma.fop; -import java.io.PrintWriter; -import java.io.StringWriter; +import java.io.*; import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; @@ -14,7 +13,6 @@ import static org.metanorma.Constants.APP_NAME; import static org.metanorma.Constants.ERROR_EXIT_CODE; import static org.metanorma.Constants.DEBUG; -import static org.metanorma.fop.PDFGenerator.logger; import org.metanorma.utils.LoggerHelper; @@ -351,7 +349,11 @@ public static void main(String[] args) throws ParseException { } LoggerHelper.closeFileHandler(); - Util.printProcessingTime("Total mn2pdf", startTime); + Profiler.printProcessingTime("Total mn2pdf", startTime); + + Profiler.removeMethodCall(); + + Profiler.printFullProcessingTime(); } private static String getUsage() { From cf3ed51885fb03842e524f0ff34b39f6c0126e1b Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Fri, 9 Dec 2022 22:12:27 +0300 Subject: [PATCH 03/10] log refactoring, metanorma/mn-native-pdf#393 --- src/main/java/org/metanorma/fop/Profiler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/metanorma/fop/Profiler.java b/src/main/java/org/metanorma/fop/Profiler.java index b439a52d..6327a895 100644 --- a/src/main/java/org/metanorma/fop/Profiler.java +++ b/src/main/java/org/metanorma/fop/Profiler.java @@ -39,7 +39,7 @@ public static void printProcessingTime(String methodName, long startTime, String if (addon.isEmpty()) { addon = "(" + addon + ")"; } - String msg = String.format(methodName + addon + " processing time: {0} milliseconds", endTime - startTime); + String msg = String.format(methodName + addon + " processing time: %s milliseconds", endTime - startTime); logger.log(Level.INFO, msg); arraysMethodCalls.add(String.join("", Collections.nCopies(stackMethods.size() * 2, " ")) + msg); } From fdb62888ca67f1df941282c38c61470440dbe417 Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Sat, 10 Dec 2022 16:27:21 +0300 Subject: [PATCH 04/10] log refactoring, metanorma/mn-native-pdf#393 --- .../java/org/metanorma/fop/PDFGenerator.java | 35 +++++--- src/main/java/org/metanorma/fop/Profiler.java | 2 +- .../org/metanorma/fop/SourceXMLDocument.java | 34 +++++++- src/main/java/org/metanorma/fop/Util.java | 2 +- .../java/org/metanorma/fop/XSLTconverter.java | 2 - .../java/org/metanorma/fop/fontConfig.java | 85 +++++++++++++++++-- src/main/java/org/metanorma/fop/mn2pdf.java | 3 +- 7 files changed, 136 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/metanorma/fop/PDFGenerator.java b/src/main/java/org/metanorma/fop/PDFGenerator.java index 99e94505..18358c3e 100644 --- a/src/main/java/org/metanorma/fop/PDFGenerator.java +++ b/src/main/java/org/metanorma/fop/PDFGenerator.java @@ -662,13 +662,12 @@ private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontc private String generateFOPIntermediateFormat(Source src, File fontConfig, File pdf, boolean isSecondPass, String sfx) throws SAXException, IOException, TransformerConfigurationException, TransformerException { + long startMethodTime = System.currentTimeMillis(); String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); Profiler.addMethodCall(methodName); String xmlIF = ""; - - long startMethodTime = System.currentTimeMillis(); - + // run 1st pass to produce FOP Intermediate Format FopFactory fopFactory = FopFactory.newInstance(fontConfig); //Create a user agent @@ -860,7 +859,6 @@ private String applyXSLT(String xsltfile, Object sourceXML, boolean fixSurrogate String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); Source srcXSL = new StreamSource(getStreamFromResources(getClass().getClassLoader(), xsltfile)); @@ -1111,6 +1109,7 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); long startMethodTime = System.currentTimeMillis(); try { @@ -1126,33 +1125,36 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi logger.severe("Can't generate information about tables from Intermediate Format."); ex.printStackTrace(); } - + debugSaveXML(xmlTablesOnly, pdf.getAbsolutePath() + ".tablesonly.xml"); - + SourceXMLDocument sourceXMLDocumentTablesOnly = new SourceXMLDocument(xmlTablesOnly); + // transform XML to XSL-FO (XML .fo file) xsltConverter.transform(sourceXMLDocumentTablesOnly); String xmlFO = sourceXMLDocumentTablesOnly.getXMLFO(); - - debugSaveXML(xmlFO, pdf.getAbsolutePath() + ".fo.tables.xml"); - + //debug + debugSaveXML(xmlFO, pdf.getAbsolutePath() + ".fo.tables.xml"); + fontcfg.outputFontManifestLog(Paths.get(pdf.getAbsolutePath() + ".tables.fontmanifest.log.txt")); - + fontcfg.setSourceDocumentFontList(sourceXMLDocumentTablesOnly.getDocumentFonts()); Source sourceFO = new StreamSource(new StringReader(xmlFO)); + logger.info("[INFO] Generation of Intermediate Format with information about the table's widths ..."); String xmlIF = generateFOPIntermediateFormat(sourceFO, fontcfg.getConfig(), pdf, true, ".tables"); xmlTableIF = createTableIF(xmlIF); - + debugSaveXML(xmlTableIF, pdf.getAbsolutePath() + ".tables.xml"); - + xsltConverter.setParam("table_if", "false"); logger.info("[INFO] Generated successfully!"); } + if (!xmlTableIF.isEmpty()) { // pass Table widths XML via parameter 'if_xml' logger.info("[INFO] Generation XML with table's widths ..."); @@ -1175,12 +1177,21 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi private void debugSaveXML(String xmlString, String pathTo) { try { if (DEBUG) { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + //DEBUG: write table width information to file String xmlString_UTF8 = xmlString.replace("", ""); try ( BufferedWriter writer = Files.newBufferedWriter(Paths.get(pathTo))) { writer.write(xmlString_UTF8); } + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); + //Setup output //OutputStream outstream = new java.io.FileOutputStream(pdf.getAbsolutePath() + ".fo.xml"); //Resulting SAX events (the generated FO) must be piped through to FOP diff --git a/src/main/java/org/metanorma/fop/Profiler.java b/src/main/java/org/metanorma/fop/Profiler.java index 6327a895..bdc36780 100644 --- a/src/main/java/org/metanorma/fop/Profiler.java +++ b/src/main/java/org/metanorma/fop/Profiler.java @@ -39,7 +39,7 @@ public static void printProcessingTime(String methodName, long startTime, String if (addon.isEmpty()) { addon = "(" + addon + ")"; } - String msg = String.format(methodName + addon + " processing time: %s milliseconds", endTime - startTime); + String msg = String.format(methodName + addon + " processing time: %d milliseconds", endTime - startTime); logger.log(Level.INFO, msg); arraysMethodCalls.add(String.join("", Collections.nCopies(stackMethods.size() * 2, " ")) + msg); } diff --git a/src/main/java/org/metanorma/fop/SourceXMLDocument.java b/src/main/java/org/metanorma/fop/SourceXMLDocument.java index 4879e4c4..37a4c811 100644 --- a/src/main/java/org/metanorma/fop/SourceXMLDocument.java +++ b/src/main/java/org/metanorma/fop/SourceXMLDocument.java @@ -63,6 +63,10 @@ public class SourceXMLDocument { String xmlFO = ""; public SourceXMLDocument(File fXML) { + String methodName = "SourceXMLDocument"; + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + this.fXML = fXML; this.documentFilePath = this.fXML.getParent(); if (this.documentFilePath == null) { @@ -77,9 +81,15 @@ public SourceXMLDocument(File fXML) { logger.severe("Can't read source XML."); ex.printStackTrace(); } + Profiler.printProcessingTime("Parse " + methodName, startMethodTime); + Profiler.removeMethodCall(); } public SourceXMLDocument(String strXML) { + String methodName = "SourceXMLDocument"; + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + try { this.sourceXMLstr = strXML; DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); @@ -90,6 +100,8 @@ public SourceXMLDocument(String strXML) { logger.severe("Can't parse source XML."); ex.printStackTrace(); } + Profiler.printProcessingTime("Parse " + methodName, startMethodTime); + Profiler.removeMethodCall(); } public StreamSource getStreamSource() { @@ -113,7 +125,12 @@ public void setXMLFO(String xmlFO) { this.xmlFO = xmlFO; } - public List getDocumentFonts() { + public List getDocumentFonts() { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + List documentFontList = new ArrayList<>(); if(!xmlFO.isEmpty()) { @@ -149,7 +166,10 @@ public List getDocumentFonts() { logger.info(e.toString()); } } - + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); + return documentFontList; } @@ -248,6 +268,11 @@ public String getIndexFilePath() { } public ArrayList getLanguagesList () { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + ArrayList languagesList = new ArrayList<>(); try { // open XML and find all tags @@ -273,7 +298,10 @@ public ArrayList getLanguagesList () { logger.severe("Can't read language list from source XML."); ex.printStackTrace(); } - + + Profiler.printProcessingTime("Parse " + methodName, startMethodTime); + Profiler.removeMethodCall(); + return languagesList; } diff --git a/src/main/java/org/metanorma/fop/Util.java b/src/main/java/org/metanorma/fop/Util.java index 83965331..ac08f4a7 100644 --- a/src/main/java/org/metanorma/fop/Util.java +++ b/src/main/java/org/metanorma/fop/Util.java @@ -86,7 +86,7 @@ public class Util { protected static final Logger logger = Logger.getLogger(LoggerHelper.LOGGER_NAME); - + public static int getFileSize(URL url) { URLConnection conn = null; try { diff --git a/src/main/java/org/metanorma/fop/XSLTconverter.java b/src/main/java/org/metanorma/fop/XSLTconverter.java index adbd3001..beb9cf7e 100644 --- a/src/main/java/org/metanorma/fop/XSLTconverter.java +++ b/src/main/java/org/metanorma/fop/XSLTconverter.java @@ -76,9 +76,7 @@ public Object getParam(String name) { public void transform(SourceXMLDocument sourceXMLDocument) throws TransformerException { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - startTime = System.currentTimeMillis(); //Setup input for XSLT transformation diff --git a/src/main/java/org/metanorma/fop/fontConfig.java b/src/main/java/org/metanorma/fop/fontConfig.java index 24c9732b..11e91cce 100644 --- a/src/main/java/org/metanorma/fop/fontConfig.java +++ b/src/main/java/org/metanorma/fop/fontConfig.java @@ -100,7 +100,7 @@ public fontConfig() { setFontPath(DEFAULT_FONT_PATH); - FOPconfigXML = getSourceFOPConfigFile(); + FOPconfigXML = getSourceFOPConfigFile(); if (fopFonts.isEmpty()) { fopFonts = getFOPfonts(); @@ -128,6 +128,10 @@ public void setFontPath(String fontPath) { } public void setFontManifest(File fFontManifest) { + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + this.fFontManifest = fFontManifest; /* Example expected format: Cambria: @@ -293,13 +297,22 @@ public void setFontManifest(File fFontManifest) { System.exit(ERROR_EXIT_CODE); } } + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } public void outputFontManifestLog(Path logPath) { + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + if(DEBUG) { Util.outputLog(logPath, fontManifestLog.toString()); logger.log(Level.INFO, "Font manifest reading log saved to ''{0}''.", logPath); } + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } public Map getFontStyles(String style) { @@ -404,6 +417,11 @@ public FOPFontTriplet getFontDefaultProperties(String font) { } public void setSourceDocumentFontList(List sourceDocumentFontList) { + + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + this.sourceDocumentFontList = sourceDocumentFontList; isReady = false; @@ -421,11 +439,17 @@ public void setSourceDocumentFontList(List sourceDocumentFontList) { } }*/ } - + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } private void updateConfig() throws IOException, Exception { + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + if(!isReady) { // set file paths for fonts @@ -460,6 +484,9 @@ private void updateConfig() throws IOException, Exception { }*/ isReady = true; } + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } public void outputAvailableAWTFonts(Path logPath) { @@ -471,11 +498,19 @@ public void outputAvailableAWTFonts(Path logPath) { } private Document getSourceFOPConfigFile() { + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); InputStream config = Util.getStreamFromResources(getClass().getClassLoader(), CONFIG_NAME); Document sourceFOPConfig = dBuilder.parse(config); + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); + return sourceFOPConfig; } catch (Exception ex) { logger.severe(ex.toString()); @@ -483,7 +518,11 @@ private Document getSourceFOPConfigFile() { } } - private List getFOPfonts() { + private List getFOPfonts() { + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + List fonts = new ArrayList<>(); NodeList fontNodes = FOPconfigXML.getElementsByTagName("font"); @@ -502,7 +541,11 @@ private List getFOPfonts() { logger.log(Level.SEVERE, "Error in reading font information: {0}", ex.toString()); logger.log(Level.SEVERE, "XML fragment: {0}", innerXml(fontNode)); } - } + } + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); + return fonts; } @@ -519,7 +562,10 @@ public List getUsedFonts () // set file paths for fonts private void setFontsPaths() throws IOException, URISyntaxException { - + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + List machineFontList = getMachineFonts(); fopFonts.stream() @@ -623,9 +669,16 @@ private void setFontsPaths() throws IOException, URISyntaxException { } }); printMessage(sb.toString()); + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } private List getMachineFonts() throws IOException{ + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + List systemFontListURL = new ArrayList<>(); List userFontListURL = new ArrayList<>(); @@ -643,10 +696,18 @@ private List getMachineFonts() throws IOException{ for(URL url: userFontListURL){ machineFontList.add(URLDecoder.decode(url.toString(), StandardCharsets.UTF_8.name())); } + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); + return machineFontList; } private void updateFontsInFOPConfig(Document xmlDocument) { + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "/fop/renderers/renderer/fonts"; try { @@ -702,7 +763,10 @@ private void updateFontsInFOPConfig(Document xmlDocument) { } } catch (XPathExpressionException ex) { logger.severe(ex.toString()); - } + } + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } public void outputFOPFontsLog(Path logPath) { @@ -770,6 +834,10 @@ public void setPDFUAmode(String mode) throws SAXException, IOException, ParserCo private void updateFontsForGraphicsEnvironment(){ + String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + Profiler.addMethodCall(methodName); + long startMethodTime = System.currentTimeMillis(); + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try (Stream walk = Files.walk(Paths.get(this.fontPath))) { @@ -787,6 +855,9 @@ private void updateFontsForGraphicsEnvironment(){ } catch (IOException e) { e.printStackTrace(); } + + Profiler.printProcessingTime(methodName, startMethodTime); + Profiler.removeMethodCall(); } public static void registerFont(GraphicsEnvironment ge, String fontFile){ @@ -860,8 +931,8 @@ private String innerXml(Node node) { Result target = new StreamResult(out); transformer.transform(source, target); } catch (TransformerException ex) {} + return out.toString(); - } } diff --git a/src/main/java/org/metanorma/fop/mn2pdf.java b/src/main/java/org/metanorma/fop/mn2pdf.java index c1f58576..95b113a2 100644 --- a/src/main/java/org/metanorma/fop/mn2pdf.java +++ b/src/main/java/org/metanorma/fop/mn2pdf.java @@ -221,7 +221,8 @@ public class mn2pdf { * @param args command-line arguments */ public static void main(String[] args) throws ParseException { - + Profiler.addMethodCall(new Object(){}.getClass().getEnclosingMethod().getName()); + LoggerHelper.setupLogger(); startTime = System.currentTimeMillis(); From efb0bebeea9812b6b64c642d3cd27112783c23c3 Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Sat, 10 Dec 2022 16:28:42 +0300 Subject: [PATCH 05/10] fonts registration optimized, metanorma/mn-native-pdf#393 --- .../java/org/metanorma/fop/PDFGenerator.java | 1 - .../java/org/metanorma/fop/fontConfig.java | 61 +++++++++++-------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/metanorma/fop/PDFGenerator.java b/src/main/java/org/metanorma/fop/PDFGenerator.java index 18358c3e..acef80f7 100644 --- a/src/main/java/org/metanorma/fop/PDFGenerator.java +++ b/src/main/java/org/metanorma/fop/PDFGenerator.java @@ -1111,7 +1111,6 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi Profiler.addMethodCall(methodName); long startMethodTime = System.currentTimeMillis(); - long startMethodTime = System.currentTimeMillis(); try { if (isTableExists && xmlTableIF.isEmpty()) { // generate IF with table width data diff --git a/src/main/java/org/metanorma/fop/fontConfig.java b/src/main/java/org/metanorma/fop/fontConfig.java index 11e91cce..6ac1aa71 100644 --- a/src/main/java/org/metanorma/fop/fontConfig.java +++ b/src/main/java/org/metanorma/fop/fontConfig.java @@ -88,7 +88,9 @@ class fontConfig { static List fopFonts = new ArrayList<>(); private File fFontManifest; - + + private static List registeredFonts = new ArrayList<>(); + private boolean isReady = false; //private final List defaultFontList = new DefaultFonts().getDefaultFonts(); @@ -861,35 +863,40 @@ private void updateFontsForGraphicsEnvironment(){ } public static void registerFont(GraphicsEnvironment ge, String fontFile){ - if (ge == null) { - ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); - } - try { - // try to obtain font to check an exception: - // java.lang.NullPointerException: Cannot load from short array because "sun.awt.FontConfiguration.head" is null - String[] names = ge.getAvailableFontFamilyNames(); - int count = names.length; - } catch (Exception e) { - logger.severe("[ERROR] " + e.toString()); - logger.severe("[ERROR] fontconfig not found or no one system font not installed."); - System.exit(ERROR_EXIT_CODE); - } - try { - Font ttfFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontFile)); - //register the font - ge.registerFont(ttfFont); - } catch(FontFormatException e) { + if (!registeredFonts.contains(fontFile)) { + + if (ge == null) { + ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + } try { - Font type1Font = Font.createFont(Font.TYPE1_FONT, new File(fontFile)); + // try to obtain font to check an exception: + // java.lang.NullPointerException: Cannot load from short array because "sun.awt.FontConfiguration.head" is null + String[] names = ge.getAvailableFontFamilyNames(); + int count = names.length; + } catch (Exception e) { + logger.severe("[ERROR] " + e.toString()); + logger.severe("[ERROR] fontconfig not found or no one system font not installed."); + System.exit(ERROR_EXIT_CODE); + } + try { + Font ttfFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontFile)); //register the font - ge.registerFont(type1Font); - } catch(FontFormatException e1) { - e1.printStackTrace(); - } catch (IOException e2) { - e2.printStackTrace(); + ge.registerFont(ttfFont); + registeredFonts.add(fontFile); + } catch(FontFormatException e) { + try { + Font type1Font = Font.createFont(Font.TYPE1_FONT, new File(fontFile)); + //register the font + ge.registerFont(type1Font); + registeredFonts.add(fontFile); + } catch(FontFormatException e1) { + e1.printStackTrace(); + } catch (IOException e2) { + e2.printStackTrace(); + } + } catch (IOException e) { + e.printStackTrace(); } - } catch (IOException e) { - e.printStackTrace(); } } From 8a33fc0fece5fc586ec0ea5f2686837d3cbff1d3 Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Sat, 10 Dec 2022 18:47:10 +0300 Subject: [PATCH 06/10] redundant debug info removed, added class info, metanorma/mn-native-pdf#393 --- .../java/org/metanorma/fop/PDFGenerator.java | 37 +++++------ .../org/metanorma/fop/SourceXMLDocument.java | 24 ------- src/main/java/org/metanorma/fop/Util.java | 2 +- .../java/org/metanorma/fop/XSLTconverter.java | 2 +- .../java/org/metanorma/fop/fontConfig.java | 66 +------------------ 5 files changed, 19 insertions(+), 112 deletions(-) diff --git a/src/main/java/org/metanorma/fop/PDFGenerator.java b/src/main/java/org/metanorma/fop/PDFGenerator.java index acef80f7..22cf2bcc 100644 --- a/src/main/java/org/metanorma/fop/PDFGenerator.java +++ b/src/main/java/org/metanorma/fop/PDFGenerator.java @@ -209,7 +209,7 @@ public PDFGenerator (String inputXMLFilePath, String inputXSLFilePath, String ou public boolean process() { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); try { @@ -366,7 +366,7 @@ public boolean process() { */ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File pdf) throws IOException, FOPException, SAXException, TransformerException, ParserConfigurationException { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); String imagesxml = sourceXMLDocument.getImageFilePath(); @@ -374,7 +374,7 @@ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File String indexxml = sourceXMLDocument.getIndexFilePath(); try { - + //Setup XSLT Properties additionalXSLTparams = new Properties(); @@ -446,7 +446,7 @@ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOException, FOPException, SAXException, TransformerException { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); OutputStream out = null; @@ -618,7 +618,7 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOExceptio private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontcfg, Properties xslparams, XSLTconverter xsltConverter, File pdf) throws Exception, IOException, FOPException, SAXException, TransformerException, ParserConfigurationException { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); Source src = sourceFO; @@ -663,7 +663,7 @@ private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontc private String generateFOPIntermediateFormat(Source src, File fontConfig, File pdf, boolean isSecondPass, String sfx) throws SAXException, IOException, TransformerConfigurationException, TransformerException { long startMethodTime = System.currentTimeMillis(); - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); String xmlIF = ""; @@ -730,7 +730,7 @@ private String generateFOPIntermediateFormat(Source src, File fontConfig, File p private void createIndexFile(String indexxmlFilePath, String intermediateXML, File pdf) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); long startMethodTime = System.currentTimeMillis(); @@ -765,7 +765,7 @@ private void createIndexFile(String indexxmlFilePath, String intermediateXML, Fi private String addHiddenMath(String sourceXML) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); long startMethodTime = System.currentTimeMillis(); @@ -776,7 +776,7 @@ private String addHiddenMath(String sourceXML) { InputSource inputSource = new InputSource( new StringReader(sourceXML)); saxParser.parse(inputSource, fopIFHiddenMathHandler); String result = fopIFHiddenMathHandler.getResultedXML(); - Profiler.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(methodName, startMethodTime); Profiler.removeMethodCall(); return result; } @@ -790,7 +790,7 @@ private String addHiddenMath(String sourceXML) { private String flatIFforXFDF(String sourceXML) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); long startMethodTime = System.currentTimeMillis(); @@ -801,7 +801,7 @@ private String flatIFforXFDF(String sourceXML) { InputSource inputSource = new InputSource( new StringReader(sourceXML)); saxParser.parse(inputSource, fopIFFlatHandler); String result = fopIFFlatHandler.getResultedXML(); - Profiler.printProcessingTime(new Object(){}.getClass().getEnclosingMethod().getName(), startMethodTime); + Profiler.printProcessingTime(methodName, startMethodTime); Profiler.removeMethodCall(); return result; } @@ -815,7 +815,7 @@ private String flatIFforXFDF(String sourceXML) { private String createTableIF(String intermediateXML) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); String xmlTableIF = ""; @@ -857,7 +857,7 @@ private String createTableIF(String intermediateXML) { // Apply XSL tranformation (file xsltfile) for XML String or StreamSource private String applyXSLT(String xsltfile, Object sourceXML, boolean fixSurrogatePairs) throws Exception { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); long startMethodTime = System.currentTimeMillis(); @@ -921,7 +921,7 @@ private String applyXSLT(String xsltfile, Object sourceXML, boolean fixSurrogate // Apply XSL tranformation (file xsltfile) for the source xml and IF string (parameter 'if_xml') private String applyXSLTExtended(String xsltfile, StreamSource sourceXML, String xmlIFStr, boolean fixSurrogatePairs) throws Exception { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); long startMethodTime = System.currentTimeMillis(); @@ -1107,7 +1107,7 @@ private void readEncryptionParameters(File fEncryptionParameters) { private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, File pdf) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); long startMethodTime = System.currentTimeMillis(); @@ -1177,10 +1177,6 @@ private void debugSaveXML(String xmlString, String pathTo) { try { if (DEBUG) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); - //DEBUG: write table width information to file String xmlString_UTF8 = xmlString.replace("", ""); try ( @@ -1188,9 +1184,6 @@ private void debugSaveXML(String xmlString, String pathTo) { writer.write(xmlString_UTF8); } - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); - //Setup output //OutputStream outstream = new java.io.FileOutputStream(pdf.getAbsolutePath() + ".fo.xml"); //Resulting SAX events (the generated FO) must be piped through to FOP diff --git a/src/main/java/org/metanorma/fop/SourceXMLDocument.java b/src/main/java/org/metanorma/fop/SourceXMLDocument.java index 37a4c811..a4eeb036 100644 --- a/src/main/java/org/metanorma/fop/SourceXMLDocument.java +++ b/src/main/java/org/metanorma/fop/SourceXMLDocument.java @@ -63,9 +63,6 @@ public class SourceXMLDocument { String xmlFO = ""; public SourceXMLDocument(File fXML) { - String methodName = "SourceXMLDocument"; - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); this.fXML = fXML; this.documentFilePath = this.fXML.getParent(); @@ -81,14 +78,9 @@ public SourceXMLDocument(File fXML) { logger.severe("Can't read source XML."); ex.printStackTrace(); } - Profiler.printProcessingTime("Parse " + methodName, startMethodTime); - Profiler.removeMethodCall(); } public SourceXMLDocument(String strXML) { - String methodName = "SourceXMLDocument"; - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); try { this.sourceXMLstr = strXML; @@ -100,8 +92,6 @@ public SourceXMLDocument(String strXML) { logger.severe("Can't parse source XML."); ex.printStackTrace(); } - Profiler.printProcessingTime("Parse " + methodName, startMethodTime); - Profiler.removeMethodCall(); } public StreamSource getStreamSource() { @@ -127,10 +117,6 @@ public void setXMLFO(String xmlFO) { public List getDocumentFonts() { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); - List documentFontList = new ArrayList<>(); if(!xmlFO.isEmpty()) { @@ -167,9 +153,6 @@ public List getDocumentFonts() { } } - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); - return documentFontList; } @@ -269,10 +252,6 @@ public String getIndexFilePath() { public ArrayList getLanguagesList () { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); - ArrayList languagesList = new ArrayList<>(); try { // open XML and find all tags @@ -299,9 +278,6 @@ public ArrayList getLanguagesList () { ex.printStackTrace(); } - Profiler.printProcessingTime("Parse " + methodName, startMethodTime); - Profiler.removeMethodCall(); - return languagesList; } diff --git a/src/main/java/org/metanorma/fop/Util.java b/src/main/java/org/metanorma/fop/Util.java index ac08f4a7..aaa201cf 100644 --- a/src/main/java/org/metanorma/fop/Util.java +++ b/src/main/java/org/metanorma/fop/Util.java @@ -691,7 +691,7 @@ public static String floatArrayToString(float[] a) { } public static String innerXml(Node node) { - DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0"); + DOMImplementationLS lsImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation().getFeature("LS", "3.0"); LSSerializer lsSerializer = lsImpl.createLSSerializer(); lsSerializer.getDomConfig().setParameter("xml-declaration", false); NodeList childNodes = node.getChildNodes(); diff --git a/src/main/java/org/metanorma/fop/XSLTconverter.java b/src/main/java/org/metanorma/fop/XSLTconverter.java index beb9cf7e..56ccf145 100644 --- a/src/main/java/org/metanorma/fop/XSLTconverter.java +++ b/src/main/java/org/metanorma/fop/XSLTconverter.java @@ -75,7 +75,7 @@ public Object getParam(String name) { public void transform(SourceXMLDocument sourceXMLDocument) throws TransformerException { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); + String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); startTime = System.currentTimeMillis(); diff --git a/src/main/java/org/metanorma/fop/fontConfig.java b/src/main/java/org/metanorma/fop/fontConfig.java index 6ac1aa71..ba9e4bf3 100644 --- a/src/main/java/org/metanorma/fop/fontConfig.java +++ b/src/main/java/org/metanorma/fop/fontConfig.java @@ -130,9 +130,6 @@ public void setFontPath(String fontPath) { } public void setFontManifest(File fFontManifest) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); this.fFontManifest = fFontManifest; /* Example expected format: @@ -299,22 +296,13 @@ public void setFontManifest(File fFontManifest) { System.exit(ERROR_EXIT_CODE); } } - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); } public void outputFontManifestLog(Path logPath) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); - if(DEBUG) { Util.outputLog(logPath, fontManifestLog.toString()); logger.log(Level.INFO, "Font manifest reading log saved to ''{0}''.", logPath); } - - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); } public Map getFontStyles(String style) { @@ -420,10 +408,6 @@ public FOPFontTriplet getFontDefaultProperties(String font) { public void setSourceDocumentFontList(List sourceDocumentFontList) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); - this.sourceDocumentFontList = sourceDocumentFontList; isReady = false; @@ -441,16 +425,10 @@ public void setSourceDocumentFontList(List sourceDocumentFontList) { } }*/ } - - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); } private void updateConfig() throws IOException, Exception { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); if(!isReady) { @@ -486,9 +464,6 @@ private void updateConfig() throws IOException, Exception { }*/ isReady = true; } - - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); } public void outputAvailableAWTFonts(Path logPath) { @@ -500,9 +475,6 @@ public void outputAvailableAWTFonts(Path logPath) { } private Document getSourceFOPConfigFile() { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); try { @@ -510,9 +482,6 @@ private Document getSourceFOPConfigFile() { InputStream config = Util.getStreamFromResources(getClass().getClassLoader(), CONFIG_NAME); Document sourceFOPConfig = dBuilder.parse(config); - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); - return sourceFOPConfig; } catch (Exception ex) { logger.severe(ex.toString()); @@ -521,10 +490,6 @@ private Document getSourceFOPConfigFile() { } private List getFOPfonts() { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); - List fonts = new ArrayList<>(); NodeList fontNodes = FOPconfigXML.getElementsByTagName("font"); @@ -533,10 +498,10 @@ private List getFOPfonts() { for (int i = 0; i < fontNodes.getLength(); i++) { Node fontNode = fontNodes.item(i); XmlMapper xmlMapper = new XmlMapper(); - + try { // DEBUG String xml = xmlMapper.writeValueAsString(new FOPFont()); - + FOPFont fopfont = xmlMapper.readValue(innerXml(fontNode), FOPFont.class); fonts.add(fopfont); } catch (JsonProcessingException ex) { @@ -545,9 +510,6 @@ private List getFOPfonts() { } } - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); - return fonts; } @@ -564,9 +526,6 @@ public List getUsedFonts () // set file paths for fonts private void setFontsPaths() throws IOException, URISyntaxException { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); List machineFontList = getMachineFonts(); @@ -671,15 +630,9 @@ private void setFontsPaths() throws IOException, URISyntaxException { } }); printMessage(sb.toString()); - - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); } private List getMachineFonts() throws IOException{ - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); List systemFontListURL = new ArrayList<>(); List userFontListURL = new ArrayList<>(); @@ -699,16 +652,10 @@ private List getMachineFonts() throws IOException{ machineFontList.add(URLDecoder.decode(url.toString(), StandardCharsets.UTF_8.name())); } - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); - return machineFontList; } private void updateFontsInFOPConfig(Document xmlDocument) { - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "/fop/renderers/renderer/fonts"; @@ -766,9 +713,6 @@ private void updateFontsInFOPConfig(Document xmlDocument) { } catch (XPathExpressionException ex) { logger.severe(ex.toString()); } - - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); } public void outputFOPFontsLog(Path logPath) { @@ -836,9 +780,6 @@ public void setPDFUAmode(String mode) throws SAXException, IOException, ParserCo private void updateFontsForGraphicsEnvironment(){ - String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); - Profiler.addMethodCall(methodName); - long startMethodTime = System.currentTimeMillis(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); @@ -857,9 +798,6 @@ private void updateFontsForGraphicsEnvironment(){ } catch (IOException e) { e.printStackTrace(); } - - Profiler.printProcessingTime(methodName, startMethodTime); - Profiler.removeMethodCall(); } public static void registerFont(GraphicsEnvironment ge, String fontFile){ From 80768b8764d3538088f2118a346afb8a2c96f2a2 Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Sat, 10 Dec 2022 21:15:56 +0300 Subject: [PATCH 07/10] added parameter to omit attachments in intermediate processing, metanorma/mn-native-pdf#393 --- src/main/java/org/metanorma/fop/PDFGenerator.java | 4 ++-- src/main/java/org/metanorma/fop/XSLTconverter.java | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/metanorma/fop/PDFGenerator.java b/src/main/java/org/metanorma/fop/PDFGenerator.java index 22cf2bcc..e5016395 100644 --- a/src/main/java/org/metanorma/fop/PDFGenerator.java +++ b/src/main/java/org/metanorma/fop/PDFGenerator.java @@ -1130,8 +1130,8 @@ private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, Fi SourceXMLDocument sourceXMLDocumentTablesOnly = new SourceXMLDocument(xmlTablesOnly); // transform XML to XSL-FO (XML .fo file) - xsltConverter.transform(sourceXMLDocumentTablesOnly); - + xsltConverter.transform(sourceXMLDocumentTablesOnly, false); + String xmlFO = sourceXMLDocumentTablesOnly.getXMLFO(); //debug diff --git a/src/main/java/org/metanorma/fop/XSLTconverter.java b/src/main/java/org/metanorma/fop/XSLTconverter.java index 56ccf145..d76534bd 100644 --- a/src/main/java/org/metanorma/fop/XSLTconverter.java +++ b/src/main/java/org/metanorma/fop/XSLTconverter.java @@ -72,8 +72,12 @@ public void setParam(String name, Object value) { public Object getParam(String name) { return transformerFO.getParameter(name); } - + public void transform(SourceXMLDocument sourceXMLDocument) throws TransformerException { + transform(sourceXMLDocument, true); + } + + public void transform(SourceXMLDocument sourceXMLDocument, boolean isFinalTransform) throws TransformerException { String methodName = getClass().getSimpleName() + "." + (new Object(){}.getClass().getEnclosingMethod().getName()); Profiler.addMethodCall(methodName); @@ -87,7 +91,8 @@ public void transform(SourceXMLDocument sourceXMLDocument) throws TransformerExc // Step 0. Convert XML to FO file with XSL StringWriter resultWriter = new StringWriter(); StreamResult sr = new StreamResult(resultWriter); - + + transformerFO.setParameter("final_transform", String.valueOf(isFinalTransform)); //Start XSLT transformation and FO generating transformerFO.transform(src, sr); From bc460d777c3bafb43993fd43f80d6b4f601ab226 Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Sat, 10 Dec 2022 21:56:30 +0300 Subject: [PATCH 08/10] added base64 encoding for embedded files, metanorma/mn-native-pdf#393 --- src/main/java/org/metanorma/fop/Util.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/org/metanorma/fop/Util.java b/src/main/java/org/metanorma/fop/Util.java index aaa201cf..205a79bc 100644 --- a/src/main/java/org/metanorma/fop/Util.java +++ b/src/main/java/org/metanorma/fop/Util.java @@ -701,4 +701,11 @@ public static String innerXml(Node node) { } return sb.toString(); } + + public static String encodeBase64(String input) { + if (input == null || input.isEmpty()) { + return ""; + } + return Base64.getEncoder().encodeToString(input.getBytes()); + } } From ced087cf73d7035bc50de5fab9395b09bf123e82 Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Sun, 11 Dec 2022 20:26:41 +0300 Subject: [PATCH 09/10] optimized for embed files at last stage only, metanorma/mn-native-pdf#393 [skip actions] --- src/main/java/org/metanorma/fop/PDFGenerator.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/metanorma/fop/PDFGenerator.java b/src/main/java/org/metanorma/fop/PDFGenerator.java index e5016395..f99e13ba 100644 --- a/src/main/java/org/metanorma/fop/PDFGenerator.java +++ b/src/main/java/org/metanorma/fop/PDFGenerator.java @@ -401,7 +401,12 @@ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File logger.info("[INFO] XSL-FO file preparation..."); // transform XML to XSL-FO (XML .fo file) - xsltConverter.transform(sourceXMLDocument); + if (shouldCreateIFFile(indexxml)) { + // IF file will be created later in runSecondPass, so no need to set "final_transform" = true (i.e. attach embedded files) + xsltConverter.transform(sourceXMLDocument, false); + } else { + xsltConverter.transform(sourceXMLDocument); + } String xmlFO = sourceXMLDocument.getXMLFO(); debugXSLFO = xmlFO; @@ -627,7 +632,7 @@ private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontc long startMethodTime = System.currentTimeMillis(); - if (!indexxml.isEmpty() && !fileXmlIF.exists()) { //there is index + if (shouldCreateIFFile(indexxml)) { //there is index // if file exist - it means that now document by language is processing // and don't need to create intermediate file again @@ -658,7 +663,10 @@ private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontc Profiler.removeMethodCall(); return src; } - + + private boolean shouldCreateIFFile(String indexxml) { + return !indexxml.isEmpty() && !(new File(indexxml)).exists(); + } private String generateFOPIntermediateFormat(Source src, File fontConfig, File pdf, boolean isSecondPass, String sfx) throws SAXException, IOException, TransformerConfigurationException, TransformerException { From b3ec3f70449f2888cca664daf663947914f9e5be Mon Sep 17 00:00:00 2001 From: Intelligent2013 Date: Sun, 11 Dec 2022 22:41:36 +0300 Subject: [PATCH 10/10] trigger