Skip to content

Commit 0fd1857

Browse files
committed
fix version
1 parent a01a622 commit 0fd1857

File tree

3 files changed

+125
-108
lines changed

3 files changed

+125
-108
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ See the [entry page](http://ruleml.github.io/RuleML2TPTP/) of the project for mo
22

33
### Getting Started
44

5-
1. This project is built using [maven](http://maven.apache.org/).
6-
2. Translate a RuleML file by calling "java -jar /path/to/ruleml2tptp.jar <source> -o <output>". Use option "-h" for a brief usage.
7-
3. This project needs JDK 1.7 or higher to compile the Java code.
5+
1. This project is built using [maven](http://maven.apache.org/);
6+
2. Use option "--help" for a brief usage;
7+
3. Translate a RuleML file by calling "java -jar /path/to/ruleml2tptp.jar <input> -o <output>";
8+
4. This project needs JDK 1.7 or higher to compile the Java code.

pom.xml

+5-9
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,15 @@
9898
<resources>
9999
<resource>
100100
<directory>src/main/resources</directory>
101-
<filtering>true</filtering>
102-
<includes>
103-
<include>*.properties</include>
104-
</includes>
101+
<filtering>false</filtering>
105102
</resource>
106103
<resource>
107104
<directory>src/main/resources</directory>
108-
<filtering>false</filtering>
109-
<excludes>
110-
<exclude>*.properties</exclude>
111-
</excludes>
105+
<filtering>true</filtering>
106+
<includes>
107+
<include>**/*.properties</include>
108+
</includes>
112109
</resource>
113110
</resources>
114111
</build>
115-
116112
</project>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.ruleml.translation.ruleml2tptp;
22

3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
35
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
47
import java.io.FileOutputStream;
58
import java.io.IOException;
69
import java.io.InputStream;
@@ -10,126 +13,143 @@
1013
import java.io.StringReader;
1114
import java.io.StringWriter;
1215
import java.nio.charset.StandardCharsets;
16+
import java.util.Map;
1317
import java.util.Properties;
1418
import javax.xml.transform.TransformerException;
1519
import javax.xml.transform.TransformerFactoryConfigurationError;
1620
import javax.xml.transform.sax.SAXTransformerFactory;
1721
import javax.xml.transform.stream.StreamResult;
1822
import javax.xml.transform.stream.StreamSource;
1923
import net.sourceforge.argparse4j.ArgumentParsers;
20-
import net.sourceforge.argparse4j.annotation.Arg;
2124
import net.sourceforge.argparse4j.impl.Arguments;
25+
import net.sourceforge.argparse4j.inf.Argument;
26+
import net.sourceforge.argparse4j.inf.ArgumentAction;
2227
import net.sourceforge.argparse4j.inf.ArgumentParser;
2328
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;
2531

2632
/**
2733
* @author [email protected] (Meng Luan)
2834
*/
2935
public class Main {
3036

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>");
3251

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;
3556

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+
}
3893

39-
@Arg(dest = "xslt_version")
40-
public boolean xsltVersion;
94+
@Override
95+
public void onAttach(Argument arg) {
4196
}
4297

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;
98101
}
102+
}
99103

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+
}
129113
}
114+
}
115+
116+
private static class OutputStreamType implements ArgumentType<OutputStream> {
130117

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+
}
134125
}
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+
}
135155
}

0 commit comments

Comments
 (0)