|
32 | 32 | */
|
33 | 33 | package org.openjdk.jmc.utils.releasenotes;
|
34 | 34 |
|
35 |
| -import com.sun.org.apache.xalan.internal.xslt.Process; |
| 35 | +import static java.nio.file.Files.exists; |
| 36 | +import static java.nio.file.Files.newInputStream; |
| 37 | +import static java.nio.file.Files.newOutputStream; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.io.InputStream; |
| 41 | +import java.io.OutputStream; |
| 42 | +import java.nio.file.Path; |
| 43 | +import java.nio.file.Paths; |
| 44 | +import java.util.ArrayDeque; |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.Deque; |
| 47 | + |
| 48 | +import javax.xml.transform.TransformerException; |
| 49 | +import javax.xml.transform.TransformerFactory; |
| 50 | +import javax.xml.transform.stream.StreamResult; |
| 51 | +import javax.xml.transform.stream.StreamSource; |
36 | 52 |
|
37 | 53 | /**
|
38 | 54 | * Wrapper for Xalan to do an XSLT. Used to generate the release notes HTML.
|
39 | 55 | * <p>
|
40 | 56 | * Example: Transform -IN notes.xml -XSL stylesheet.xsl -OUT new_and_noteworthy.html
|
41 | 57 | */
|
42 | 58 | public class Transform {
|
| 59 | + |
43 | 60 | public static void main(String[] args) {
|
44 |
| - Process._main(args); |
| 61 | + final Deque<String> deque = new ArrayDeque<>(Arrays.asList(args)); |
| 62 | + Path inputFile = null; |
| 63 | + Path outputFile = null; |
| 64 | + Path sylesheetFile = null; |
| 65 | + while (!deque.isEmpty()) { |
| 66 | + switch (deque.poll()) { |
| 67 | + case "-IN": |
| 68 | + inputFile = checkedToPath(deque.poll(), "input file", true); |
| 69 | + break; |
| 70 | + case "-OUT": |
| 71 | + outputFile = checkedToPath(deque.poll(), "output file", false); |
| 72 | + break; |
| 73 | + case "-XSL": |
| 74 | + sylesheetFile = checkedToPath(deque.poll(), "stylesheet file", true); |
| 75 | + break; |
| 76 | + default: |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + if (inputFile != null && outputFile != null && sylesheetFile != null) { |
| 81 | + try { |
| 82 | + transform(inputFile, outputFile, sylesheetFile); |
| 83 | + System.exit(0); |
| 84 | + } catch (IOException | TransformerException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + System.exit(1); |
| 87 | + } |
| 88 | + } else { |
| 89 | + usage(); |
| 90 | + System.exit(1); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static void usage() { |
| 95 | + System.out.println("Usage: Transform -IN <input file> -XSL <stylesheet file> -OUT <output file>"); |
| 96 | + } |
| 97 | + |
| 98 | + private static Path checkedToPath(String arg, String fileDescription, boolean isInputFile) { |
| 99 | + if (arg == null) { |
| 100 | + System.err.format("%s not given", fileDescription).println(); |
| 101 | + } else { |
| 102 | + Path file = Paths.get(arg); |
| 103 | + if (isInputFile && exists(file)) { |
| 104 | + return file; |
| 105 | + } else { |
| 106 | + System.err.format("%s '%s' not found", fileDescription, file).println(); |
| 107 | + } |
| 108 | + } |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + private static void transform(Path inputFile, Path outputFile, Path sylesheetFile) |
| 113 | + throws IOException, TransformerException { |
| 114 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 115 | + try (InputStream xslIn = newInputStream(sylesheetFile); |
| 116 | + InputStream in = newInputStream(inputFile); |
| 117 | + OutputStream out = newOutputStream(outputFile)) { |
| 118 | + transformerFactory.newTransformer(new StreamSource(xslIn)).transform(new StreamSource(in), |
| 119 | + new StreamResult(out)); |
| 120 | + } |
45 | 121 | }
|
46 | 122 | }
|
0 commit comments