-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLatexRenderJob.java
116 lines (92 loc) · 3.16 KB
/
LatexRenderJob.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package de.vsfexperts.latex.renderer;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Optional;
import java.util.Scanner;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.vsfexperts.latex.storage.Archive;
import de.vsfexperts.latex.storage.LocalFileSystemStorage;
/**
* Renders a LaTeX file.
*/
class LatexRenderJob implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(LatexRenderJob.class);
private final UUID id = UUID.randomUUID();
private final String template;
private final LocalFileSystemStorage storage;
private final Archive archive;
public LatexRenderJob(final LocalFileSystemStorage storage, final Archive archive, final String template) {
this.template = template;
this.storage = storage;
this.archive = archive;
}
@Override
public void run() {
try {
runInternal();
} catch (final LatexRenderingException e) {
throw e;
} catch (final Exception e) {
throw new LatexRenderingException(e);
} finally {
cleanupRenderOutput();
}
}
private void runInternal() throws IOException, InterruptedException {
final File texDirectory = storage.getBaseDirectory(id);
final File texFile = storage.createFile(id, id + ".tex", template);
renderPdf(texDirectory.getAbsolutePath(), texFile);
renderPdf(texDirectory.getAbsolutePath(), texFile);
archiveSourceDocument(texFile);
archivePdf();
}
private void renderPdf(final String texDirectoryPath, final File texFile) throws IOException, InterruptedException {
final Process pdfRenderer = buildRenderProcess(texDirectoryPath, texFile).start();
new Thread(new ProcessOutputLogger(pdfRenderer)).start();
pdfRenderer.waitFor();
}
private ProcessBuilder buildRenderProcess(final String texDirectoryPath, final File texFile) {
final String executable = "pdflatex";
final String batchModeSwitch = "-interaction=nonstopmode";
final String stopOnErrorSwitch = "-halt-on-error";
final String outputDirectory = "-output-directory=" + texDirectoryPath;
final String auxDirectory = "-aux-directory=" + texDirectoryPath;
final ProcessBuilder processBuilder = new ProcessBuilder(executable, batchModeSwitch, stopOnErrorSwitch,
outputDirectory, auxDirectory, texFile.getAbsolutePath());
processBuilder.redirectErrorStream(true);
processBuilder.directory(new File(texDirectoryPath));
return processBuilder;
}
private void cleanupRenderOutput() {
storage.delete(id);
}
private void archiveSourceDocument(final File texFile) {
archive.store(id, texFile);
}
private void archivePdf() {
final Optional<File> pdfFile = storage.getFile(id, id + ".pdf");
if (pdfFile.isPresent()) {
archive.store(id, pdfFile.get());
}
}
public UUID getId() {
return id;
}
private static class ProcessOutputLogger implements Runnable {
private final Process process;
ProcessOutputLogger(final Process process) {
this.process = process;
}
@Override
public void run() {
try (Scanner processOutput = new Scanner(new InputStreamReader(process.getInputStream()))) {
while (processOutput.hasNextLine()) {
LOG.trace(processOutput.nextLine());
}
}
}
}
}