All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.jsonldjava.tools.Playground Maven / Gradle / Ivy
package com.github.jsonldjava.tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import joptsimple.ValueConversionException;
import joptsimple.ValueConverter;
import org.openrdf.model.Model;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParserRegistry;
import org.openrdf.rio.Rio;
import com.github.jsonldjava.core.JsonLdOptions;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.utils.JsonUtils;
public class Playground {
private static Set getProcessingOptions() {
return new LinkedHashSet(Arrays.asList("expand", "compact", "frame", "normalize",
"flatten", "fromrdf", "tordf"));
}
private static boolean hasContext(String opt) {
return "compact".equals(opt) || "frame".equals(opt) || "flatten".equals(opt);
}
private static Map getOutputFormats() {
final Map outputFormats = new HashMap();
for (final RDFFormat format : RDFParserRegistry.getInstance().getKeys()) {
outputFormats.put(format.getName().replaceAll("-", "").replaceAll("/", "")
.toLowerCase(), format);
}
return outputFormats;
}
public static void main(String[] args) throws Exception {
final Map formats = getOutputFormats();
final Set outputForms = new LinkedHashSet(Arrays.asList("compacted",
"expanded", "flattened"));
final OptionParser parser = new OptionParser();
final OptionSpec help = parser.accepts("help").forHelp();
final OptionSpec base = parser.accepts("base").withRequiredArg()
.ofType(String.class).defaultsTo("").describedAs("base URI");
final OptionSpec inputFile = parser.accepts("inputFile").withRequiredArg()
.ofType(File.class).required().describedAs("The input file");
final OptionSpec context = parser.accepts("context").withRequiredArg()
.ofType(File.class).describedAs("The context");
final OptionSpec outputFormat = parser
.accepts("format")
.withOptionalArg()
.ofType(String.class)
.withValuesConvertedBy(new ValueConverter() {
@Override
public RDFFormat convert(String arg0) {
// Normalise the name to provide alternatives
final String formatName = arg0.replaceAll("-", "").replaceAll("/", "")
.toLowerCase();
if (formats.containsKey(formatName)) {
return formats.get(formatName);
}
throw new ValueConversionException("Format was not known: " + arg0
+ " (Valid values are: " + formats.keySet() + ")");
}
@Override
public String valuePattern() {
return null;
}
@Override
public Class valueType() {
return RDFFormat.class;
}
})
.defaultsTo(RDFFormat.NQUADS)
.describedAs(
"The output file format to use. Defaults to nquads. Valid values are: "
+ formats.keySet());
final OptionSpec processingOption = parser
.accepts("process")
.withRequiredArg()
.ofType(String.class)
.required()
.withValuesConvertedBy(new ValueConverter() {
@Override
public String convert(String value) {
if (getProcessingOptions().contains(value.toLowerCase())) {
return value.toLowerCase();
}
throw new ValueConversionException("Processing option was not known: "
+ value + " (Valid values are: " + getProcessingOptions() + ")");
}
@Override
public Class valueType() {
return String.class;
}
@Override
public String valuePattern() {
return null;
}
})
.describedAs(
"The processing to perform. Valid values are: "
+ getProcessingOptions().toString());
final OptionSpec outputForm = parser
.accepts("outputForm")
.withOptionalArg()
.ofType(String.class)
.defaultsTo("expanded")
.withValuesConvertedBy(new ValueConverter() {
@Override
public String convert(String value) {
if (outputForms.contains(value.toLowerCase())) {
return value.toLowerCase();
}
throw new ValueConversionException("Output form was not known: " + value
+ " (Valid values are: " + outputForms + ")");
}
@Override
public String valuePattern() {
return null;
}
@Override
public Class valueType() {
return String.class;
}
})
.describedAs(
"The way to output the results from fromRDF. Defaults to expanded. Valid values are: "
+ outputForms);
OptionSet options = null;
try {
options = parser.parse(args);
} catch (final OptionException e) {
System.out.println(e.getMessage());
parser.printHelpOn(System.out);
throw e;
}
if (options.has(help)) {
parser.printHelpOn(System.out);
return;
}
final JsonLdOptions opts = new JsonLdOptions("");
Object inobj = null;
Object ctxobj = null;
opts.setBase(options.valueOf(base));
opts.outputForm = options.valueOf(outputForm);
opts.format = options.has(outputFormat) ? options.valueOf(outputFormat)
.getDefaultMIMEType() : "application/nquads";
final RDFFormat sesameOutputFormat = options.valueOf(outputFormat);
final RDFFormat sesameInputFormat = Rio.getParserFormatForFileName(
options.valueOf(inputFile).getName(), RDFFormat.JSONLD);
final String processingOptionValue = options.valueOf(processingOption);
if (!options.valueOf(inputFile).exists()) {
System.out.println("Error: input file \"" + options.valueOf(inputFile)
+ "\" doesn't exist");
parser.printHelpOn(System.out);
return;
}
// if base is currently null, set it
if (opts.getBase() == null || opts.getBase().equals("")) {
opts.setBase(options.valueOf(inputFile).toURI().toASCIIString());
}
if ("fromrdf".equals(processingOptionValue)) {
inobj = readFile(options.valueOf(inputFile));
} else {
inobj = JsonUtils.fromInputStream(new FileInputStream(options.valueOf(inputFile)));
}
if (hasContext(processingOptionValue) && options.has(context)) {
if (!options.valueOf(context).exists()) {
System.out.println("Error: context file \"" + options.valueOf(context)
+ "\" doesn't exist");
parser.printHelpOn(System.out);
return;
}
ctxobj = JsonUtils.fromInputStream(new FileInputStream(options.valueOf(context)));
}
Object outobj = null;
if ("fromrdf".equals(processingOptionValue)) {
final Model inModel = Rio.parse(new StringReader((String) inobj), opts.getBase(),
sesameInputFormat);
outobj = JsonLdProcessor.fromRDF(inModel, opts, new SesameJSONLDRDFParser());
} else if ("tordf".equals(processingOptionValue)) {
opts.useNamespaces = true;
outobj = JsonLdProcessor
.toRDF(inobj,
new SesameJSONLDTripleCallback(Rio.createWriter(sesameOutputFormat,
System.out)), opts);
} else if ("expand".equals(processingOptionValue)) {
outobj = JsonLdProcessor.expand(inobj, opts);
} else if ("compact".equals(processingOptionValue)) {
if (ctxobj == null) {
System.out.println("Error: The compaction context must not be null.");
parser.printHelpOn(System.out);
return;
}
outobj = JsonLdProcessor.compact(inobj, ctxobj, opts);
} else if ("normalize".equals(processingOptionValue)) {
outobj = JsonLdProcessor.normalize(inobj, opts);
} else if ("frame".equals(processingOptionValue)) {
if (ctxobj != null && !(ctxobj instanceof Map)) {
System.out
.println("Invalid JSON-LD syntax; a JSON-LD frame must be a single object.");
parser.printHelpOn(System.out);
return;
}
outobj = JsonLdProcessor.frame(inobj, ctxobj, opts);
} else if ("flatten".equals(processingOptionValue)) {
outobj = JsonLdProcessor.flatten(inobj, ctxobj, opts);
} else {
System.out
.println("Error: invalid processing option \"" + processingOptionValue + "\"");
parser.printHelpOn(System.out);
return;
}
if ("tordf".equals(processingOptionValue)) {
// Already serialised above
} else if ("normalize".equals(processingOptionValue)) {
System.out.println((String) outobj);
} else {
System.out.println(JsonUtils.toPrettyString(outobj));
}
}
private static String readFile(File in) throws IOException {
final BufferedReader buf = new BufferedReader(new InputStreamReader(
new FileInputStream(in), "UTF-8"));
String inobj = "";
try {
String line;
while ((line = buf.readLine()) != null) {
line = line.trim();
inobj = (inobj) + line + "\n";
}
} finally {
buf.close();
}
return inobj;
}
// private static void usage() {
// System.out.println("Usage: jsonldplayground ");
// System.out.println("\tinput: a filename or JsonLdUrl to the rdf input (in rdfxml or n3)");
// System.out.println("\toptions:");
// System.out
// .println("\t\t--ignorekeys : a (space separated) list of keys to ignore (e.g. @geojson)");
// System.out.println("\t\t--base : base URI");
// System.out.println("\t\t--debug: Print out stack traces when errors occur");
// System.out.println("\t\t--expand : expand the input JSON-LD");
// System.out
// .println("\t\t--compact : compact the input JSON-LD applying the optional context file");
// System.out
// .println("\t\t--normalize : normalize the input JSON-LD outputting as format (defaults to nquads)");
// System.out
// .println("\t\t--frame : frame the input JSON-LD with the optional frame file");
// System.out
// .println("\t\t--flatten : flatten the input JSON-LD applying the optional context file");
// System.out
// .println("\t\t--fromRDF : generate JSON-LD from the input rdf (format defaults to nquads)");
// System.out
// .println("\t\t--toRDF : generate RDF from the input JSON-LD (format defaults to nquads)");
// System.out
// .println("\t\t--outputForm [compacted|expanded|flattened] : the way to output the results from fromRDF (defaults to expanded)");
// System.out.println("\t\t--simplify : simplify the input JSON-LD");
// System.exit(1);
// }
}