Skip to content

Commit 222d00b

Browse files
committed
Cleanup
Signed-off-by: Peter Ansell <p_ansell@yahoo.com>
1 parent 5174f1f commit 222d00b

File tree

4 files changed

+66
-88
lines changed

4 files changed

+66
-88
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
<packaging>jar</packaging>
1515

1616
<properties>
17-
<rdf4j.version>2.3.2</rdf4j.version>
17+
<rdf4j.version>2.4.1</rdf4j.version>
1818
</properties>
1919

2020
<dependencies>
2121
<dependency>
2222
<groupId>${project.groupId}</groupId>
2323
<artifactId>jsonld-java</artifactId>
24-
<version>0.12.1</version>
24+
<version>0.12.2-SNAPSHOT</version>
2525
</dependency>
2626
<dependency>
2727
<groupId>junit</groupId>

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

Lines changed: 60 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,12 @@
2424
import javax.net.ssl.X509ExtendedTrustManager;
2525
import javax.net.ssl.X509TrustManager;
2626

27-
import joptsimple.OptionException;
28-
import joptsimple.OptionParser;
29-
import joptsimple.OptionSet;
30-
import joptsimple.OptionSpec;
31-
import joptsimple.ValueConversionException;
32-
import joptsimple.ValueConverter;
33-
3427
import org.apache.http.auth.AuthScope;
3528
import org.apache.http.auth.UsernamePasswordCredentials;
3629
import org.apache.http.client.CredentialsProvider;
3730
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
3831
import org.apache.http.impl.client.BasicCredentialsProvider;
39-
import org.apache.http.impl.client.CloseableHttpClient;
32+
import org.apache.http.impl.client.HttpClientBuilder;
4033
import org.apache.http.impl.client.cache.BasicHttpCacheStorage;
4134
import org.apache.http.impl.client.cache.CacheConfig;
4235
import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
@@ -46,14 +39,21 @@
4639
import org.eclipse.rdf4j.rio.Rio;
4740

4841
import com.github.jsonldjava.core.DocumentLoader;
49-
import com.github.jsonldjava.core.JsonLdConsts;
5042
import com.github.jsonldjava.core.JsonLdApi;
43+
import com.github.jsonldjava.core.JsonLdConsts;
5144
import com.github.jsonldjava.core.JsonLdOptions;
5245
import com.github.jsonldjava.core.JsonLdProcessor;
5346
import com.github.jsonldjava.core.RDFDataset;
5447
import com.github.jsonldjava.utils.JarCacheStorage;
5548
import com.github.jsonldjava.utils.JsonUtils;
5649

