From f41bd253dab73d8e68fba8ca9f14605554ca7a07 Mon Sep 17 00:00:00 2001 From: Filip Date: Fri, 12 Aug 2022 12:46:14 +0200 Subject: [PATCH 1/3] Add keepArrays (de)compressor option --- pom.xml | 4 ++-- src/main/java/com/apicatalog/cli/command/CompactCmd.java | 8 ++++---- .../java/com/apicatalog/cli/command/CompressCmd.java | 4 ++++ .../java/com/apicatalog/cli/command/DecompressCmd.java | 9 +++++++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 22f7d73..3be2630 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 0.8.0-SNAPSHOT jar - Linked Data Command Line Processor + Command Line Processor for Linked Data Processing https://github.com/filip26/ld-cli @@ -208,4 +208,4 @@ - \ No newline at end of file + diff --git a/src/main/java/com/apicatalog/cli/command/CompactCmd.java b/src/main/java/com/apicatalog/cli/command/CompactCmd.java index 15108bd..44e6438 100644 --- a/src/main/java/com/apicatalog/cli/command/CompactCmd.java +++ b/src/main/java/com/apicatalog/cli/command/CompactCmd.java @@ -50,10 +50,10 @@ public final class CompactCmd implements Callable { boolean ordered = false; @Option(names = { "-a", "--keep-arrays" }, description = "keep arrays with just one element") - boolean compactArrays = true; + boolean keepArrays = false; @Option(names = { "-r", "--keep-iris" }, description = "keep absolute IRIs") - boolean compactToRelative = true; + boolean keepAbsoluteIris = false; @Spec CommandSpec spec; @@ -78,8 +78,8 @@ public Integer call() throws Exception { api.base(base); api.ordered(ordered); - api.compactArrays(compactArrays); - api.compactToRelative(compactToRelative); + api.compactArrays(!keepArrays); + api.compactToRelative(!keepAbsoluteIris); final JsonObject output = api.get(); diff --git a/src/main/java/com/apicatalog/cli/command/CompressCmd.java b/src/main/java/com/apicatalog/cli/command/CompressCmd.java index b0072d4..fa372d6 100644 --- a/src/main/java/com/apicatalog/cli/command/CompressCmd.java +++ b/src/main/java/com/apicatalog/cli/command/CompressCmd.java @@ -42,6 +42,9 @@ public final class CompressCmd implements Callable { @Option(names = { "-b", "--base" }, description = "input document base IRI") URI base = null; + @Option(names = { "-a", "--keep-arrays" }, description = "keep arrays with just one element") + boolean keepArrays = false; + @Spec CommandSpec spec; @@ -68,6 +71,7 @@ public Integer call() throws Exception { final byte[] encoded = CborLd.encoder(json.asJsonObject()) .base(base) + .compactArray(!keepArrays) .encode(); try (FileOutputStream os = new FileOutputStream(output)) { diff --git a/src/main/java/com/apicatalog/cli/command/DecompressCmd.java b/src/main/java/com/apicatalog/cli/command/DecompressCmd.java index cb965ab..a7ab133 100644 --- a/src/main/java/com/apicatalog/cli/command/DecompressCmd.java +++ b/src/main/java/com/apicatalog/cli/command/DecompressCmd.java @@ -7,7 +7,6 @@ import com.apicatalog.cborld.CborLd; import com.apicatalog.cli.JsonOutput; -import com.apicatalog.jsonld.document.Document; import jakarta.json.JsonStructure; import jakarta.json.JsonValue; @@ -40,6 +39,9 @@ public final class DecompressCmd implements Callable { @Option(names = { "-b", "--base" }, description = "input document base IRI") URI base = null; + @Option(names = { "-a", "--keep-arrays" }, description = "keep arrays with just one element") + boolean keepArrays = false; + @Spec CommandSpec spec; @@ -50,7 +52,10 @@ public Integer call() throws Exception { byte[] encoded = Files.readAllBytes(input.toPath()); - final JsonValue output = CborLd.decoder(encoded).base(base).decode(); + final JsonValue output = CborLd.decoder(encoded) + .compactArray(!keepArrays) + .base(base) + .decode(); JsonOutput.print((JsonStructure)output, pretty); From f3e0800c9e95c5249b48bcd5c360351488e1ea50 Mon Sep 17 00:00:00 2001 From: Filip Date: Fri, 12 Aug 2022 12:54:34 +0200 Subject: [PATCH 2/3] Add (de)compressor processing mode option --- .../com/apicatalog/cli/command/CompressCmd.java | 14 ++++++++++++++ .../com/apicatalog/cli/command/DecompressCmd.java | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/apicatalog/cli/command/CompressCmd.java b/src/main/java/com/apicatalog/cli/command/CompressCmd.java index fa372d6..f94e72a 100644 --- a/src/main/java/com/apicatalog/cli/command/CompressCmd.java +++ b/src/main/java/com/apicatalog/cli/command/CompressCmd.java @@ -5,6 +5,9 @@ import java.util.concurrent.Callable; import com.apicatalog.cborld.CborLd; +import com.apicatalog.cborld.config.DefaultConfig; +import com.apicatalog.cborld.db.DbConfig; +import com.apicatalog.cborld.encoder.EncoderConfig; import com.apicatalog.jsonld.document.Document; import com.apicatalog.jsonld.document.JsonDocument; import com.apicatalog.jsonld.json.JsonUtils; @@ -45,6 +48,9 @@ public final class CompressCmd implements Callable { @Option(names = { "-a", "--keep-arrays" }, description = "keep arrays with just one element") boolean keepArrays = false; + @Option(names = { "-m", "--mode" }, description = "processing mode", paramLabel = "default|digitalbazaar") + String mode = "default"; + @Spec CommandSpec spec; @@ -68,11 +74,19 @@ public Integer call() throws Exception { if (JsonUtils.isNotObject(json)) { throw new IllegalArgumentException("The input docunent root is not JSON object but [" + json.getValueType() + "]."); } + + EncoderConfig config = DefaultConfig.INSTANCE; + if ("digitalbazaar".equalsIgnoreCase(mode)) { + config = DbConfig.INSTANCE; + } + final byte[] encoded = CborLd.encoder(json.asJsonObject()) + .config(config) .base(base) .compactArray(!keepArrays) .encode(); + try (FileOutputStream os = new FileOutputStream(output)) { os.write(encoded); diff --git a/src/main/java/com/apicatalog/cli/command/DecompressCmd.java b/src/main/java/com/apicatalog/cli/command/DecompressCmd.java index a7ab133..4b49fc0 100644 --- a/src/main/java/com/apicatalog/cli/command/DecompressCmd.java +++ b/src/main/java/com/apicatalog/cli/command/DecompressCmd.java @@ -6,6 +6,9 @@ import java.util.concurrent.Callable; import com.apicatalog.cborld.CborLd; +import com.apicatalog.cborld.config.DefaultConfig; +import com.apicatalog.cborld.db.DbConfig; +import com.apicatalog.cborld.decoder.DecoderConfig; import com.apicatalog.cli.JsonOutput; import jakarta.json.JsonStructure; @@ -42,6 +45,9 @@ public final class DecompressCmd implements Callable { @Option(names = { "-a", "--keep-arrays" }, description = "keep arrays with just one element") boolean keepArrays = false; + @Option(names = { "-m", "--mode" }, description = "processing mode", paramLabel = "default|digitalbazaar") + String mode = "default"; + @Spec CommandSpec spec; @@ -52,9 +58,16 @@ public Integer call() throws Exception { byte[] encoded = Files.readAllBytes(input.toPath()); + DecoderConfig config = DefaultConfig.INSTANCE; + + if ("digitalbazaar".equalsIgnoreCase(mode)) { + config = DbConfig.INSTANCE; + } + final JsonValue output = CborLd.decoder(encoded) - .compactArray(!keepArrays) + .config(config) .base(base) + .compactArray(!keepArrays) .decode(); JsonOutput.print((JsonStructure)output, pretty); From 2c38fa76022d5a0fd8647ba46065263f3438f100 Mon Sep 17 00:00:00 2001 From: Filip Date: Fri, 12 Aug 2022 13:08:01 +0200 Subject: [PATCH 3/3] Fix typos --- README.md | 4 ++-- src/main/java/com/apicatalog/cli/command/FlattenCmd.java | 2 +- src/main/java/com/apicatalog/cli/command/FromRdfCmd.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5b882ac..1e00aaf 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,9 @@ Options: Commands: expand Expand JSON-LD document compact Compact JSON-LD document using the context - flatten Flatten JSON-LD document and optionally compacts it using a context + flatten Flatten JSON-LD document and optionally compact it using a context frame Frame JSON-LD document using the frame - fromrdf Transform N-Quads document into a JSON-LD document in expanded form + fromrdf Transform N-Quads document into a JSON-LD document in an expanded form tordf Transform JSON-LD document into N-Quads document compress Compress JSON-LD document into CBOR-LD decompress Decompress CBOR-LD document into JSON-LD diff --git a/src/main/java/com/apicatalog/cli/command/FlattenCmd.java b/src/main/java/com/apicatalog/cli/command/FlattenCmd.java index 3e3023a..225b6cd 100644 --- a/src/main/java/com/apicatalog/cli/command/FlattenCmd.java +++ b/src/main/java/com/apicatalog/cli/command/FlattenCmd.java @@ -18,7 +18,7 @@ @Command( name = "flatten", mixinStandardHelpOptions = false, - description = "Flatten JSON-LD document and optionally compacts it using a context", + description = "Flatten JSON-LD document and optionally compact it using a context", sortOptions = true, descriptionHeading = "%n", parameterListHeading = "%nParameters:%n", diff --git a/src/main/java/com/apicatalog/cli/command/FromRdfCmd.java b/src/main/java/com/apicatalog/cli/command/FromRdfCmd.java index 3feca3c..7ed63f7 100644 --- a/src/main/java/com/apicatalog/cli/command/FromRdfCmd.java +++ b/src/main/java/com/apicatalog/cli/command/FromRdfCmd.java @@ -18,7 +18,7 @@ @Command( name = "fromrdf", mixinStandardHelpOptions = false, - description = "Transform N-Quads document into a JSON-LD document in expanded form", + description = "Transform N-Quads document into a JSON-LD document in an expanded form", sortOptions = true, descriptionHeading = "%n", parameterListHeading = "%nParameters:%n",