Skip to content

Commit

Permalink
Merge pull request #21 from filip26/feat/cborld
Browse files Browse the repository at this point in the history
Complete basic CBOR-LD support
  • Loading branch information
filip26 authored Aug 12, 2022
2 parents 6067595 + 2c38fa7 commit 4e59868
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<packaging>jar</packaging>

<name>A Command Line Processor for Linked Data Processing</name>

<url>https://github.com/filip26/ld-cli</url>

<properties>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/apicatalog/cli/command/CompactCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public final class CompactCmd implements Callable<Integer> {
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;
Expand All @@ -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();

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/apicatalog/cli/command/CompressCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +45,12 @@ public final class CompressCmd implements Callable<Integer> {
@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;

@Option(names = { "-m", "--mode" }, description = "processing mode", paramLabel = "default|digitalbazaar")
String mode = "default";

@Spec
CommandSpec spec;

Expand All @@ -65,10 +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);
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/apicatalog/cli/command/DecompressCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
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 com.apicatalog.jsonld.document.Document;

import jakarta.json.JsonStructure;
import jakarta.json.JsonValue;
Expand Down Expand Up @@ -40,6 +42,12 @@ public final class DecompressCmd implements Callable<Integer> {
@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;

@Option(names = { "-m", "--mode" }, description = "processing mode", paramLabel = "default|digitalbazaar")
String mode = "default";

@Spec
CommandSpec spec;

Expand All @@ -50,7 +58,17 @@ public Integer call() throws Exception {

byte[] encoded = Files.readAllBytes(input.toPath());

final JsonValue output = CborLd.decoder(encoded).base(base).decode();
DecoderConfig config = DefaultConfig.INSTANCE;

if ("digitalbazaar".equalsIgnoreCase(mode)) {
config = DbConfig.INSTANCE;
}

final JsonValue output = CborLd.decoder(encoded)
.config(config)
.base(base)
.compactArray(!keepArrays)
.decode();

JsonOutput.print((JsonStructure)output, pretty);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/apicatalog/cli/command/FlattenCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/apicatalog/cli/command/FromRdfCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4e59868

Please sign in to comment.