Skip to content

Commit

Permalink
Merge pull request #97 from filip26/fix/cborld
Browse files Browse the repository at this point in the history
Fix/cborld
  • Loading branch information
filip26 authored Jul 26, 2024
2 parents 3a4e638 + 7cfc85a commit 87edc7e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/executables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: graalvm/setup-graalvm@v1
with:
version: 'latest'
java-version: '17'
java-version: '21'
components: 'native-image'
github-token: ${{ secrets.GITHUB_TOKEN }}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v1
with:
java-version: 17
java-version: 21
- name: Build with Maven
env:
GITHUB_ACTOR: ${{ github.actor }}
Expand Down
22 changes: 15 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.apicatalog</groupId>
<artifactId>ld-cli</artifactId>
<version>0.8.0</version>
<version>0.9.0-SNAPSHOT</version>
<packaging>jar</packaging>

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

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

<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -26,7 +28,7 @@
<command.name>ld-cli</command.name>

<titanium.version>1.4.1</titanium.version>
<iridium.version>0.1.3</iridium.version>
<iridium.version>0.2.2</iridium.version>
<jakarta.json-p.version>2.0.1</jakarta.json-p.version>

<graal.version>24.0.2</graal.version>
Expand Down Expand Up @@ -95,7 +97,6 @@
<version>2.16.1</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -177,7 +178,14 @@
<configuration>
<imageName>${command.name}</imageName>
<mainClass>${mainClass}</mainClass>
<buildArgs>&#45;&#45;no-server &#45;&#45;no-fallback &#45;&#45;report-unsupported-elements-at-runtime &#45;&#45;allow-incomplete-classpath &#45;H:ReflectionConfigurationFiles=${project.basedir}/graal.json &#45;H:+ReportExceptionStackTraces &#45;H:+AddAllCharsets &#45;H:IncludeResourceBundles=org.glassfish.json.messages &#45;H:IncludeResources=.*/.*jsonld$</buildArgs>
<buildArgs>&#45;&#45;no-server &#45;&#45;no-fallback
&#45;&#45;report-unsupported-elements-at-runtime
&#45;&#45;allow-incomplete-classpath
&#45;H:ReflectionConfigurationFiles=${project.basedir}/graal.json
&#45;H:+ReportExceptionStackTraces
&#45;H:+AddAllCharsets
&#45;H:IncludeResourceBundles=org.glassfish.json.messages
&#45;H:IncludeResources=.*/.*jsonld$</buildArgs>
</configuration>
<executions>
<execution>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/apicatalog/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
optionListHeading = "%nOptions:%n",
commandListHeading = "%nCommands:%n",
version = {
"ld-cli 0.8.0 https://github.com/filip26/ld-cli",
"titanium-json-ld 1.3.1 https://github.com/filip26/titanium-json-ld",
"iridium-cbor-ld 0.1.1 https://github.com/filip26/iridium-cbor-ld",
"ld-cli 0.9.0 https://github.com/filip26/ld-cli",
"titanium-json-ld 1.4.1 https://github.com/filip26/titanium-json-ld",
"iridium-cbor-ld 0.2.0 https://github.com/filip26/iridium-cbor-ld",
}
)
public final class App {
Expand All @@ -49,7 +49,7 @@ public final class App {
boolean version;

static {
((HttpLoader) HttpLoader.defaultInstance()).setFallbackContentType(MediaType.JSON);
((HttpLoader) HttpLoader.defaultInstance()).fallbackContentType(MediaType.JSON);
}

public static void main(String[] args) {
Expand Down
58 changes: 26 additions & 32 deletions src/main/java/com/apicatalog/cli/command/CompressCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import java.util.concurrent.Callable;

import com.apicatalog.cborld.CborLd;
import com.apicatalog.cborld.barcode.BarcodesConfig;
import com.apicatalog.cborld.config.DefaultConfig;
import com.apicatalog.cborld.db.DbConfig;
import com.apicatalog.cborld.config.V05Config;
import com.apicatalog.cborld.encoder.EncoderConfig;
import com.apicatalog.jsonld.document.Document;
import com.apicatalog.jsonld.document.JsonDocument;
Expand All @@ -22,15 +23,7 @@
import picocli.CommandLine.Parameters;
import picocli.CommandLine.Spec;

@Command(
name = "compress",
mixinStandardHelpOptions = false,
description = "Compress JSON-LD document into CBOR-LD",
sortOptions = true,
descriptionHeading = "%n",
parameterListHeading = "%nParameters:%n",
optionListHeading = "%nOptions:%n"
)
@Command(name = "compress", mixinStandardHelpOptions = false, description = "Compress JSON-LD document into CBOR-LD", sortOptions = true, descriptionHeading = "%n", parameterListHeading = "%nParameters:%n", optionListHeading = "%nOptions:%n")
public final class CompressCmd implements Callable<Integer> {

@Option(names = { "-h", "--help" }, hidden = true, usageHelp = true)
Expand All @@ -39,22 +32,23 @@ public final class CompressCmd implements Callable<Integer> {
@Option(names = { "-i", "--input" }, description = "input document IRI")
URI input = null;

@Parameters(index = "0", arity = "1", description = "output document filename" )
@Parameters(index = "0", arity = "1", description = "output document filename")
String output = null;

@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")

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

@Spec
CommandSpec spec;

private CompressCmd() {}
private CompressCmd() {
}

@Override
public Integer call() throws Exception {
Expand All @@ -64,35 +58,35 @@ public Integer call() throws Exception {
if (input != null) {
final DocumentLoader loader = SchemeRouter.defaultInstance();
document = loader.loadDocument(input, new DocumentLoaderOptions());

} else {
document = JsonDocument.of(System.in);
document = JsonDocument.of(System.in);
}

final JsonStructure json = document.getJsonContent().orElseThrow(() -> new IllegalArgumentException("Invalid input document. JSON document expected but got [" + document.getContentType() + "]."));

final JsonStructure json = document.getJsonContent()
.orElseThrow(() -> new IllegalArgumentException("Invalid input document. JSON document expected but got [" + document.getContentType() + "]."));

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 EncoderConfig config = switch (mode) {
case "barcodes" -> BarcodesConfig.INSTANCE;
case "v05" -> V05Config.INSTANCE;
default -> DefaultConfig.INSTANCE;
};

final byte[] encoded = CborLd.encoder(json.asJsonObject())
.config(config)
.base(base)
.compactArray(!keepArrays)
.encode();

final byte[] encoded = CborLd.createEncoder(config)
.base(base)
.compactArray(!keepArrays)
.build()
.encode(json.asJsonObject());

try (FileOutputStream os = new FileOutputStream(output)) {
os.write(encoded);
os.flush();
}
return spec.exitCodeOnSuccess();

return spec.exitCodeOnSuccess();
}
}
52 changes: 23 additions & 29 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,9 @@
import java.util.concurrent.Callable;

import com.apicatalog.cborld.CborLd;
import com.apicatalog.cborld.barcode.BarcodesConfig;
import com.apicatalog.cborld.config.DefaultConfig;
import com.apicatalog.cborld.db.DbConfig;
import com.apicatalog.cborld.config.V05Config;
import com.apicatalog.cborld.decoder.DecoderConfig;
import com.apicatalog.cli.JsonOutput;

Expand All @@ -19,15 +20,7 @@
import picocli.CommandLine.Parameters;
import picocli.CommandLine.Spec;

@Command(
name = "decompress",
mixinStandardHelpOptions = false,
description = "Decompress CBOR-LD document into JSON-LD",
sortOptions = true,
descriptionHeading = "%n",
parameterListHeading = "%nParameters:%n",
optionListHeading = "%nOptions:%n"
)
@Command(name = "decompress", mixinStandardHelpOptions = false, description = "Decompress CBOR-LD document into JSON-LD", sortOptions = true, descriptionHeading = "%n", parameterListHeading = "%nParameters:%n", optionListHeading = "%nOptions:%n")
public final class DecompressCmd implements Callable<Integer> {

@Option(names = { "-h", "--help" }, hidden = true, usageHelp = true)
Expand All @@ -41,36 +34,37 @@ 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")

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

@Spec
CommandSpec spec;

private DecompressCmd() {}
private DecompressCmd() {
}

@Override
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)
.config(config)
.base(base)
.compactArray(!keepArrays)
.decode();

JsonOutput.print((JsonStructure)output, pretty);
final byte[] encoded = Files.readAllBytes(input.toPath());

final DecoderConfig config = switch (mode) {
case "barcodes" -> BarcodesConfig.INSTANCE;
case "v05" -> V05Config.INSTANCE;
default -> DefaultConfig.INSTANCE;
};

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

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

return spec.exitCodeOnSuccess();
}
Expand Down

0 comments on commit 87edc7e

Please sign in to comment.