|
1 | 1 | package org.ruleml.translation.ruleml2tptp;
|
2 | 2 |
|
3 |
| -import org.kohsuke.args4j.Argument; |
4 |
| -import org.kohsuke.args4j.CmdLineException; |
5 |
| -import org.kohsuke.args4j.CmdLineParser; |
6 |
| -import org.kohsuke.args4j.Option; |
7 |
| -import java.io.File; |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedOutputStream; |
8 | 5 | import java.io.FileInputStream;
|
| 6 | +import java.io.FileNotFoundException; |
9 | 7 | import java.io.FileOutputStream;
|
10 |
| -import java.io.InputStream; |
11 | 8 | import java.io.IOException;
|
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.InputStreamReader; |
12 | 11 | import java.io.OutputStream;
|
| 12 | +import java.io.Reader; |
13 | 13 | import java.io.StringReader;
|
14 | 14 | import java.io.StringWriter;
|
15 |
| -import java.util.ArrayList; |
16 |
| -import java.util.List; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Properties; |
17 | 18 | import javax.xml.transform.TransformerException;
|
18 | 19 | import javax.xml.transform.TransformerFactoryConfigurationError;
|
19 | 20 | import javax.xml.transform.sax.SAXTransformerFactory;
|
20 |
| -import javax.xml.transform.stream.StreamSource; |
21 | 21 | import javax.xml.transform.stream.StreamResult;
|
| 22 | +import javax.xml.transform.stream.StreamSource; |
| 23 | +import net.sourceforge.argparse4j.ArgumentParsers; |
| 24 | +import net.sourceforge.argparse4j.impl.Arguments; |
| 25 | +import net.sourceforge.argparse4j.inf.Argument; |
| 26 | +import net.sourceforge.argparse4j.inf.ArgumentAction; |
| 27 | +import net.sourceforge.argparse4j.inf.ArgumentParser; |
| 28 | +import net.sourceforge.argparse4j.inf.ArgumentParserException; |
| 29 | +import net.sourceforge.argparse4j.inf.ArgumentType; |
| 30 | +import net.sourceforge.argparse4j.inf.Namespace; |
22 | 31 |
|
23 | 32 | /**
|
24 | 33 | * @author [email protected] (Meng Luan)
|
25 | 34 | */
|
26 | 35 | public class Main {
|
27 | 36 |
|
28 |
| - private static final String dummyXML = "<?xml version='1.0'?><blank/>"; |
29 |
| - private static final String xsltProperties = String.format( |
30 |
| - "<?xml version='1.0'?>" |
31 |
| - + "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" |
32 |
| - + " <xsl:output method='text'/>" |
33 |
| - + " <xsl:template match='/'>" |
34 |
| - + " <xsl:text> xsl:version = </xsl:text>" |
35 |
| - + " <xsl:value-of select=\"system-property('xsl:version')\"/>" |
36 |
| - + " <xsl:text>%n xsl:vendor = </xsl:text>" |
37 |
| - + " <xsl:value-of select=\"system-property('xsl:vendor')\"/>" |
38 |
| - + " <xsl:text>%n xsl:vendor-url = </xsl:text>" |
39 |
| - + " <xsl:value-of select=\"system-property('xsl:vendor-url')\"/>" |
40 |
| - + " </xsl:template>" |
41 |
| - + "</xsl:stylesheet>"); |
42 |
| - |
43 |
| - private static final int EC_GENERAL = 1; |
44 |
| - private static final int EC_TRANSFORM = 2; |
45 |
| - |
46 |
| - @Option(name="-h",aliases={"-?","-help"},help=true,usage="print this message") |
47 |
| - private boolean help; |
48 |
| - |
49 |
| - @Option(name="-o",aliases={"-output"},metaVar="<path>",usage="use given output path (the standard output by default)") |
50 |
| - private File output; |
51 |
| - |
52 |
| - @Option(name="-w",aliases={"-overwrite"},usage="overwrite the output file") |
53 |
| - private boolean overwrite; |
54 |
| - |
55 |
| - @Argument |
56 |
| - private String input; |
57 |
| - |
58 |
| - private SAXTransformerFactory transFactory; |
59 |
| - |
60 |
| - public static void main(String args[]) { |
61 |
| - final Main appMain = new Main(); |
62 |
| - CmdLineParser parser = new CmdLineParser(appMain); |
63 |
| - try { |
64 |
| - parser.parseArgument(args); |
65 |
| - } catch (CmdLineException ex) { |
66 |
| - System.err.println(ex.getLocalizedMessage() + "."); |
67 |
| - System.err.println("Try option \"-h\" for usage."); |
68 |
| - System.err.println(); |
69 |
| - System.exit(EC_GENERAL); |
70 |
| - } |
71 |
| - try { |
72 |
| - appMain.transFactory = |
73 |
| - (SAXTransformerFactory) (SAXTransformerFactory.newInstance()); |
74 |
| - } catch (TransformerFactoryConfigurationError err) { |
75 |
| - throw new IllegalStateException(err); |
76 |
| - } |
77 |
| - if (appMain.help) { |
78 |
| - System.out.println("Usage: [options] <path>"); |
79 |
| - System.out.println(); |
80 |
| - System.out.println("Options:"); |
81 |
| - parser.printUsage(System.out); |
82 |
| - System.out.println(); |
83 |
| - final StringWriter noteWriter = new StringWriter(); |
84 |
| - try { |
85 |
| - appMain.transFactory.newTransformer(new StreamSource(new StringReader(xsltProperties))) |
86 |
| - .transform(new StreamSource(new StringReader(dummyXML)), new StreamResult(noteWriter)); |
87 |
| - } catch (TransformerException ex) { |
88 |
| - throw new IllegalStateException(ex); |
89 |
| - } |
90 |
| - System.out.println("XSLT Properties:"); |
91 |
| - System.out.println(noteWriter.toString()); |
92 |
| - System.out.println(); |
93 |
| - return; |
94 |
| - } |
95 |
| - appMain.run(); |
| 37 | + private static final String DUMMY_XML = "<?xml version='1.0'?><blank/>"; |
| 38 | + private static final String XSLT_PROPERTIES = String.format( |
| 39 | + "<?xml version='1.0'?>" |
| 40 | + + "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" |
| 41 | + + " <xsl:output method='text'/>" |
| 42 | + + " <xsl:template match='/'>" |
| 43 | + + " <xsl:text>xsl:vendor = </xsl:text>" |
| 44 | + + " <xsl:value-of select=\"system-property('xsl:vendor')\"/>" |
| 45 | + + " <xsl:text>%nxsl:vendor-url = </xsl:text>" |
| 46 | + + " <xsl:value-of select=\"system-property('xsl:vendor-url')\"/>" |
| 47 | + + " <xsl:text>%nxsl:version = </xsl:text>" |
| 48 | + + " <xsl:value-of select=\"system-property('xsl:version')\"/>" |
| 49 | + + " </xsl:template>" |
| 50 | + + "</xsl:stylesheet>"); |
| 51 | + |
| 52 | + private static final int EC_GENERAL = 1; |
| 53 | + private static final int EC_TRANSFORM = 2; |
| 54 | + private static final Properties PROPERTIES = new Properties(); |
| 55 | + private static SAXTransformerFactory transFactory; |
| 56 | + |
| 57 | + public static void main(final String args[]) throws IOException { |
| 58 | + try (final Reader reader |
| 59 | + = new InputStreamReader(Main.class.getResourceAsStream("application.properties"), StandardCharsets.UTF_8)) { |
| 60 | + PROPERTIES.load(reader); |
| 61 | + } |
| 62 | + try { |
| 63 | + transFactory = (SAXTransformerFactory) (SAXTransformerFactory.newInstance()); |
| 64 | + } catch (TransformerFactoryConfigurationError err) { |
| 65 | + throw new IllegalStateException(err); |
| 66 | + } |
| 67 | + final Namespace opts = parseArgs(args); |
| 68 | + final Translator translator = new Translator(transFactory); |
| 69 | + translator.loadTemplates(); |
| 70 | + try { |
| 71 | + translate(translator, getInput(opts), getOutput(opts)); |
| 72 | + } catch (TransformerException ex) { |
| 73 | + System.err.println(ex.getMessageAndLocation()); |
| 74 | + System.exit(EC_TRANSFORM); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static class XSLTVersionAction implements ArgumentAction { |
| 79 | + |
| 80 | + @Override |
| 81 | + public void run(final ArgumentParser parser, final Argument arg, final Map<String, Object> attrs, final String flag, |
| 82 | + final Object value) throws ArgumentParserException { |
| 83 | + try { |
| 84 | + final StringWriter writer = new StringWriter(); |
| 85 | + transFactory.newTransformer(new StreamSource(new StringReader(XSLT_PROPERTIES))) |
| 86 | + .transform(new StreamSource(new StringReader(DUMMY_XML)), new StreamResult(writer)); |
| 87 | + System.out.println(writer.toString()); |
| 88 | + } catch (final TransformerException ex) { |
| 89 | + throw new IllegalStateException(ex); |
| 90 | + } |
| 91 | + System.exit(0); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onAttach(final Argument arg) { |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean consumeArgument() { |
| 100 | + return false; |
96 | 101 | }
|
| 102 | + } |
97 | 103 |
|
98 |
| - private void run() { |
99 |
| - final Translator translator = new Translator(transFactory); |
100 |
| - translator.loadTemplates(); |
101 |
| - try { |
102 |
| - if (output != null && !overwrite && !output.createNewFile()) { |
103 |
| - System.err.println("The output file has existed. Use option \"-w\" to overwrite it."); |
104 |
| - System.exit(EC_GENERAL); |
105 |
| - } |
106 |
| - if (input == null) { |
107 |
| - if (output == null) { |
108 |
| - translate(translator, System.in, System.out); |
109 |
| - } else { |
110 |
| - translate(translator, System.in, new FileOutputStream(output)); |
111 |
| - } |
112 |
| - } else if (output == null) { |
113 |
| - translate(translator, new FileInputStream(input), System.out); |
114 |
| - } else { |
115 |
| - translate(translator, new FileInputStream(input), new FileOutputStream(output)); |
116 |
| - } |
117 |
| - |
118 |
| - } catch (TransformerException ex) { |
119 |
| - System.err.println(ex.getMessageAndLocation()); |
120 |
| - System.err.println(); |
121 |
| - System.exit(EC_TRANSFORM); |
122 |
| - } catch (IOException ex) { |
123 |
| - System.err.println("Failed to operate file: " + ex.getLocalizedMessage()); |
124 |
| - System.err.println(); |
125 |
| - System.exit(EC_TRANSFORM); |
126 |
| - } |
| 104 | + private static class InputStreamType implements ArgumentType<InputStream> { |
| 105 | + |
| 106 | + @Override |
| 107 | + public InputStream convert(final ArgumentParser parser, final Argument arg, final String value) |
| 108 | + throws ArgumentParserException { |
| 109 | + try { |
| 110 | + return new BufferedInputStream(new FileInputStream(value)); |
| 111 | + } catch (final FileNotFoundException | SecurityException e) { |
| 112 | + throw new ArgumentParserException("Couldn't read input file: " + value, e, parser, arg); |
| 113 | + } |
127 | 114 | }
|
| 115 | + } |
| 116 | + |
| 117 | + private static class OutputStreamType implements ArgumentType<OutputStream> { |
128 | 118 |
|
129 |
| - private void translate(Translator translator, InputStream in, OutputStream out) |
130 |
| - throws TransformerException { |
131 |
| - translator.translate(new StreamSource(in), new StreamResult(out)); |
| 119 | + @Override |
| 120 | + public OutputStream convert(final ArgumentParser parser, final Argument arg, final String value) |
| 121 | + throws ArgumentParserException { |
| 122 | + try { |
| 123 | + return new BufferedOutputStream(new FileOutputStream(value)); |
| 124 | + } catch (final FileNotFoundException | SecurityException e) { |
| 125 | + throw new ArgumentParserException("Couldn't write to output file: " + value, e, parser, arg); |
| 126 | + } |
132 | 127 | }
|
| 128 | + } |
| 129 | + |
| 130 | + private static InputStream getInput(final Namespace opts) { |
| 131 | + final InputStream in = opts.get("input"); |
| 132 | + return in == null ? System.in : in; |
| 133 | + } |
| 134 | + |
| 135 | + private static OutputStream getOutput(final Namespace opts) { |
| 136 | + final OutputStream out = opts.get("output"); |
| 137 | + return out == null ? System.out : out; |
| 138 | + } |
| 139 | + |
| 140 | + private static Namespace parseArgs(final String[] args) { |
| 141 | + final ArgumentParser parser = ArgumentParsers.newArgumentParser("ruleml2tptp", true) |
| 142 | + .description("Translate RuleML into TPTP.") |
| 143 | + .version(PROPERTIES.getProperty("application.name") + " " + PROPERTIES.getProperty("application.version")) |
| 144 | + .defaultHelp(true); |
| 145 | + parser.addArgument("--version").action(Arguments.version()).help("show version and exit"); |
| 146 | + parser.addArgument("--xslt-vendor").action(new XSLTVersionAction()).help("show XSLT vendor and exit"); |
| 147 | + parser.addArgument("input").metavar("<input>").nargs("?").type(new InputStreamType()) |
| 148 | + .help("input file path or standard input if not specified"); |
| 149 | + parser.addArgument("-o", "--output").metavar("<output>").type(new OutputStreamType()) |
| 150 | + .help("output file path or standard output if not specified"); |
| 151 | + return parser.parseArgsOrFail(args); |
| 152 | + } |
133 | 153 |
|
| 154 | + private static void translate(Translator translator, InputStream in, OutputStream out) throws TransformerException { |
| 155 | + translator.translate(new StreamSource(in), new StreamResult(out)); |
| 156 | + } |
134 | 157 | }
|
0 commit comments