78 lines
2.5 KiB
Java
78 lines
2.5 KiB
Java
package JSON2RDF;
|
|
|
|
import java.io.*;
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.parser.JSONParser;
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
import org.apache.jena.riot.system.StreamRDF;
|
|
import org.apache.jena.riot.system.StreamRDFLib;
|
|
import picocli.CommandLine;
|
|
|
|
@CommandLine.Command(name = "json2rdf")
|
|
public class JSON2RDF {
|
|
private final InputStream jsonIn;
|
|
private final OutputStream rdfOut;
|
|
|
|
|
|
@CommandLine.Parameters(paramLabel = "https://localhost/" , index = "0", description = "Base URI of the RDF output data")
|
|
private URI baseURI;
|
|
|
|
{
|
|
try {
|
|
baseURI = new URI("https://localhost/");
|
|
} catch (URISyntaxException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
// @CommandLine.Parameters(paramLabel = "D:\\WORK\\GGD\\src\\main\\java\\Data", description = "json file")
|
|
@CommandLine.Option(names = { "--input-charset" }, description = "Input charset (default: ${DEFAULT-VALUE})")
|
|
private final Charset inputCharset = StandardCharsets.UTF_8;
|
|
|
|
@CommandLine.Option(names = { "--output-charset" }, description = "Output charset (default: ${DEFAULT-VALUE})")
|
|
private final Charset outputCharset = StandardCharsets.UTF_8;
|
|
|
|
public static void main(String[] args) throws IOException
|
|
{
|
|
JSON2RDF json2rdf = new JSON2RDF(System.in, System.out);
|
|
|
|
try
|
|
{
|
|
CommandLine.ParseResult parseResult = new CommandLine(json2rdf).parseArgs(args);
|
|
if (!CommandLine.printHelpIfRequested(parseResult)) json2rdf.convert();
|
|
}
|
|
catch (CommandLine.ParameterException ex)
|
|
{ // command line arguments could not be parsed
|
|
System.err.println(ex.getMessage());
|
|
ex.getCommandLine().usage(System.err);
|
|
}
|
|
}
|
|
|
|
public JSON2RDF(InputStream csvIn, OutputStream rdfOut) {
|
|
|
|
|
|
this.jsonIn = csvIn;
|
|
this.rdfOut = rdfOut;
|
|
}
|
|
|
|
|
|
public void convert() throws IOException {
|
|
if (jsonIn.available() == 0) throw new IllegalStateException("JSON input not provided");
|
|
|
|
try (Reader reader = new BufferedReader(new InputStreamReader(jsonIn, inputCharset)))
|
|
{
|
|
StreamRDF rdfStream = StreamRDFLib.writer(new BufferedWriter(new OutputStreamWriter(rdfOut, outputCharset)));
|
|
new JsonStreamRDFWriter(reader, rdfStream, baseURI.toString()).convert();
|
|
}
|
|
}
|
|
|
|
}
|
|
|