Skip to content

Commit

Permalink
Merge pull request #136 from metanorma/profiling
Browse files Browse the repository at this point in the history
Profiling
  • Loading branch information
Intelligent2013 authored Sep 3, 2022
2 parents 2bc33dd + 1ab7bf9 commit 7abecb7
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.49
JAR_VERSION := 1.50
JAR_FILE := mn2pdf-$(JAR_VERSION).jar

all: target/$(JAR_FILE)
Expand Down
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.49.jar --xml-file <XML-FileName> --xsl-file <XSLT-FileName> --pdf-file <Output-PDF-FileName> [--syntax-highlight]
java -Xss5m -Xmx2048m -jar target/mn2pdf-1.50.jar --xml-file <XML-FileName> --xsl-file <XSLT-FileName> --pdf-file <Output-PDF-FileName> [--syntax-highlight]
----

e.g.

[source,sh]
----
java -Xss5m -Xmx2048m -jar target/mn2pdf-1.49.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.50.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf
----

=== PDF encryption features
Expand Down Expand Up @@ -100,7 +100,7 @@ Update version in `pom.xml`, e.g.:
----
<groupId>org.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.49</version>
<version>1.50</version>
<name>Metanorma XML to PDF converter</name>
----

Expand All @@ -111,8 +111,8 @@ Tag the same version in Git:

[source,xml]
----
git tag v1.49
git push origin v1.49
git tag v1.50
git push origin v1.50
----

Then the corresponding GitHub release will be automatically created at:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.49</version>
<version>1.50</version>
<name>Metanorma XML to PDF converter</name>
<packaging>jar</packaging>
<url>https://www.metanorma.org</url>
Expand Down
112 changes: 83 additions & 29 deletions src/main/java/org/metanorma/fop/PDFGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -112,8 +114,6 @@ public class PDFGenerator {

private String debugXSLFO = "";

private long startTime;

public void setFontsPath(String fontsPath) {
this.fontsPath = fontsPath;
}
Expand Down Expand Up @@ -389,11 +389,7 @@ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File
logger.info("[INFO] XSL-FO file preparation...");

// transform XML to XSL-FO (XML .fo file)
startTime = System.currentTimeMillis();

xsltConverter.transform(sourceXMLDocument);

printProcessingTime();

String xmlFO = sourceXMLDocument.getXMLFO();
debugXSLFO = xmlFO;
Expand Down Expand Up @@ -440,6 +436,7 @@ private void convertmn2pdf(fontConfig fontcfg, XSLTconverter xsltConverter, File
private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOException, FOPException, SAXException, TransformerException {
OutputStream out = null;
String xmlIF = null;
long startMethodTime = System.currentTimeMillis();
try {

String mime = MimeConstants.MIME_PDF;
Expand All @@ -454,21 +451,21 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOExceptio
if (isAddMathAsText) {
logger.info("Updating Intermediate Format (adding hidden math)...");
xmlIF = applyXSLT("add_hidden_math.xsl", xmlIF, true);
}

debugXSLFO = xmlIF;

debugXSLFO = xmlIF;
debugSaveXML(xmlIF, pdf.getAbsolutePath() + ".if.mathtext.xml");
}

debugSaveXML(xmlIF, pdf.getAbsolutePath() + ".if.mathtext.xml");

if (isAddLineNumbers) {
logger.info("Updating Intermediate Format (adding line numbers)...");
xmlIF = applyXSLT("add_line_numbers.xsl", xmlIF, true);
}

debugXSLFO = xmlIF;

debugSaveXML(xmlIF, pdf.getAbsolutePath() + ".if.linenumbers.xml");

debugXSLFO = xmlIF;

debugSaveXML(xmlIF, pdf.getAbsolutePath() + ".if.linenumbers.xml");
}

src = new StreamSource(new StringReader(xmlIF));
}
Expand Down Expand Up @@ -575,6 +572,8 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf) throws IOExceptio
}
}

printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime);

if (isAddAnnotations) {
logger.log(Level.INFO, "[INFO] Annotation processing...");
try {
Expand All @@ -597,15 +596,15 @@ private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontc

File fileXmlIF = new File(indexxml);

long startMethodTime = System.currentTimeMillis();

if (!indexxml.isEmpty() && !fileXmlIF.exists()) { //there is index
// if file exist - it means that now document by language is processing
// and don't need to create intermediate file again

String xmlIF = generateFOPIntermediateFormat(sourceFO, fontcfg.getConfig(), pdf, true, "");


//Util.createIndexFile(indexxml, xmlIF);
createIndexFile(indexxml, xmlIF);
createIndexFile(indexxml, xmlIF, pdf);

if (fileXmlIF.exists()) {
// pass index.xml path to xslt (for second pass)
Expand All @@ -626,13 +625,16 @@ private Source runSecondPass (String indexxml, Source sourceFO, fontConfig fontc
src = new StreamSource(new StringReader(xmlFO));

}
printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime);
return src;
}


private String generateFOPIntermediateFormat(Source src, File fontConfig, File pdf, boolean isSecondPass, String sfx) throws SAXException, IOException, TransformerConfigurationException, TransformerException {
String xmlIF = "";

long startMethodTime = System.currentTimeMillis();

// run 1st pass to produce FOP Intermediate Format
FopFactory fopFactory = FopFactory.newInstance(fontConfig);
//Create a user agent
Expand Down Expand Up @@ -685,10 +687,15 @@ private String generateFOPIntermediateFormat(Source src, File fontConfig, File p
out.close();
}

printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime);

return xmlIF;
}

private void createIndexFile(String indexxmlFilePath, String intermediateXML) {
private void createIndexFile(String indexxmlFilePath, String intermediateXML, File pdf) {

long startMethodTime = System.currentTimeMillis();

try {
String xmlIndex = applyXSLT("index.xsl", intermediateXML, false);

Expand All @@ -698,29 +705,35 @@ private void createIndexFile(String indexxmlFilePath, String intermediateXML) {
writer.write(xmlIndex.toString());
}
}
debugSaveXML(xmlIndex, pdf.getAbsolutePath() + ".index.xml");
}
catch (Exception ex) {
//System.err.println("Can't save index.xml into temporary folder");
logger.severe("Can't save index.xml into temporary folder");
ex.printStackTrace();
}
}
printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime);
}


