Aspose.TeX's input interface | Java
Please, refer to Aspose.TeX for Java API reference for formal definitions of I/O implementation.
The file system and ZIP examples typeset hello-world.tex to hello-world.xps. Set inputDirectory to a folder containing the TeX file and outputDirectory to an existing output folder. For ZIP input, set inputZip to a java.nio.file.Path pointing to an archive containing in/hello-world.tex. Import the Aspose.TeX classes used in the selected snippet, com.aspose.tex.rendering.XpsDevice, and the relevant java.io and java.nio.file classes. The calling method must handle or declare exceptions. Apply a license as described in the
licensing guide to avoid evaluation restrictions.
The concept of the input directory
Since I/O primitives of the TeX language can only deal with file names, Aspose.TeX for Java defines a directory as a mapping between names and bulks of data. The bulks of data are supposed to be files, streams, arrays, or whatever else.The API allows us to specify the input and output working directories separately. It provides the general IInputWorkingDirectory interface, which the user can implement for their own purposes. It also provides its own implementations, which will be discussed below. The interface defines the getFile() method, which returns the data stream and determines the full name of the file, while taking some, hypothetically different, name as the first argument, which, in fact, is the mapping key.
Getting file input from the disk file system
Here’s how we would do it:
1// inputDirectory contains hello-world.tex; outputDirectory is an existing output folder.
2TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
3InputFileSystemDirectory input = new InputFileSystemDirectory(inputDirectory);
4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
5options.setInputWorkingDirectory(input);
6options.setOutputWorkingDirectory(output);
7options.setInteraction(Interaction.NonstopMode);
8options.setTerminalOut(new OutputMemoryTerminal());
9
10XpsDevice device = new XpsDevice();
11TeXJobResult result;
12try {
13 result = new TeXJob("hello-world", device, options).run();
14} finally {
15 device.dispose();
16 output.close();
17 input.close();
18}This use case is quite simple, so we won’t dwell on it for a long time.
Getting file input from a ZIP archive
We can also put the input files in a ZIP archive and consider it an input directory. In this case, we should proceed as follows:
1// Read hello-world.tex from the in/ folder of a ZIP archive.
2// inputZip is a java.nio.file.Path; outputDirectory is an existing output folder.
3TeXJobResult result;
4try (InputStream zipStream = Files.newInputStream(inputZip)) {
5 TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
6 InputZipDirectory input = new InputZipDirectory(zipStream, "in");
7 OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
8 options.setInputWorkingDirectory(input);
9 options.setOutputWorkingDirectory(output);
10 options.setInteraction(Interaction.NonstopMode);
11 options.setTerminalOut(new OutputMemoryTerminal());
12 XpsDevice device = new XpsDevice();
13 try {
14 result = new TeXJob("hello-world", device, options).run();
15 } finally {
16 device.dispose();
17 output.close();
18 input.close();
19 }
20}First, we create the stream containing the ZIP file. Then, after creating the conversion options, we set the InputWorkingDirectory option to be an instance of the InputZipDirectory class. The second argument of the constructor is the base path inside the archive. If we want the entire archive to be an input directory, we should pass the empty string. The example keeps the ZIP stream and input directory open until the TeX job finishes, then closes them. It uses noninteractive execution and captures terminal output in memory.
The concept of the input terminal
Now let’s remember that there is also the terminal input. As for this one, Aspose.TeX for Java defines the general IInputTerminal interface as having two methods. One of them returns a BufferedReader instance. Another method returns an InputStream as the underlying stream. Provided implementations of the interface are discussed below.
Getting terminal input from the console
To do this, we need to set the TerminalIn option to be an instance of the InputConsoleTerminal class.
1// Use the console for terminal input; this is also the default.
2TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
3options.setTerminalIn(new InputConsoleTerminal());InputConsoleTerminal is the default value of this option. This snippet only demonstrates terminal configuration; it does not run a job. A job that requests terminal input needs input from the user or a custom terminal implementation. The file system and ZIP examples above use noninteractive execution with complete input files.
Theoretically, if we have an interactive TeX file (or script) with predictable behavior, we may want to implement a version of the input terminal that would contain a complementary script for responding to the TeX engine’s requests. Try your hand when you have time!