Skip to content

Commit 6f15e65

Browse files
authored
Merge pull request #16 from RuleML/dev
Ignore Data in a list
2 parents 7b07180 + 1a67af6 commit 6f15e65

File tree

10 files changed

+286
-223
lines changed

10 files changed

+286
-223
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

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<version>2.0.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

10-
<name>ruleml2tptp</name>
10+
<name>RuleML2TPTP</name>
1111
<url>https://github.com/RuleML/RuleML2TPTP</url>
1212

1313
<properties>
@@ -16,9 +16,9 @@
1616

1717
<dependencies>
1818
<dependency>
19-
<groupId>args4j</groupId>
20-
<artifactId>args4j</artifactId>
21-
<version>2.33</version>
19+
<groupId>net.sourceforge.argparse4j</groupId>
20+
<artifactId>argparse4j</artifactId>
21+
<version>0.7.0</version>
2222
<scope>compile</scope>
2323
</dependency>
2424
<dependency>
@@ -95,6 +95,18 @@
9595
</executions>
9696
</plugin>
9797
</plugins>
98+
<resources>
99+
<resource>
100+
<directory>src/main/resources</directory>
101+
<filtering>false</filtering>
102+
</resource>
103+
<resource>
104+
<directory>src/main/resources</directory>
105+
<filtering>true</filtering>
106+
<includes>
107+
<include>**/*.properties</include>
108+
</includes>
109+
</resource>
110+
</resources>
98111
</build>
99-
100112
</project>
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,157 @@
11
package org.ruleml.translation.ruleml2tptp;
22

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;
85
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
97
import java.io.FileOutputStream;
10-
import java.io.InputStream;
118
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
1211
import java.io.OutputStream;
12+
import java.io.Reader;
1313
import java.io.StringReader;
1414
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;
1718
import javax.xml.transform.TransformerException;
1819
import javax.xml.transform.TransformerFactoryConfigurationError;
1920
import javax.xml.transform.sax.SAXTransformerFactory;
20-
import javax.xml.transform.stream.StreamSource;
2121
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;
2231

2332
/**
2433
* @author [email protected] (Meng Luan)
2534
*/
2635
public class Main {
2736

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;
96101
}
102+
}
97103

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+
}
127114
}
115+
}
116+
117+
private static class OutputStreamType implements ArgumentType<OutputStream> {
128118

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+
}
132127
}
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+
}
133153

154+
private static void translate(Translator translator, InputStream in, OutputStream out) throws TransformerException {
155+
translator.translate(new StreamSource(in), new StreamResult(out));
156+
}
134157
}

0 commit comments

Comments
 (0)