private String createTableIF(String intermediateXML) {
String xmlTableIF = "";
long startMethodTime = System.currentTimeMillis();
try {
xmlTableIF = applyXSLT("table_if.xsl", intermediateXML, false);
} catch (Exception ex) {
logger.severe("Can't generate information about tables from Intermediate Format.");
ex.printStackTrace();
}
printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime);
return xmlTableIF;
}


// Apply XSL tranformation (file xsltfile) for xml string
private String applyXSLT(String xsltfile, String xmlStr, boolean fixSurrogatePairs) throws Exception {
/*private String applyXSLT(String xsltfile, String xmlStr, boolean fixSurrogatePairs) throws Exception {
String xmlTableIF = "";
long startMethodTime = System.currentTimeMillis();
Source srcXSL = new StreamSource(getStreamFromResources(getClass().getClassLoader(), xsltfile));
TransformerFactory factory = TransformerFactory.newInstance();
Expand All @@ -734,11 +747,37 @@ private String applyXSLT(String xsltfile, String xmlStr, boolean fixSurrogatePai
transformer.transform(src, sr);
String xmlResult = resultWriter.toString();
printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime);
return xmlResult;
}*/

// Apply XSL tranformation (file xsltfile) for XML String or StreamSource
private String applyXSLT(String xsltfile, Object sourceXML, boolean fixSurrogatePairs) throws Exception {
long startMethodTime = System.currentTimeMillis();

Source srcXSL = new StreamSource(getStreamFromResources(getClass().getClassLoader(), xsltfile));
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(srcXSL);
if (fixSurrogatePairs) {
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16");
}

Source src = (sourceXML instanceof StreamSource) ? (StreamSource)sourceXML : new StreamSource(new StringReader((String)sourceXML));

StringWriter resultWriter = new StringWriter();
StreamResult sr = new StreamResult(resultWriter);
transformer.transform(src, sr);
String xmlResult = resultWriter.toString();

printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime, xsltfile);

return xmlResult;
}

// 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 {
long startMethodTime = System.currentTimeMillis();

Source srcXSL = new StreamSource(getStreamFromResources(getClass().getClassLoader(), xsltfile));
TransformerFactory factory = TransformerFactory.newInstance();
Expand All @@ -761,6 +800,8 @@ private String applyXSLTExtended(String xsltfile, StreamSource sourceXML, String
transformer.transform(sourceXML, sr);
String xmlResult = resultWriter.toString();

printProcessingTime(new Object(){}.getClass().getEnclosingMethod(), startMethodTime, xsltfile);

return xmlResult;
}

Expand Down Expand Up @@ -964,24 +1005,35 @@ private void readEncryptionParameters(File fEncryptionParameters) {
}

private void setTablesWidths(fontConfig fontcfg, XSLTconverter xsltConverter, File pdf) {

long startMethodTime = System.currentTimeMillis();
try {
if (isTableExists && xmlTableIF.isEmpty()) {
// generate IF with table width data
xsltConverter.setParam("table_if", "true");
logger.info("[INFO] Generation of XSL-FO with information about the table's widths ...");
// transform XML to XSL-FO (XML .fo file)
startTime = System.currentTimeMillis();
xsltConverter.transform(sourceXMLDocument);

printProcessingTime();
String xmlTablesOnly = "";
try {
xmlTablesOnly = applyXSLT("tables_only.xsl", sourceXMLDocument.getStreamSource(), true);
} catch (Exception ex) {
logger.severe("Can't generate information about tables from Intermediate Format.");
ex.printStackTrace();
}

String xmlFO = sourceXMLDocument.getXMLFO();
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");

fontcfg.setSourceDocumentFontList(sourceXMLDocument.getDocumentFonts());
//debug
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 ...");
Expand Down Expand Up @@ -1009,6 +1061,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);
}

private void debugSaveXML(String xmlString, String pathTo) {
Expand All @@ -1032,10 +1085,11 @@ private void debugSaveXML(String xmlString, String pathTo) {
}
}

private void printProcessingTime() {
private void printProcessingTime(Method method, long startTime, String ... params) {
if (DEBUG) {
long endTime = System.currentTimeMillis();
logger.log(Level.INFO, "processing time: {0} milliseconds", endTime - startTime);
String addon = Arrays.toString(params);
logger.log(Level.INFO, "Method '" + method.getName() + "(" + addon + ")' processing time: {0} milliseconds", endTime - startTime);
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/metanorma/fop/SourceXMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ public SourceXMLDocument(File fXML) {
}
}

public SourceXMLDocument(String strXML) {
try {
this.sourceXMLstr = strXML;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputSource xmlIFIS = new InputSource(new StringReader(strXML));
sourceXML = dBuilder.parse(xmlIFIS);
} catch (Exception ex) {
logger.severe("Can't parse source XML.");
ex.printStackTrace();
}
}

public StreamSource getStreamSource() {
if (sourceXMLstr.isEmpty()) {
try {
Expand Down
Loading

0 comments on commit 7abecb7

Please sign in to comment.