Skip to content

Commit 2352365

Browse files
authored
Merge pull request #141 from filip26/feat/diagnose
Debug Mode
2 parents c9d3396 + 3c4777e commit 2352365

File tree

66 files changed

+1082
-531
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1082
-531
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ document = decoder.decode(encoded);
6868

6969
### Dictionary
7070

71-
It’s highly recommended to use dictionaries to maximize the compression ratio. A dictionary consists of terms and the codes they are encoded to. The dictionary code is preserved in the encoded CBOR-LD, but the decoder must have the same dictionary enabled. See the [W3C CBOR-LD Registry for examples](https://json-ld.github.io/cbor-ld-spec/#registry).
71+
Use dictionaries to maximize the compression ratio. A dictionary consists of terms and the codes they are encoded to. The dictionary code is preserved in the encoded CBOR-LD, but the decoder must have the same dictionary enabled. See the [W3C CBOR-LD Registry for examples](https://json-ld.github.io/cbor-ld-spec/#registry).
7272

7373
A dictionary should contain terms that are not present in contexts, such as the context URIs themselves, custom common values bound to types, and custom common URIs.
7474

@@ -94,18 +94,39 @@ var dictionary = DocumentDictionaryBuilder
9494
var encoder = CborLd.createEncoder()
9595
.dictionary(dictionary)
9696
.loader(...)
97-
// customize
97+
// ... customize
9898
.build();
9999

100100
// use with decoder, please note you can register multiple dictionaries
101101
var decoder = CborLd.createDecoder()
102102
.dictionary(dictionary)
103103
.loader(...)
104-
// customize
104+
// ... customize
105105
.build();
106106

107107
```
108108

109+
### Diagnose / Debug
110+
111+
```javascript
112+
var debug = CborLd.create[Decoder|Encoder]()
113+
// ... customize
114+
.debug();
115+
116+
debug.[encode|decode](...);
117+
118+
debug.isCborLd(); // true if the encoded document is in CBOR-LD format
119+
debug.version(); // CBOR-LD encoding version
120+
debug.dictionary(); // static terms
121+
debug.terms(); // dynamic term map
122+
123+
debug.isError(); // true if an exception has been thrown
124+
debug.error(); // an exception or null
125+
126+
debug.dump(); // dump as JSON
127+
128+
```
129+
109130
### Backward Compatibility
110131

111132
```javascript

src/main/java/com/apicatalog/cborld/CborLd.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ public class CborLd {
1313

1414
public static final byte LEADING_BYTE = (byte) 0xD9; // tag
1515

16-
public static final byte[] VERSION_10_BYTES = new byte[] { (byte) 0xCB, 0x1D };
16+
public static final byte[] VERSION_1_BYTES = new byte[] { (byte) 0xCB, 0x1D };
1717
public static final byte VERSION_06_BYTE = (byte) 0x06;
1818
public static final byte VERSION_05_BYTE = (byte) 0x05;
1919

20-
public static final byte UNCOMPRESSED_BYTE = 0x00;
21-
22-
public static final byte COMPRESSED_BYTE = 0x01;
23-
2420
protected CborLd() {
2521
/* protected */ }
2622

@@ -84,7 +80,7 @@ public static EncoderBuilder createEncoder() {
8480
public static EncoderBuilder createEncoder(EncoderConfig config) {
8581
return EncoderBuilder.of(config);
8682
}
87-
83+
8884
public static EncoderBuilder createEncoder(CborLdVersion version) {
8985
return EncoderBuilder.of(version);
9086
}

src/main/java/com/apicatalog/cborld/CborLdVersion.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.apicatalog.cborld;
22

33
import java.util.Arrays;
4+
import java.util.Objects;
45

56
public enum CborLdVersion {
67

7-
V1(CborLd.VERSION_10_BYTES),
8+
V1(CborLd.VERSION_1_BYTES),
89

910
// legacy
1011
V06(new byte[] { CborLd.VERSION_06_BYTE }),
@@ -20,14 +21,24 @@ public byte[] bytes() {
2021
return bytes;
2122
}
2223

24+
/**
25+
* Identifies the CborLdVersion from a given byte array and offset.
26+
*
27+
* @param bytes The input byte array
28+
* @param offset The offset to start checking from
29+
* @return Matching CborLdVersion or {@code null} if not found
30+
*/
2331
public static CborLdVersion of(byte[] bytes, int offset) {
24-
for (CborLdVersion version : CborLdVersion.values()) {
25-
if (((offset + version.bytes.length) < bytes.length)
26-
&& Arrays.equals(version.bytes, 0, version.bytes.length, bytes, offset, offset + version.bytes().length)) {
32+
Objects.requireNonNull(bytes);
33+
34+
for (var version : values()) {
35+
var versionBytes = version.bytes;
36+
int end = offset + versionBytes.length;
37+
38+
if (end <= bytes.length && Arrays.mismatch(versionBytes, 0, versionBytes.length, bytes, offset, end) == -1) {
2739
return version;
2840
}
2941
}
3042
return null;
3143
}
32-
3344
}

src/main/java/com/apicatalog/cborld/config/BaseConfig.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,9 @@
22

33
import com.apicatalog.cborld.CborLdVersion;
44

5-
public class BaseConfig implements Config {
5+
public interface BaseConfig {
66

7-
// protected DocumentLoader loader;
8-
// protected boolean bundledContexts;
9-
// protected URI base;
7+
CborLdVersion version();
108

11-
protected BaseConfig() {
12-
// this.bundledContexts = bundledContexts;
13-
// this.loader = null;
14-
// this.base = null;
15-
}
16-
17-
// @Override
18-
// public DocumentLoader loader() {
19-
// return loader;
20-
// }
21-
//
22-
// @Override
23-
// public URI base() {
24-
// return base;
25-
// }
26-
27-
@Override
28-
public CborLdVersion version() {
29-
// TODO Auto-generated method stub
30-
return null;
31-
}
32-
33-
@Override
34-
public boolean isCompactArrays() {
35-
// TODO Auto-generated method stub
36-
return false;
37-
}
9+
boolean isCompactArrays();
3810
}

src/main/java/com/apicatalog/cborld/config/Config.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/com/apicatalog/cborld/config/ConfigV1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import com.apicatalog.cborld.registry.DefaultDocumentDictionary;
3838
import com.apicatalog.cborld.registry.DocumentDictionary;
3939

40-
public class ConfigV1 extends BaseConfig implements EncoderConfig, DecoderConfig {
40+
public class ConfigV1 implements EncoderConfig, DecoderConfig {
4141

4242
public static final ConfigV1 INSTANCE = new ConfigV1();
4343

src/main/java/com/apicatalog/cborld/config/DictionaryAlgorithm.java renamed to src/main/java/com/apicatalog/cborld/config/ContextTermsAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.apicatalog.cborld.config;
22

3-
public enum DictionaryAlgorithm {
3+
public enum ContextTermsAlgorithm {
44

55
ProcessingOrderAppliedContexts,
66

src/main/java/com/apicatalog/cborld/config/LegacyConfigV05.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.apicatalog.cborld.registry.DocumentDictionary;
3232
import com.apicatalog.cborld.registry.LegacyDictionary;
3333

34-
public class LegacyConfigV05 extends BaseConfig implements EncoderConfig, DecoderConfig {
34+
public class LegacyConfigV05 implements EncoderConfig, DecoderConfig {
3535

3636
public static final LegacyConfigV05 INSTANCE = new LegacyConfigV05();
3737

src/main/java/com/apicatalog/cborld/config/LegacyConfigV06.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.apicatalog.cborld.encoder.EncoderConfig;
2323
import com.apicatalog.cborld.encoder.value.ContextValueEncoder;
2424
import com.apicatalog.cborld.encoder.value.CustomTypeValueEncoder;
25-
import com.apicatalog.cborld.encoder.value.CustomUriValueEncoder;
2625
import com.apicatalog.cborld.encoder.value.DidKeyValueEncoder;
2726
import com.apicatalog.cborld.encoder.value.IdValueEncoder;
2827
import com.apicatalog.cborld.encoder.value.MultibaseValueEncoder;
@@ -37,7 +36,7 @@
3736
import com.apicatalog.cborld.registry.DocumentDictionary;
3837
import com.apicatalog.cborld.registry.LegacyDictionary;
3938

40-
public class LegacyConfigV06 extends BaseConfig implements EncoderConfig, DecoderConfig {
39+
public class LegacyConfigV06 implements EncoderConfig, DecoderConfig {
4140

4241
public static final LegacyConfigV06 INSTANCE = new LegacyConfigV06();
4342

@@ -59,7 +58,6 @@ public class LegacyConfigV06 extends BaseConfig implements EncoderConfig, Decode
5958
VALUE_ENCODERS.add(new CustomTypeValueEncoder());
6059

6160
// value driven
62-
VALUE_ENCODERS.add(new CustomUriValueEncoder());
6361
VALUE_ENCODERS.add(new UuidValueEncoder());
6462
VALUE_ENCODERS.add(new DidKeyValueEncoder());
6563
}

src/main/java/com/apicatalog/cborld/context/Context.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public static Context from(Data document, URI base, DocumentLoader loader) throw
4747

4848
}
4949

50-
public static Context from(Data document, URI base, DocumentLoader loader, Consumer<Collection<String>> appliedContexts,
50+
public static Context from(Data document,
51+
URI base,
52+
DocumentLoader loader,
53+
Consumer<Collection<String>> appliedContexts,
5154
TypeKeyNameMapper typeMapper) throws JsonLdError {
5255

5356
final JsonLdOptions options = new JsonLdOptions();

0 commit comments

Comments
 (0)