50+
import joptsimple.OptionException;
51+
import joptsimple.OptionParser;
52+
import joptsimple.OptionSet;
53+
import joptsimple.OptionSpec;
54+
import joptsimple.ValueConversionException;
55+
import joptsimple.ValueConverter;
56+
5757
/**
5858
* A command-line-interface used to load and process JSON-LD and RDF files.
5959
*/
@@ -81,14 +81,14 @@ public static void main(String[] args) throws Exception {
8181
final OptionSpec<RDFFormat> outputFormat = parser.accepts("format").withOptionalArg()
8282
.ofType(String.class).withValuesConvertedBy(new ValueConverter<RDFFormat>() {
8383
@Override
84-
public RDFFormat convert(String arg0) {
84+
public RDFFormat convert(String inputFormat) {
8585
// Normalise the name to provide alternatives
86-
final String formatName = arg0.replaceAll("-", "").replaceAll("/", "")
87-
.toLowerCase();
86+
final String formatName = inputFormat.replaceAll("-", "")
87+
.replaceAll("/", "").toLowerCase();
8888
if (formats.containsKey(formatName)) {
8989
return formats.get(formatName);
9090
}
91-
throw new ValueConversionException("Format was not known: " + arg0
91+
throw new ValueConversionException("Format was not known: " + inputFormat
9292
+ " (Valid values are: " + formats.keySet() + ")");
9393
}
9494

@@ -158,7 +158,8 @@ public Class<String> valueType() {
158158
.ofType(String.class).describedAs("username for basic authentication credentials");
159159

160160
final OptionSpec<String> passwordOption = parser.accepts("password").withOptionalArg()
161-
.ofType(String.class).describedAs("password for basic authentication credentials (defaults to value of 'PASSWORD' environment property, if set, or empty string otherwise)");
161+
.ofType(String.class).describedAs(
162+
"password for basic authentication credentials (defaults to value of 'PASSWORD' environment property, if set, or empty string otherwise)");
162163

163164
final OptionSpec<String> authHostOption = parser.accepts("authHost").withOptionalArg()
164165
.ofType(String.class).defaultsTo("localhost")
@@ -168,7 +169,8 @@ public Class<String> valueType() {
168169
.ofType(Integer.class).defaultsTo(443)
169170
.describedAs("host port authentication scope");
170171

171-
final OptionSpec<Void> authInsecureOption = parser.accepts("insecure","Similar to `curl -k` or `curl --insecure`: if unspecified, all SSL connections are secure by default; if specified, trust everything (do not use for production!)");
172+
final OptionSpec<Void> authInsecureOption = parser.accepts("insecure",
173+
"Similar to `curl -k` or `curl --insecure`: if unspecified, all SSL connections are secure by default; if specified, trust everything (do not use for production!)");
172174

173175
OptionSet options = null;
174176

@@ -223,71 +225,38 @@ public Class<String> valueType() {
223225
final DocumentLoader documentLoader = new DocumentLoader();
224226

225227
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
226-
credsProvider.setCredentials(
227-
new AuthScope(authHost, authPort),
228+
credsProvider.setCredentials(new AuthScope(authHost, authPort),
228229
new UsernamePasswordCredentials(username, password));
229230

230-
final CacheConfig cacheConfig = CacheConfig.custom()
231-
.setMaxCacheEntries(1000)
231+
final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000)
232232
.setMaxObjectSize(1024 * 128).build();
233233

234-
if (options.has(authInsecureOption)) {
234+
HttpClientBuilder builder = CachingHttpClientBuilder.create()
235+
// allow caching
236+
.setCacheConfig(cacheConfig)
237+
// Wrap the local JarCacheStorage around a
238+
// BasicHttpCacheStorage
239+
.setHttpCacheStorage(new JarCacheStorage(null, cacheConfig,
240+
new BasicHttpCacheStorage(cacheConfig)))
241+
.setDefaultCredentialsProvider(credsProvider);
235242

243+
if (options.has(authInsecureOption)) {
236244
final SSLContext ctx = SSLContext.getInstance("TLS");
237-
final X509TrustManager tm = new InsecureX509TrustManager();
238-
ctx.init(null, new TrustManager[] { tm }, null);
239-
245+
ctx.init(null, new TrustManager[] { new InsecureX509TrustManager() }, null);
240246
final HostnameVerifier v = new HostnameVerifier() {
241-
242247
@Override
243248
public boolean verify(String s, SSLSession sslSession) {
244249
return true;
245250
}
246251
};
247252

248253
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(ctx, v);
249-
250-
final CloseableHttpClient httpClient = CachingHttpClientBuilder
251-
.create()
252-
// allow caching
253-
.setCacheConfig(cacheConfig)
254-
// Wrap the local JarCacheStorage around a BasicHttpCacheStorage
255-
.setHttpCacheStorage(
256-
new JarCacheStorage(null, cacheConfig, new BasicHttpCacheStorage(
257-
cacheConfig)))
258-
259-
// Add in the credentials provider
260-
.setDefaultCredentialsProvider(credsProvider)
261-
262-
// insecure ssl connections
263-
.setSSLSocketFactory(sslsf)
264-
265-
// When you are finished setting the properties, call build
266-
.build();
267-
documentLoader.setHttpClient(httpClient);
268-
opts.setDocumentLoader(documentLoader);
269-
270-
} else {
271-
272-
final CloseableHttpClient httpClient = CachingHttpClientBuilder
273-
.create()
274-
// allow caching
275-
.setCacheConfig(cacheConfig)
276-
// Wrap the local JarCacheStorage around a BasicHttpCacheStorage
277-
.setHttpCacheStorage(
278-
new JarCacheStorage(null, cacheConfig, new BasicHttpCacheStorage(
279-
cacheConfig)))
280-
281-
// Add in the credentials provider
282-
.setDefaultCredentialsProvider(credsProvider)
283-
284-
// When you are finished setting the properties, call build
285-
.build();
286-
287-
documentLoader.setHttpClient(httpClient);
288-
opts.setDocumentLoader(documentLoader);
289-
254+
// insecure ssl connections
255+
builder = builder.setSSLSocketFactory(sslsf);
290256
}
257+
258+
documentLoader.setHttpClient(builder.build());
259+
opts.setDocumentLoader(documentLoader);
291260
}
292261

293262
if ("fromrdf".equals(processingOptionValue)) {
@@ -331,18 +300,20 @@ public boolean verify(String s, SSLSession sslSession) {
331300
// outobj = JsonLdProcessor.normalize(inobj, opts);
332301

333302
// see https://github.com/jsonld-java/jsonld-java/issues/194
334-
// until this is fixed, it is necessary to clear the format so that JsonLdProcessor won't try to interpret it.
303+
// until this is fixed, it is necessary to clear the format so that
304+
// JsonLdProcessor won't try to interpret it.
335305
opts.format = null;
336306

337-
// If an output format is specified, add a callback to show the result.
338-
Object result = JsonLdProcessor.toRDF(
339-
inobj,
307+
// If an output format is specified, add a callback to show the
308+
// result.
309+
final Object result = JsonLdProcessor.toRDF(inobj,
340310
options.has(outputFormat)
341-
? new RDF4JJSONLDTripleCallback(Rio.createWriter(sesameOutputFormat, System.out))
311+
? new RDF4JJSONLDTripleCallback(
312+
Rio.createWriter(sesameOutputFormat, System.out))
342313
: null,
343314
opts);
344315
if (RDFDataset.class.isInstance(result)) {
345-
RDFDataset rdfds = RDFDataset.class.cast(result);
316+
final RDFDataset rdfds = RDFDataset.class.cast(result);
346317
outobj = new JsonLdApi(opts).normalize(rdfds);
347318
} else {
348319
outobj = result;
@@ -397,45 +368,52 @@ private static Map<String, RDFFormat> getOutputFormats() {
397368
}
398369

399370
private static String readFile(File in) throws IOException {
400-
String inobj = "";
401-
try (BufferedReader buf = Files.newBufferedReader(in.toPath(), StandardCharsets.UTF_8)){
371+
final StringBuilder inobj = new StringBuilder(1024);
372+
try (BufferedReader buf = Files.newBufferedReader(in.toPath(), StandardCharsets.UTF_8)) {
402373
String line;
403374
while ((line = buf.readLine()) != null) {
404375
line = line.trim();
405-
inobj = (inobj) + line + "\n";
376+
inobj.append(line).append('\n');
406377
}
407378
}
408-
return inobj;
379+
return inobj.toString();
409380
}
410381

411-
private static class InsecureX509TrustManager extends X509ExtendedTrustManager implements X509TrustManager {
382+
private static class InsecureX509TrustManager extends X509ExtendedTrustManager
383+
implements X509TrustManager {
412384

385+
@Override
413386
public void checkClientTrusted(X509Certificate[] xcs, String string) {
414387
}
415388

416389
@Override
417-
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
390+
public void checkServerTrusted(X509Certificate[] xcs, String string)
391+
throws CertificateException {
418392
}
419393

394+
@Override
420395
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
421396
return null;
422397
}
423398

424-
425399
@Override
426-
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
400+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket)
401+
throws CertificateException {
427402
}
428403

429404
@Override
430-
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
405+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket)
406+
throws CertificateException {
431407
}
432408

433409
@Override
434-
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
410+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s,
411+
SSLEngine sslEngine) throws CertificateException {
435412
}
436413

437414
@Override
438-
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
415+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s,
416+
SSLEngine sslEngine) throws CertificateException {
439417
}
440418
}
441419

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import java.util.Set;
55

66
import org.eclipse.rdf4j.model.BNode;
7+
import org.eclipse.rdf4j.model.IRI;
78
import org.eclipse.rdf4j.model.Literal;
89
import org.eclipse.rdf4j.model.Model;
910
import org.eclipse.rdf4j.model.Namespace;
1011
import org.eclipse.rdf4j.model.Resource;
1112
import org.eclipse.rdf4j.model.Statement;
12-
import org.eclipse.rdf4j.model.IRI;
1313
import org.eclipse.rdf4j.model.Value;
1414
import org.eclipse.rdf4j.model.vocabulary.RDF;
1515
import org.eclipse.rdf4j.model.vocabulary.XMLSchema;
@@ -18,7 +18,7 @@
1818
import com.github.jsonldjava.core.RDFDataset;
1919

2020
/**
21-
* Implementation of RDFParser for RDF4J-2.2.
21+
* Implementation of RDFParser for RDF4J-2.
2222
*
2323
* @author Peter Ansell
2424
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import java.util.List;
44
import java.util.Map.Entry;
55

6+
import org.eclipse.rdf4j.model.IRI;
67
import org.eclipse.rdf4j.model.Resource;
78
import org.eclipse.rdf4j.model.Statement;
8-
import org.eclipse.rdf4j.model.IRI;
99
import org.eclipse.rdf4j.model.Value;
1010
import org.eclipse.rdf4j.model.ValueFactory;
1111
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
@@ -23,7 +23,7 @@
2323
import com.github.jsonldjava.core.RDFDataset;
2424

2525
/**
26-
* Implementation of JsonLdTripleCallback for RDF4J-2.2.
26+
* Implementation of JsonLdTripleCallback for RDF4J-2.
2727
*
2828
* @author Peter Ansell
2929
*/

0 commit comments

Comments
 (0)