-
-
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.
Merge pull request #50 from filip26/patch/iron
Carbon ORM Support
- Loading branch information
Showing
38 changed files
with
496 additions
and
160 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
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
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
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
93 changes: 93 additions & 0 deletions
93
java/src/main/java/com/apicatalog/linkedtree/jsonld/io/JsonLdReader.java
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,93 @@ | ||
package com.apicatalog.linkedtree.jsonld.io; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import com.apicatalog.jsonld.JsonLd; | ||
import com.apicatalog.jsonld.JsonLdError; | ||
import com.apicatalog.jsonld.document.JsonDocument; | ||
import com.apicatalog.jsonld.loader.DocumentLoader; | ||
import com.apicatalog.linkedtree.adapter.NodeAdapterError; | ||
import com.apicatalog.linkedtree.builder.TreeBuilderError; | ||
import com.apicatalog.linkedtree.jsonld.JsonLdKeyword; | ||
import com.apicatalog.linkedtree.orm.Context; | ||
import com.apicatalog.linkedtree.orm.Fragment; | ||
import com.apicatalog.linkedtree.orm.mapper.TreeMapping; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonStructure; | ||
|
||
public class JsonLdReader { | ||
|
||
protected final JsonLdTreeReader reader; | ||
protected DocumentLoader loader; | ||
|
||
protected JsonLdReader(JsonLdTreeReader reader, DocumentLoader loader) { | ||
this.reader = reader; | ||
this.loader = loader; | ||
} | ||
|
||
public static final JsonLdReader of(TreeMapping mapping, DocumentLoader loader) { | ||
return new JsonLdReader(JsonLdTreeReader.of(mapping), loader); | ||
} | ||
|
||
public <T> T read(Class<T> typeInterface, JsonObject fragment) throws TreeBuilderError, NodeAdapterError { | ||
|
||
JsonObject input = fragment; | ||
|
||
JsonStructure expandContext = null; | ||
|
||
// check context | ||
if (!input.containsKey(JsonLdKeyword.CONTEXT)) { | ||
Collection<String> contexts = context(typeInterface, new ArrayList<>(2)); //TODO get from scans, use cache | ||
// inject context if exist | ||
if (!contexts.isEmpty()) { | ||
expandContext = Json.createArrayBuilder(contexts).build(); | ||
} | ||
} | ||
|
||
try { | ||
final JsonArray expanded = JsonLd | ||
.expand(JsonDocument.of(fragment)) | ||
.context(expandContext) | ||
.loader(loader) | ||
.get(); | ||
|
||
return reader.read(typeInterface, expanded); | ||
|
||
} catch (JsonLdError e) { | ||
throw new NodeAdapterError(e); | ||
} | ||
} | ||
|
||
Collection<String> context(Class<?> typeInterface, Collection<String> context) { | ||
|
||
if (typeInterface == null) { | ||
return context; | ||
} | ||
|
||
if (typeInterface.getInterfaces() != null) { | ||
for (Class<?> superType : typeInterface.getInterfaces()) { | ||
context(superType, context); | ||
} | ||
} | ||
|
||
Fragment fragment = typeInterface.getDeclaredAnnotation(Fragment.class); | ||
|
||
if (fragment != null) { | ||
|
||
Context fragmentContext = typeInterface.getAnnotation(Context.class); | ||
if (fragmentContext != null) { | ||
if (fragmentContext.override()) { | ||
context.clear(); | ||
} | ||
for (String ctx : fragmentContext.value()) { | ||
context.add(ctx); | ||
} | ||
} | ||
} | ||
return context; | ||
} | ||
} |
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.