Skip to content

Commit

Permalink
add non-generated classes and add maven
Browse files Browse the repository at this point in the history
  • Loading branch information
olinux committed Nov 27, 2023
1 parent 3fe01d8 commit df2ddce
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 0 deletions.
105 changes: 105 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2023 EBRAINS AISBL
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0.
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
~ This open source software code was developed in part or in whole in the
~ Human Brain Project, funded from the European Union's Horizon 2020
~ Framework Programme for Research and Innovation under
~ Specific Grant Agreement No. 945539
~ (Human Brain Project SGA3).
~
-->

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openmetadatainitiative</groupId>
<artifactId>openminds</artifactId>
<version>3.0.0-beta1</version>
<name>openMINDS</name>
<packaging>jar</packaging>
<description>A java mapping for openMINDS instances</description>
<url>https://openminds-documentation.readthedocs.io</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
<distribution>repo</distribution>
</license>
</licenses>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

</build>


<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.openmetadatainitiative.openminds.utils;

public interface Builder<T extends Instance> {

T build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.openmetadatainitiative.openminds.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

public abstract class ByTypeDeserializer<T extends Entity> extends JsonDeserializer<T> {

private final Class<? extends T>[] subTypes;

public ByTypeDeserializer(Class<? extends T>... subTypes) {
this.subTypes = subTypes;
}

private T map(JsonNode root, ObjectMapper mapper, Iterable<JsonNode> nodes) throws JsonProcessingException {
for (JsonNode type : nodes) {
if (type != null) {
final String typeString = type.asText();
if (typeString != null) {
final Optional<Class<? extends T>> first = Arrays.stream(this.subTypes).filter(t -> {
final InstanceType annotation = t.getAnnotation(InstanceType.class);
if(annotation == null || annotation.value() == null){
throw new RuntimeException("Type {} was mapped as a subtype but doesn't provide the @InstanceType annotation!");
}
return typeString.equals(annotation.value());
}).findFirst();
if (first.isPresent()) {
return mapper.treeToValue(root, first.get());
}
}
}
}
return null;
}

@Override
public T deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
final JsonNode root = mapper.readTree(jsonParser);
final JsonNode typeNode = root.get("@type");
if( typeNode.isTextual()){
return map(root, mapper, Collections.singleton(typeNode));
}
if (typeNode.isArray()) {
return map(root, mapper, typeNode);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.openmetadatainitiative.openminds.utils;

public interface Entity {

InstanceId getId();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.openmetadatainitiative.openminds.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class Instance implements Entity{

@JsonProperty("@id")
protected InstanceId id;

@JsonProperty("@type")
protected List<String> types;

@JsonIgnore
private final LocalId localId;

public Instance(LocalId localId) {
this.localId = localId;
}

@JsonIgnore
public LocalId getLocalId() {
return localId;
}

public InstanceId getId(){
return id;
}
protected <T extends Instance> Reference<T> doGetReference(){
return new Reference<>(getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.openmetadatainitiative.openminds.utils;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.IOException;

@JsonSerialize(using = InstanceId.Serializer.class)
@JsonDeserialize(using = InstanceId.Deserializer.class)
public record InstanceId(String instanceId) {

public static InstanceId withPrefix(String id, String prefix){
return id!=null ? prefix != null ? new InstanceId(prefix+id) : new InstanceId(id) : null;

}

static class Serializer extends JsonSerializer<InstanceId> {
@Override
public void serialize(InstanceId instanceId, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
if (instanceId.instanceId != null) {
jsonGenerator.writeString(instanceId.instanceId);
} else {
jsonGenerator.writeNull();
}

}
}

static class Deserializer extends JsonDeserializer<InstanceId> {
@Override
public InstanceId deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final String value = jsonParser.getText();
if (value != null) {
return new InstanceId(value);
}
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.openmetadatainitiative.openminds.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface InstanceType {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.openmetadatainitiative.openminds.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.IOException;

@JsonSerialize(using = LocalId.Serializer.class)
@JsonDeserialize(using = LocalId.Deserializer.class)
public record LocalId(String id) {

static class Serializer extends JsonSerializer<LocalId> {
@Override
public void serialize(LocalId localId, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
if (localId.id != null) {
jsonGenerator.writeString("_:"+localId.id);
} else {
jsonGenerator.writeNull();
}

}
}

static class Deserializer extends JsonDeserializer<LocalId> {
@Override
public LocalId deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
final String value = jsonParser.getText();
if (value != null && value.startsWith("_:")) {
return new LocalId(value.substring(2));
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.openmetadatainitiative.openminds.utils;

public record OpenMINDSContext(String idPrefix, boolean autoAssignIdPrefix) {

public static OpenMINDSContext defaultContext(){
return local(true);
}

public static OpenMINDSContext local(boolean autoAssignIdPrefix){
return new OpenMINDSContext("_:", autoAssignIdPrefix);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.openmetadatainitiative.openminds.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ParsingUtils {

public static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.openmetadatainitiative.openminds.utils;

import com.fasterxml.jackson.annotation.JsonProperty;

public record Reference<T extends Instance>(@JsonProperty("@id") InstanceId id){

public static <T extends Instance> Reference<T> create(T instance){
return instance != null ? new Reference<T>(instance.getId()) : null;
}

}

0 comments on commit df2ddce

Please sign in to comment.