Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions core/src/main/java/org/incenp/linkml/core/Base16BlobConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* LinkML-Java - LinkML library for Java
* Copyright © 2026 Damien Goutte-Gattat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* (3) Neither the name of the copyright holder nor the names its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package org.incenp.linkml.core;

import org.incenp.linkml.core.types.BinaryBlob;
import org.incenp.linkml.core.types.BinaryBlob.BinaryEncoding;

/**
* A converter for slots typed as <code>xsd:hexBinary</code>.
* <p>
* Such slots are represented in this runtime as {@link BinaryBlob} fields.
*/
public class Base16BlobConverter extends ScalarConverterBase {

@Override
public Class<?> getType() {
return BinaryBlob.class;
}

@Override
public String getURI() {
return "http://www.w3.org/2001/XMLSchema#hexBinary";
}

@Override
protected Object convertImpl(Object raw, ConverterContext ctx) throws LinkMLRuntimeException {
try {
return new BinaryBlob(raw.toString(), BinaryEncoding.BASE16);
} catch ( IllegalArgumentException e ) {
throw new LinkMLValueError("Invalid value, hex-encoded binary blob expected", e);
}
}

@Override
public Object serialise(Object object, ConverterContext ctx) throws LinkMLRuntimeException {
if ( getType().isInstance(object) ) {
return ((BinaryBlob) object).getValue(BinaryEncoding.BASE16);
} else {
throw new LinkMLInternalError("Invalid value");
}
}
}
74 changes: 74 additions & 0 deletions core/src/main/java/org/incenp/linkml/core/Base64BlobConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* LinkML-Java - LinkML library for Java
* Copyright © 2026 Damien Goutte-Gattat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* (3) Neither the name of the copyright holder nor the names its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package org.incenp.linkml.core;

import org.incenp.linkml.core.types.BinaryBlob;
import org.incenp.linkml.core.types.BinaryBlob.BinaryEncoding;

