-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CBOR-LD custom dictionaries (#102)
- Loading branch information
Showing
10 changed files
with
154 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ buildNumber.properties | |
*.swp | ||
|
||
/out.cborld | ||
/o.cborld | ||
/ex.jsonld |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.apicatalog.cli; | ||
|
||
public class JsonCborConfig { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.apicatalog.cli; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import com.apicatalog.cborld.document.DocumentDictionary; | ||
import com.apicatalog.cborld.document.DocumentDictionaryBuilder; | ||
import com.apicatalog.jsonld.JsonLdError; | ||
import com.apicatalog.jsonld.document.Document; | ||
import com.apicatalog.jsonld.loader.DocumentLoader; | ||
import com.apicatalog.jsonld.loader.DocumentLoaderOptions; | ||
import com.apicatalog.jsonld.loader.SchemeRouter; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonNumber; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.stream.JsonParser; | ||
|
||
public class JsonCborDictionary { | ||
|
||
public static DocumentDictionary of(URI input) throws IOException, JsonLdError { | ||
if (input.isAbsolute()) { | ||
final DocumentLoader loader = SchemeRouter.defaultInstance(); | ||
return of(loader.loadDocument(input, new DocumentLoaderOptions())); | ||
} | ||
return of(new ByteArrayInputStream(Files.readAllBytes(Path.of(input.toString())))); | ||
} | ||
|
||
public static DocumentDictionary of(Document doc) { | ||
return of(doc.getJsonContent().orElseThrow(() -> new IllegalArgumentException("Invalid dictionary")).asJsonObject()); | ||
} | ||
|
||
public static DocumentDictionary of(InputStream is) { | ||
return of(parse(is)); | ||
} | ||
|
||
public static DocumentDictionary of(JsonObject json) { | ||
var builder = DocumentDictionaryBuilder.create(json.getInt("code")); | ||
|
||
for (var item : json.entrySet()) { | ||
switch (item.getKey()) { | ||
case "code": | ||
continue; | ||
case "context": | ||
item.getValue().asJsonObject().entrySet() | ||
.forEach(e -> builder.context( | ||
((JsonNumber) e.getValue()).intValue(), | ||
e.getKey())); | ||
default: | ||
item.getValue().asJsonObject().entrySet() | ||
.forEach(e -> builder.type( | ||
item.getKey(), | ||
((JsonNumber) e.getValue()).intValue(), | ||
e.getKey())); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
public static JsonObject parse(InputStream json) { | ||
try (final JsonParser parser = Json.createParser(json)) { | ||
|
||
if (!parser.hasNext()) { | ||
throw new IllegalArgumentException("Invalid dictionary definition"); | ||
} | ||
|
||
parser.next(); | ||
|
||
return parser.getObject(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.