Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Jena 4.10 #389

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions core/lyo-core-settings/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
<artifactId>jersey-common</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import com.google.common.base.Strings;
import org.apache.jena.datatypes.DatatypeFormatException;
import org.apache.jena.datatypes.RDFDatatype;
import org.apache.jena.datatypes.TypeMapper;
import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.datatypes.xsd.XSDDateTime;
import org.apache.jena.datatypes.xsd.impl.XMLLiteralType;
import org.apache.jena.ext.com.google.common.base.Strings;
import org.apache.jena.rdf.model.Property;
import org.eclipse.lyo.oslc4j.core.model.ResourceShape;
import org.eclipse.lyo.oslc4j.core.model.XMLLiteral;
Expand Down
10 changes: 10 additions & 0 deletions core/oslc4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@
<artifactId>jersey-common</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -13,40 +13,71 @@
*/
package org.eclipse.lyo.oslc4j.provider.jena;

import org.apache.jena.rdf.model.Container;
import org.apache.jena.rdf.model.NodeIterator;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.datatypes.DatatypeFormatException;
import org.apache.jena.datatypes.RDFDatatype;
import org.apache.jena.rdf.model.ReifiedStatement;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;

import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.datatypes.xsd.XSDDateTime;
import org.apache.jena.datatypes.xsd.impl.XMLLiteralType;
import org.apache.jena.datatypes.xsd.impl.XSDDateType;
import org.apache.jena.graph.BlankNodeId;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.rdf.model.Alt;
import org.apache.jena.rdf.model.AnonId;
import org.apache.jena.rdf.model.Bag;
import org.apache.jena.rdf.model.Container;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.NodeIterator;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFList;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.RSIterator;
import org.apache.jena.rdf.model.ReifiedStatement;
import org.apache.jena.rdf.model.ResIterator;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Seq;
import org.apache.jena.rdf.model.SimpleSelector;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.graph.BlankNodeId;
import org.apache.jena.datatypes.DatatypeFormatException;
import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.datatypes.xsd.XSDDateTime;
import org.apache.jena.datatypes.xsd.impl.XMLLiteralType;
import org.apache.jena.datatypes.xsd.impl.XSDDateType;
import org.apache.jena.rdf.model.impl.ReifierStd;
import org.apache.jena.util.ResourceUtils;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDFS;
import org.apache.jena.util.ResourceUtils;

import org.eclipse.lyo.oslc4j.core.*;
import org.eclipse.lyo.oslc4j.core.annotation.*;
import org.eclipse.lyo.oslc4j.core.exception.*;
import org.eclipse.lyo.oslc4j.core.model.*;
import org.eclipse.lyo.oslc4j.core.NestedWildcardProperties;
import org.eclipse.lyo.oslc4j.core.OSLC4JConstants;
import org.eclipse.lyo.oslc4j.core.OSLC4JUtils;
import org.eclipse.lyo.oslc4j.core.OslcGlobalNamespaceProvider;
import org.eclipse.lyo.oslc4j.core.SingletonWildcardProperties;
import org.eclipse.lyo.oslc4j.core.UnparseableLiteral;
import org.eclipse.lyo.oslc4j.core.annotation.OslcName;
import org.eclipse.lyo.oslc4j.core.annotation.OslcNamespaceDefinition;
import org.eclipse.lyo.oslc4j.core.annotation.OslcPropertyDefinition;
import org.eclipse.lyo.oslc4j.core.annotation.OslcRdfCollectionType;
import org.eclipse.lyo.oslc4j.core.annotation.OslcResourceShape;
import org.eclipse.lyo.oslc4j.core.annotation.OslcSchema;
import org.eclipse.lyo.oslc4j.core.annotation.OslcValueType;
import org.eclipse.lyo.oslc4j.core.exception.LyoModelException;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreApplicationException;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreInvalidPropertyDefinitionException;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreMissingSetMethodException;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreMisusedOccursException;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreRelativeURIException;
import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
import org.eclipse.lyo.oslc4j.core.model.AnyResource;
import org.eclipse.lyo.oslc4j.core.model.FilteredResource;
import org.eclipse.lyo.oslc4j.core.model.IExtendedResource;
import org.eclipse.lyo.oslc4j.core.model.IOslcCustomNamespaceProvider;
import org.eclipse.lyo.oslc4j.core.model.IReifiedResource;
import org.eclipse.lyo.oslc4j.core.model.IResource;
import org.eclipse.lyo.oslc4j.core.model.InheritedMethodAnnotationHelper;
import org.eclipse.lyo.oslc4j.core.model.Link;
import org.eclipse.lyo.oslc4j.core.model.OslcConstants;
import org.eclipse.lyo.oslc4j.core.model.ResponseInfo;
import org.eclipse.lyo.oslc4j.core.model.TypeFactory;
import org.eclipse.lyo.oslc4j.core.model.ValueType;
import org.eclipse.lyo.oslc4j.core.model.XMLLiteral;
import org.eclipse.lyo.oslc4j.provider.jena.ordfm.ResourcePackages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -63,14 +94,40 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.lang.reflect.*;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.AbstractSequentialList;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Deque;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NavigableSet;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Function;

@SuppressWarnings({"unused", "WeakerAccess"})
Expand Down Expand Up @@ -363,7 +420,7 @@ public static Object fromJenaResource(final Resource resource, Class<?> beanClas
if (mostConcreteResourceClass.isPresent()) {
beanClass = mostConcreteResourceClass.get();
}
final Object newInstance = beanClass.newInstance();
final Object newInstance = beanClass.getDeclaredConstructor().newInstance();
final Map<Class<?>, Map<String, Method>> classPropertyDefinitionsToSetMethods = new HashMap<>();
final Map<String,Object> visitedResources = new HashMap<>();
final HashSet<String> rdfTypes = new HashSet<>();
Expand Down Expand Up @@ -553,7 +610,7 @@ private static List<Object> createObjectResultList(Class<?> beanClass,
continue;
}
}
final Object newInstance = beanClass.newInstance();
final Object newInstance = beanClass.getDeclaredConstructor().newInstance();
final Map<String,Object> visitedResources = new HashMap<>();
final HashSet<String> rdfTypes = new HashSet<>();