/**
* A converter for slots typed as <code>xsd:base64Binary</code>.
* <p>
* Such slots are represented in this runtime as {@link BinaryBlob} fields.
*/
public class Base64BlobConverter extends ScalarConverterBase {

@Override
public Class<?> getType() {
return BinaryBlob.class;
}

@Override
public String getURI() {
return "http://www.w3.org/2001/XMLSchema#base64Binary";
}

@Override
protected Object convertImpl(Object raw, ConverterContext ctx) throws LinkMLRuntimeException {
try {
return new BinaryBlob(raw.toString(), BinaryEncoding.BASE64);
} catch ( IllegalArgumentException e ) {
throw new LinkMLValueError("Invalid value, Base64-encoded binary blob expected", e);
}
}

@Override
public Object serialise(Object object, ConverterContext ctx) throws LinkMLRuntimeException {
if ( getType().isInstance(object) ) {
return ((BinaryBlob) object).getValue(BinaryEncoding.BASE64);
} else {
throw new LinkMLInternalError("Invalid value");
}
}
}
22 changes: 20 additions & 2 deletions core/src/main/java/org/incenp/linkml/core/ConverterContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public class ConverterContext {

private Map<Class<?>, IConverter> converters = new HashMap<>();
private Map<Slot, IConverter> slotConverters = new HashMap<>();
private Map<String, IConverter> uriConverters = new HashMap<>();
private ObjectCache objectCache = new ObjectCache();
private List<DelayedAssignment> delayedAssignments = new ArrayList<>();
private Map<String, String> prefixMap = new HashMap<>();
Expand Down Expand Up @@ -168,6 +169,11 @@ public ConverterContext() {
// whose range is set to the linkml:Any class).
converters.put(Object.class, new TransparentConverter());

// Converters that are looked up by type URIs.
addConverter(new CurieConverter());
addConverter(new Base64BlobConverter());
addConverter(new Base16BlobConverter());

objectConverterProvider = (t) -> new ObjectConverter(t);
typeResolver = new DefaultTypeDesignatorResolver();
}
Expand Down Expand Up @@ -234,7 +240,12 @@ public void addConverter(Class<?> type) {
* @param converter The converter to register.
*/
public void addConverter(IConverter converter) {
converters.put(converter.getType(), converter);
String uri = converter.getURI();
if ( uri != null ) {
uriConverters.put(uri, converter);
} else {
converters.put(converter.getType(), converter);
}
}

/**
Expand Down Expand Up @@ -300,7 +311,14 @@ public IConverter getConverter(Slot slot) throws LinkMLRuntimeException {
if ( type != null ) {
conv = getCustomConverter(type, slot.getInnerType());
} else {
conv = getConverter(slot.getInnerType());
String typeUri = slot.getTypeURI();
if ( typeUri != null ) {
conv = uriConverters.get(typeUri);
}

if ( conv == null ) {
conv = getConverter(slot.getInnerType());
}
}
slotConverters.put(slot, conv);
}
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/incenp/linkml/core/CurieConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
*/
public class CurieConverter extends StringConverter {

public static final String URIORCURIE_TYPE_URI = "https://w3id.org/linkml/Uriorcurie";

@Override
public String getURI() {
return URIORCURIE_TYPE_URI;
}

@Override
protected Object convertImpl(Object raw, ConverterContext ctx) throws LinkMLRuntimeException {
if ( raw instanceof String ) {
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/org/incenp/linkml/core/IConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ public interface IConverter {
*/
public Class<?> getType();

/**
* Gets the URI identifying the LinkML type this converter is intended for.
* <p>
* This is intended to provide another way to look up converter objects, if
* looking up by the Java type (using {@link #getType()}) is not enough
* (typically, when several LinkML types are rendered using the same Java type).
*
* @return The LinkML type URI. May be {@code null}.
*/
default public String getURI() {
return null;
}

/**
* Converts a raw object into a LinkML object.
* <p>
Expand Down
18 changes: 9 additions & 9 deletions core/src/main/java/org/incenp/linkml/core/ObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import java.time.LocalTime;
import java.time.ZonedDateTime;

import org.incenp.linkml.core.annotations.LinkURI;
import org.incenp.linkml.core.annotations.TypeURI;

/**
* Represents the type of a LinkML object.
*/
Expand Down Expand Up @@ -73,21 +76,18 @@ public enum ObjectType {
* @return The corresponding LinkML type.
*/
public static ObjectType get(Class<?> type) {
// FIXME: We might have to deal with custom types as well at some point.
if ( type.isPrimitive() || type == String.class || type == Boolean.class || type == Integer.class
|| type == Float.class || type == Double.class || type == ZonedDateTime.class || type == LocalDate.class
|| type == LocalTime.class || type == URI.class ) {
return TYPE;
} else if ( type.isEnum() ) {
// FIXME: Not necessarily a *LinkML* enum
} else if ( type.isEnum() && type.isAnnotationPresent(LinkURI.class) ) {
return ENUM;
} else if ( type.getSuperclass() == null ) {
// Could be Object, an interface, or void -- in any case, cannot be a LinkML
// object at all
return NONE;
} else {
// FIXME: Not necessarily a *LinkML* class
} else if ( type.isAnnotationPresent(TypeURI.class) ) {
return TYPE;
} else if ( type.isAnnotationPresent(LinkURI.class) ) {
return CLASS;
} else {
return NONE;
}
}
}
21 changes: 21 additions & 0 deletions core/src/main/java/org/incenp/linkml/core/Slot.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.incenp.linkml.core.annotations.Required;
import org.incenp.linkml.core.annotations.SlotName;
import org.incenp.linkml.core.annotations.TypeDesignator;
import org.incenp.linkml.core.annotations.TypeURI;

/**
* Represents a “slot” (that is, a field) on a specific LinkML object.
Expand Down Expand Up @@ -197,6 +198,23 @@ public String getLinkedURI() {
return annot != null ? annot.value() : null;
}

/**
* Gets the URI that identifies the type of the slot.
* <p>
* A field representing a LinkML slot may be annotated with a {@link TypeURI}
* annotation indicating the URI for the type of the slot’s range. This is
* useful if several LinkML types are rendered using the same Java type, to
* distinguish what was the original LinkML type.
*
* @return The URI for the type set as the slot’s range. May be {@code null} if
* the field has not been explicitly annotated.
*/
public String getTypeURI() {
TypeURI annot = field.getAnnotation(TypeURI.class);
return annot != null ? annot.value() : null;

}

/**
* Indicates whether the slot is expected to hold multiple values.
*
Expand All @@ -214,6 +232,9 @@ public boolean isMultivalued() {
* <code>false</code>.
*/
public boolean isCurieTyped() {
if ( CurieConverter.URIORCURIE_TYPE_URI.equals(getTypeURI()) ) {
return true;
}
Class<?> customConverter = getCustomConverter();
return customConverter != null && customConverter.equals(CurieConverter.class);
}
Expand Down
59 changes: 59 additions & 0 deletions core/src/main/java/org/incenp/linkml/core/annotations/TypeURI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* LinkML-Java - LinkML library for Java
* Copyright © 2026 Damien Goutte-Gattat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* (3) Neither the name of the copyright holder nor the names its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package org.incenp.linkml.core.annotations;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

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

/**
* An annotation to indicate the URI associated with the LinkML type of a field.
* <p>
* When this annotation is present, it can be used to determine how the field
* should be (de)serialized, instead of determining that solely from the field’s
* Java type.
* <p>
* A secondary use of this annotation is to mark classes that represent custom
* types, to make sure they are not mistaken for classes representing LinkML
* classes.
*/
@Retention(RUNTIME)
@Target({ TYPE, FIELD })
public @interface TypeURI {
String value();
}
Loading
Loading