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