Skip to content

Commit b982d46

Browse files
committed
Cleanup in Playground
Signed-off-by: Peter Ansell <p_ansell@yahoo.com>
1 parent e735084 commit b982d46

File tree

1 file changed

+61
-96
lines changed

1 file changed

+61
-96
lines changed

src/main/java/com/github/jsonldjava/tools/Playground.java

Lines changed: 61 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import java.io.File;
55
import java.io.FileInputStream;
66
import java.io.IOException;
7-
import java.io.InputStreamReader;
87
import java.io.StringReader;
98
import java.net.Socket;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
1011
import java.security.cert.CertificateException;
1112
import java.security.cert.X509Certificate;
1213
import java.util.Arrays;
@@ -53,59 +54,11 @@
5354
import com.github.jsonldjava.utils.JarCacheStorage;
5455
import com.github.jsonldjava.utils.JsonUtils;
5556

57+
/**
58+
* A command-line-interface used to load and process JSON-LD and RDF files.
59+
*/
5660
public class Playground {
5761

58-
private static Set<String> getProcessingOptions() {
59-
return new LinkedHashSet<String>(Arrays.asList("expand", "compact", "frame", "normalize",
60-
"flatten", "fromrdf", "tordf"));
61-
}
62-
63-
private static boolean hasContext(String opt) {
64-
return "compact".equals(opt) || "frame".equals(opt) || "flatten".equals(opt);
65-
}
66-
67-
private static Map<String, RDFFormat> getOutputFormats() {
68-
final Map<String, RDFFormat> outputFormats = new HashMap<String, RDFFormat>();
69-
70-
for (final RDFFormat format : RDFParserRegistry.getInstance().getKeys()) {
71-
outputFormats.put(
72-
format.getName().replaceAll("-", "").replaceAll("/", "").toLowerCase(), format);
73-
}
74-
75-
return outputFormats;
76-
}
77-
78-
private static class InsecureX509TrustManager extends X509ExtendedTrustManager implements X509TrustManager {
79-
80-
public void checkClientTrusted(X509Certificate[] xcs, String string) {
81-
}
82-
83-
@Override
84-
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
85-
}
86-
87-
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
88-
return null;
89-
}
90-
91-
92-
@Override
93-
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
94-
}
95-
96-
@Override
97-
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
98-
}
99-
100-
@Override
101-
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
102-
}
103-
104-
@Override
105-
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
106-
}
107-
}
108-
10962
public static void main(String[] args) throws Exception {
11063

11164
final Map<String, RDFFormat> formats = getOutputFormats();
@@ -391,9 +344,9 @@ public boolean verify(String s, SSLSession sslSession) {
391344
if (RDFDataset.class.isInstance(result)) {
392345
RDFDataset rdfds = RDFDataset.class.cast(result);
393346
outobj = new JsonLdApi(opts).normalize(rdfds);
394-
} else
347+
} else {
395348
outobj = result;
396-
349+
}
397350
} else if ("frame".equals(processingOptionValue)) {
398351
if (ctxobj != null && !(ctxobj instanceof Map)) {
399352
System.out.println(
@@ -414,64 +367,76 @@ public boolean verify(String s, SSLSession sslSession) {
414367
if ("tordf".equals(processingOptionValue)) {
415368
// Already serialised above
416369
} else if ("normalize".equals(processingOptionValue)) {
417-
if (!options.has(outputFormat))
370+
if (!options.has(outputFormat)) {
418371
// if no output format was specified, then show the result.
419372
System.out.println(JsonUtils.toPrettyString(outobj));
373+
}
420374
} else {
421375
System.out.println(JsonUtils.toPrettyString(outobj));
422376
}
423377
}
424378

379+
private static Set<String> getProcessingOptions() {
380+
return new LinkedHashSet<String>(Arrays.asList("expand", "compact", "frame", "normalize",
381+
"flatten", "fromrdf", "tordf"));
382+
}
383+
384+
private static boolean hasContext(String opt) {
385+
return "compact".equals(opt) || "frame".equals(opt) || "flatten".equals(opt);
386+
}
387+
388+
private static Map<String, RDFFormat> getOutputFormats() {
389+
final Map<String, RDFFormat> outputFormats = new HashMap<String, RDFFormat>();
390+
391+
for (final RDFFormat format : RDFParserRegistry.getInstance().getKeys()) {
392+
outputFormats.put(
393+
format.getName().replaceAll("-", "").replaceAll("/", "").toLowerCase(), format);
394+
}
395+
396+
return outputFormats;
397+
}
398+
425399
private static String readFile(File in) throws IOException {
426-
final BufferedReader buf = new BufferedReader(
427-
new InputStreamReader(new FileInputStream(in), "UTF-8"));
428400
String inobj = "";
429-
try {
401+
try (BufferedReader buf = Files.newBufferedReader(in.toPath(), StandardCharsets.UTF_8)){
430402
String line;
431403
while ((line = buf.readLine()) != null) {
432404
line = line.trim();
433405
inobj = (inobj) + line + "\n";
434406
}
435-
} finally {
436-
buf.close();
437407
}
438408
return inobj;
439409
}
440410

441-
// private static void usage() {
442-
// System.out.println("Usage: jsonldplayground <options>");
443-
// System.out.println("\tinput: a filename or JsonLdUrl to the rdf input (in
444-
// rdfxml or n3)");
445-
// System.out.println("\toptions:");
446-
// System.out
447-
// .println("\t\t--ignorekeys <keys to ignore> : a (space separated) list of
448-
// keys to ignore (e.g. @geojson)");
449-
// System.out.println("\t\t--base <uri>: base URI");
450-
// System.out.println("\t\t--debug: Print out stack traces when errors
451-
// occur");
452-
// System.out.println("\t\t--expand <input>: expand the input JSON-LD");
453-
// System.out
454-
// .println("\t\t--compact <input> <context> : compact the input JSON-LD
455-
// applying the optional context file");
456-
// System.out
457-
// .println("\t\t--normalize <input> <format> : normalize the input JSON-LD
458-
// outputting as format (defaults to nquads)");
459-
// System.out
460-
// .println("\t\t--frame <input> <frame> : frame the input JSON-LD with the
461-
// optional frame file");
462-
// System.out
463-
// .println("\t\t--flatten <input> <context> : flatten the input JSON-LD
464-
// applying the optional context file");
465-
// System.out
466-
// .println("\t\t--fromRDF <input> <format> : generate JSON-LD from the
467-
// input rdf (format defaults to nquads)");
468-
// System.out
469-
// .println("\t\t--toRDF <input> <format> : generate RDF from the input
470-
// JSON-LD (format defaults to nquads)");
471-
// System.out
472-
// .println("\t\t--outputForm [compacted|expanded|flattened] : the way to
473-
// output the results from fromRDF (defaults to expanded)");
474-
// System.out.println("\t\t--simplify : simplify the input JSON-LD");
475-
// System.exit(1);
476-
// }
411+
private static class InsecureX509TrustManager extends X509ExtendedTrustManager implements X509TrustManager {
412+
413+
public void checkClientTrusted(X509Certificate[] xcs, String string) {
414+
}
415+
416+
@Override
417+
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
418+
}
419+
420+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
421+
return null;
422+
}
423+
424+
425+
@Override
426+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
427+
}
428+
429+
@Override
430+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
431+
}
432+
433+
@Override
434+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
435+
}
436+
437+
@Override
438+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
439+
}
440+
}
441+
477442
}

0 commit comments

Comments
 (0)