Skip to content

Commit 103c8bb

Browse files
authored
Merge pull request #156 from filip26/feat/tree-io-r1
Adopt tree-io 0.9.1
2 parents 3adfa12 + e633968 commit 103c8bb

File tree

6 files changed

+88
-62
lines changed

6 files changed

+88
-62
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5757
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5858

59-
<tree.version>0.7.5</tree.version>
59+
<tree.version>0.9.1</tree.version>
6060
<jakarta.json.version>2.0.1</jakarta.json.version>
6161

6262
<copper-multibase>4.1.0</copper-multibase>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public JsonArray expand() throws JsonLdError {
9393
final JsonArrayBuilder result = Json.createArrayBuilder();
9494

9595
// 5.2.
96-
for (final Object item : adapter.iterable(element)) {
96+
for (final Object item : adapter.elements(element)) {
9797

9898
// 5.2.1
9999
JsonValue expanded = Expansion

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

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
*/
1616
package com.apicatalog.cborld.context;
1717

18+
import java.io.IOException;
1819
import java.net.URI;
1920
import java.util.Collection;
20-
import java.util.List;
21+
import java.util.Iterator;
2122
import java.util.Optional;
2223
import java.util.function.Consumer;
23-
import java.util.stream.Collectors;
2424

2525
import com.apicatalog.cborld.mapping.TypeKeyNameMapper;
2626
import com.apicatalog.jsonld.JsonLdError;
27+
import com.apicatalog.jsonld.JsonLdErrorCode;
2728
import com.apicatalog.jsonld.context.ActiveContext;
2829
import com.apicatalog.jsonld.context.TermDefinition;
2930
import com.apicatalog.jsonld.json.JsonUtils;
@@ -36,7 +37,7 @@
3637
final class ObjectExpansion {
3738

3839
final JakartaMaterializer jakarta = new JakartaMaterializer();
39-
40+
4041
// mandatory
4142
private ActiveContext activeContext;
4243
private JsonValue propertyContext;
@@ -55,8 +56,8 @@ final class ObjectExpansion {
5556

5657
private ObjectExpansion(final ActiveContext activeContext, final JsonValue propertyContext,
5758
final Object element, final NodeAdapter adapter,
58-
final String activeProperty, final URI baseUrl,
59-
Consumer<Collection<String>> appliedContexts,
59+
final String activeProperty, final URI baseUrl,
60+
Consumer<Collection<String>> appliedContexts,
6061
TypeKeyNameMapper typeMapper) {
6162
this.activeContext = activeContext;
6263
this.propertyContext = propertyContext;
@@ -98,7 +99,11 @@ public JsonValue expand() throws JsonLdError {
9899

99100
initPropertyContext();
100101

101-
initLocalContext();
102+
try {
103+
initLocalContext();
104+
} catch (IOException e) {
105+
throw new JsonLdError(JsonLdErrorCode.UNSPECIFIED, e);
106+
}
102107

103108
// 10.
104109
final ActiveContext typeContext = activeContext;
@@ -150,16 +155,20 @@ private void initPreviousContext() throws JsonLdError {
150155

151156
boolean revert = true;
152157

153-
for (final String key : adapter.keys(element)
158+
final Iterator<String> keys = adapter.keys(element)
154159
.stream()
155160
.map(adapter::asString)
156161
.sorted()
157-
.toList()) {
162+
.iterator();
163+
164+
while (keys.hasNext()) {
165+
166+
final String key = keys.next();
158167

159168
final String expandedKey = UriExpansion
160169
.with(activeContext, appliedContexts)
161170
.vocab(true)
162-
.expand(key);
171+
.expand(adapter.asString(key));
163172

164173
if (Keywords.VALUE.equals(expandedKey) || (Keywords.ID.equals(expandedKey) && (adapter.size(element) == 1))) {
165174
revert = false;
@@ -173,15 +182,15 @@ private void initPreviousContext() throws JsonLdError {
173182
}
174183
}
175184

176-
private void initLocalContext() throws JsonLdError {
185+
private void initLocalContext() throws JsonLdError, IOException {
177186

178187
// 9.
179188
final Object contextElement = adapter.property(Keywords.CONTEXT, element);
180189

181190
if (contextElement != null) {
182191

183192
jakarta.node(contextElement, adapter);
184-
193+
185194
final JsonValue jsonContext = jakarta.json();
186195

187196
for (final JsonValue context : JsonUtils.toJsonArray(jsonContext)) {
@@ -205,11 +214,15 @@ private String processTypeScoped(final ActiveContext typeContext) throws JsonLdE
205214

206215
String typeKey = null;
207216

208-
for (final String key : adapter.keys(element)
217+
final Iterator<String> keys = adapter.keys(element)
209218
.stream()
210219
.map(adapter::asString)
211220
.sorted()
212-
.toList()) {
221+
.iterator();
222+
223+
while (keys.hasNext()) {
224+
225+
final String key = keys.next();
213226

214227
final String expandedKey = UriExpansion
215228
.with(activeContext, appliedContexts)
@@ -227,16 +240,18 @@ private String processTypeScoped(final ActiveContext typeContext) throws JsonLdE
227240
typeMapper.typeKeyName(key);
228241
}
229242

230-
final Object entry = adapter.property(key, element);
243+
final Object value = adapter.property(key, element);
231244

232245
// 11.2
233-
final List<String> terms = adapter.asStream(entry)
246+
final Iterator<String> terms = adapter.asStream(value)
234247
.filter(adapter::isString)
235248
.map(adapter::stringValue)
236249
.sorted()
237-
.collect(Collectors.toList());
250+
.iterator();
251+
252+
while (terms.hasNext()) {
238253

239-
for (final String term : terms) {
254+
final String term = terms.next();
240255

241256
Optional<JsonValue> localContext = typeContext.getTerm(term).map(TermDefinition::getLocalContext);
242257

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.net.URI;
1919
import java.util.Collection;
2020
import java.util.Collections;
21+
import java.util.Iterator;
2122
import java.util.Optional;
2223
import java.util.function.Consumer;
2324

@@ -29,7 +30,6 @@
2930
import com.apicatalog.jsonld.json.JsonUtils;
3031
import com.apicatalog.jsonld.lang.Keywords;
3132
import com.apicatalog.jsonld.lang.ListObject;
32-
import com.apicatalog.jsonld.lang.Utils;
3333
import com.apicatalog.jsonld.lang.ValueObject;
3434
import com.apicatalog.tree.io.NodeAdapter;
3535
import com.apicatalog.tree.io.NodeType;
@@ -104,13 +104,15 @@ public void expand() throws JsonLdError {
104104
return;
105105
}
106106

107-
final Collection<String> keys = adapter.keys(element)
107+
final Iterator<String> keys = adapter.keys(element)
108108
.stream()
109109
.map(adapter::asString)
110-
.toList();
110+
.sorted()
111+
.iterator();
112+
113+
while (keys.hasNext()) {
111114

112-
// 13.
113-
for (final String key : Utils.index(keys, ordered)) {
115+
final String key = keys.next();
114116

115117
// 13.1.
116118
if (Keywords.CONTEXT.equals(key)) {
@@ -130,6 +132,7 @@ public void expand() throws JsonLdError {
130132
}
131133

132134
final Object value = adapter.property(key, element);
135+
133136
final NodeType valueType = adapter.type(value);
134137

135138
// 13.4. If expanded property is a keyword:

src/main/java/com/apicatalog/cborld/context/mapping/CborMapping.java

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import co.nstant.in.cbor.model.Array;
1111
import co.nstant.in.cbor.model.DataItem;
1212
import co.nstant.in.cbor.model.MajorType;
13+
import co.nstant.in.cbor.model.UnicodeString;
1314
import co.nstant.in.cbor.model.UnsignedInteger;
1415

1516
class CborMapping extends CborAdapter {
@@ -31,50 +32,59 @@ class CborMapping extends CborAdapter {
3132
@Override
3233
public DataItem property(Object property, Object node) {
3334
if (property instanceof String term) {
34-
3535
DataItem key = encodeTerm.apply(term);
36-
DataItem value = super.property(key, node);
37-
boolean arrayCode = false;
38-
39-
if (value == null && MajorType.UNSIGNED_INTEGER.equals(key.getMajorType())) {
40-
key = new UnsignedInteger(((UnsignedInteger) key).getValue().add(BigInteger.ONE));
41-
value = super.property(key, node);
42-
if (value != null) {
43-
arrayCode = true;
44-
}
45-
}
46-
if (value != null) {
47-
if ((!arrayCode && MajorType.ARRAY.equals(value.getMajorType()))) {
36+
return get(term, key, node);
37+
}
38+
if (isNumber(property) && property instanceof DataItem key) {
39+
String term = decodeTerm.apply(key);
40+
return get(term, key, node);
41+
}
42+
return super.property(property, node);
43+
}
44+
45+
@Override
46+
public String asString(Object node) {
47+
if (node instanceof DataItem data) {
48+
return decodeTerm.apply(data);
49+
}
50+
return super.asString(node);
51+
}
4852

49-
value = decodeValue.apply(value, term);
53+
protected DataItem get(String term, DataItem key, Object node) {
54+
boolean arrayCode = false;
55+
DataItem value = super.property(key, node);
5056

51-
} else if (MajorType.ARRAY.equals(value.getMajorType())) {
57+
if (value == null && key instanceof UnsignedInteger keyCode) {
58+
value = super.property(new UnsignedInteger((keyCode).getValue().add(BigInteger.ONE)), node);
59+
if (value != null) {
60+
arrayCode = true;
61+
}
62+
}
5263

53-
Collection<DataItem> items = ((Array) value).getDataItems();
64+
if (value != null) {
65+
if ((!arrayCode && MajorType.ARRAY == value.getMajorType())) {
5466

55-
Array newValues = new Array(items.size());
67+
value = decodeValue.apply(value, term);
5668

57-
for (DataItem item : items) {
58-
newValues.add(decodeValue.apply(item, term));
59-
}
69+
} else if (value instanceof Array array) {
6070

61-
value = newValues;
71+
Collection<DataItem> items = array.getDataItems();
6272

63-
} else {
64-
value = decodeValue.apply(value, term);
73+
Array newValues = new Array(items.size());
74+
75+
for (DataItem item : items) {
76+
newValues.add(decodeValue.apply(item, term));
6577
}
66-
return value;
78+
79+
value = newValues;
80+
81+
} else {
82+
value = decodeValue.apply(value, term);
6783
}
84+
return value;
6885
}
69-
return super.property(property, node);
70-
}
7186

72-
@Override
73-
public String asString(Object node) {
74-
if (node instanceof DataItem) {
75-
return decodeTerm.apply((DataItem) node);
76-
}
77-
return super.asString(node);
87+
return super.property(new UnicodeString(term), node);
7888
}
7989

8090
}

src/main/java/com/apicatalog/cborld/decoder/AbstractDecoder.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,12 @@ protected final JsonValue decodeData(final DataItem data, final String term, fin
100100

101101
case BYTE_STRING:
102102
JsonValue decoded = decodeValue(data, term, def, mapping);
103-
if (decoded == null) {
104-
throw new DecoderException(Code.InvalidDocument, "Unknown encoded value [" + data.getMajorType() + "] at key [" + term + "].");
103+
if (decoded != null) {
104+
return decoded;
105105
}
106106

107-
return decoded;
108-
109107
default:
110-
throw new IllegalStateException("An unexpected data item type [" + data.getMajorType() + "].");
108+
throw new IllegalStateException("An unexpected data item type " + data.getMajorType() + " at " + term + ".");
111109
}
112110
}
113111

@@ -225,7 +223,7 @@ protected final JsonValue decodeValue(final DataItem value, final String propert
225223
if (decoded != null) {
226224
return Json.createValue(decoded);
227225
}
228-
}
226+
}
229227
return null;
230228
}
231229

0 commit comments

Comments
 (0)