Expand Down Expand Up @@ -912,7 +969,7 @@ else if (o.isResource())
Class<?> resourceClass = optionalResourceClass.isPresent()
? optionalResourceClass.get()
: setMethodComponentParameterClass;
final Object nestedBean = resourceClass.newInstance();
final Object nestedBean = resourceClass.getDeclaredConstructor().newInstance();
fromResource(classPropertyDefinitionsToSetMethods,
nestedBean.getClass(),
nestedBean,
Expand All @@ -930,7 +987,7 @@ else if (o.isResource())
{
// This property supports reified statements. Create the
// new resource to hold the value and any metadata.
final Object reifiedResource = reifiedClass.newInstance();
final Object reifiedResource = reifiedClass.getDeclaredConstructor().newInstance();

// Find a setter for the actual value.
for (Method method : reifiedClass.getMethods())
Expand All @@ -949,10 +1006,12 @@ else if (o.isResource())
}

// Fill in any reified statements.
RSIterator rsIter = statement.listReifiedStatements();
while (rsIter.hasNext())
Graph stmtGraph = statement.getModel().getGraph();
ExtendedIterator<Node> reifiedTriplesIter = ReifierStd.allNodes(stmtGraph, statement.asTriple());
while (reifiedTriplesIter.hasNext())
{
ReifiedStatement reifiedStatement = rsIter.next();
Node reifiedNode = reifiedTriplesIter.next();
Resource reifiedStatement = getResource(statement.getModel(), reifiedNode);
fromResource(classPropertyDefinitionsToSetMethods,
reifiedClass,
reifiedResource,
Expand Down Expand Up @@ -1051,7 +1110,7 @@ else if ((SortedSet.class == parameterClass) ||
// Not handled above. Let's try newInstance with possible failure.
else
{
collection = ((Collection<Object>) parameterClass.newInstance());
collection = ((Collection<Object>) parameterClass.getDeclaredConstructor().newInstance());
}

collection.addAll(values);
Expand All @@ -1062,6 +1121,23 @@ else if ((SortedSet.class == parameterClass) ||
}
}

/*
Adapted from https://github.com/spdx/tools/blob/bc35e25dacb728bf8332b82b509bb3efacd6c64e/src/org/spdx/rdfparser/SPDXDocument.java#L1225
Copyright (c) 2010, 2011 Source Auditor Inc.
SPDX-License-Identifier: Apache-2.0
*/
private static Resource getResource(Model model, Node node) {
Resource s;
if (node.isURI()) {
s = model.createResource(node.getURI());
} else if (node.isBlank()) {
s = model.createResource(new AnonId(node.getBlankNodeId()));
} else {
throw(new IllegalArgumentException("Only returning nodes for URI or bnode subjects"));
}
return s;
}

/**
* Returns a hash set of rdf:types for a given resource object. If the set
* was populated before, returns the given list. This list will only be
Expand Down Expand Up @@ -1463,9 +1539,8 @@ else if (properties instanceof NestedWildcardProperties)
final Property property = model.createProperty(propertyName);
final Object value = extendedProperty.getValue();

if (value instanceof Collection)
if (value instanceof Collection<?> collection)
{
final Collection<?> collection = (Collection<?>) value;
for (Object next : collection)
{
handleExtendedValue(resourceClass,
Expand Down Expand Up @@ -1509,14 +1584,13 @@ private static void handleExtendedValue(final Class<?> objectClass,
InvocationTargetException,
OslcCoreApplicationException
{
if (value instanceof UnparseableLiteral)
if (value instanceof UnparseableLiteral unparseable)
{
if (onlyNested)
{
return;
}

final UnparseableLiteral unparseable = (UnparseableLiteral) value;
resource.addProperty(property, model.createLiteral(unparseable.getRawValue()));
}
else if (value instanceof AnyResource)
Expand Down Expand Up @@ -2169,7 +2243,7 @@ private static void recursivelyCollectNamespaceMappings(final Map<String, String
{
try {
IOslcCustomNamespaceProvider customNamespaceProviderImpl
= customNamespaceProvider.newInstance();
= customNamespaceProvider.getDeclaredConstructor().newInstance();
Map<String, String> customNamespacePrefixes = customNamespaceProviderImpl
.getCustomNamespacePrefixes();
if(null != customNamespacePrefixes)
Expand All @@ -2183,6 +2257,12 @@ private static void recursivelyCollectNamespaceMappings(final Map<String, String
} catch (InstantiationException e) {
throw new RuntimeException("The custom namespace provider must not be a abstract," +
" nor interface class and must have a public no args constructor", e);
} catch (InvocationTargetException e) {
throw new RuntimeException(
"Class '%s' does not have a no-args constructor required for Lyo Beans"
.formatted(customNamespaceProvider.getSimpleName()), e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
Expand All @@ -2197,13 +2277,8 @@ private static void recursivelyCollectNamespaceMappings(final Map<String, String

final Class<?>[] interfaces = resourceClass.getInterfaces();

if (interfaces != null)
{
for (final Class<?> interfac : interfaces)
{
recursivelyCollectNamespaceMappings(namespaceMappings,
interfac);
}
for (final Class<?> iface : interfaces) {
recursivelyCollectNamespaceMappings(namespaceMappings, iface);
}
}

Expand Down
Loading
Loading