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

feat: Handle XMLLiteral Java type #214

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ trim_trailing_whitespace = true
[*.{java,kt}]
indent_style = space
indent_size = 4
max_line_length = 120

# indent code easily
[*.md]
Expand All @@ -20,3 +21,4 @@ indent_size = 4
[*.{xml,html,js,yml,css}]
indent_style = space
indent_size = 2
max_line_length = 100
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,18 @@
*/
package org.eclipse.lyo.oslc4j.core.model;

import org.eclipse.lyo.oslc4j.core.annotation.*;
import org.eclipse.lyo.oslc4j.core.exception.*;

import javax.ws.rs.core.UriBuilder;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import org.eclipse.lyo.oslc4j.core.CoreHelper;
Expand Down Expand Up @@ -98,6 +94,8 @@ public class ResourceShapeFactory {
CLASS_TO_VALUE_TYPE.put(String.class, ValueType.String);
CLASS_TO_VALUE_TYPE.put(Date.class, ValueType.DateTime);
CLASS_TO_VALUE_TYPE.put(URI.class, ValueType.Resource);

CLASS_TO_VALUE_TYPE.put(XMLLiteral.class, ValueType.XMLLiteral);
}

protected ResourceShapeFactory() {
Expand Down Expand Up @@ -435,7 +433,9 @@ static boolean isCollectionType(final Class<?> returnType) {
return Collection.class.isAssignableFrom(returnType);
}

private static void validateUserSpecifiedOccurs(final Class<?> resourceClass, final Method method, final OslcOccurs occursAnnotation) throws OslcCoreInvalidOccursException {
private static void validateUserSpecifiedOccurs(final Class<?> resourceClass, final Method method,
final OslcOccurs occursAnnotation)
throws OslcCoreInvalidOccursException {
final Class<?> returnType = method.getReturnType();
final Occurs occurs = occursAnnotation.value();

Expand All @@ -453,66 +453,74 @@ private static void validateUserSpecifiedOccurs(final Class<?> resourceClass, fi
}
}

protected static void validateUserSpecifiedValueType(final Class<?> resourceClass, final Method method, final ValueType userSpecifiedValueType, final Representation userSpecifiedRepresentation, final Class<?> componentType) throws OslcCoreInvalidValueTypeException {
final ValueType calculatedValueType = CLASS_TO_VALUE_TYPE.get(componentType);
// ValueType is valid if ...
// user-specified value type matches calculated value type
// or
// user-specified value type is local resource (we will validate the local resource later)
protected static void validateUserSpecifiedValueType(final Class<?> resourceClass, final Method method,
final ValueType userSpecifiedValueType,
final Representation userSpecifiedRepresentation,
final Class<?> componentType)
throws OslcCoreInvalidValueTypeException {
final ValueType calculatedValueType = CLASS_TO_VALUE_TYPE.get(componentType);
// ValueType is valid if ...
// user-specified value type matches calculated value type
// or
// user-specified value type is local resource (we will validate the local resource later)
// or
// user-specified value type is non-local resource, and Representation is Inline (we will validate later)
// or
// user-specified value type is xml literal and calculated value type is string
// or
// user-specified value type is decimal and calculated value type is numeric
if ((userSpecifiedValueType.equals(calculatedValueType))
||
(ValueType.LocalResource.equals(userSpecifiedValueType))
||
(ValueType.Resource.equals(userSpecifiedValueType) && (null != userSpecifiedRepresentation) && (Representation.Inline.equals(userSpecifiedRepresentation)))
// or
// user-specified value type is xml literal and calculated value type is string
// or
// user-specified value type is decimal and calculated value type is numeric
if ((userSpecifiedValueType.equals(calculatedValueType))
||
((ValueType.XMLLiteral.equals(userSpecifiedValueType))
&&
(ValueType.String.equals(calculatedValueType))
)
||
((ValueType.Decimal.equals(userSpecifiedValueType))
&&
((ValueType.Double.equals(calculatedValueType))
||
(ValueType.Float.equals(calculatedValueType))
||
(ValueType.Integer.equals(calculatedValueType))
)
)
) {
// We have a valid user-specified value type for our Java type
return;
}
(ValueType.LocalResource.equals(userSpecifiedValueType))
||
(ValueType.Resource.equals(userSpecifiedValueType)
&& (Representation.Inline.equals(userSpecifiedRepresentation)))
||
((ValueType.XMLLiteral.equals(userSpecifiedValueType))
&&
(ValueType.String.equals(calculatedValueType))
)
||
((ValueType.Decimal.equals(userSpecifiedValueType))
&&
((ValueType.Double.equals(calculatedValueType))
||
(ValueType.Float.equals(calculatedValueType))
||
(ValueType.Integer.equals(calculatedValueType))
)
)
) {
// We have a valid user-specified value type for our Java type
return;
}

throw new OslcCoreInvalidValueTypeException(resourceClass, method, userSpecifiedValueType);
}
throw new OslcCoreInvalidValueTypeException(resourceClass, method, userSpecifiedValueType);
}

private static void validateUserSpecifiedRepresentation(final Class<?> resourceClass, final Method method, final Representation userSpecifiedRepresentation, final Class<?> componentType) throws OslcCoreInvalidRepresentationException {
private static void validateUserSpecifiedRepresentation(final Class<?> resourceClass, final Method method,
final Representation userSpecifiedRepresentation,
final Class<?> componentType)
throws OslcCoreInvalidRepresentationException {
// Throw an Exception if ...
// User-specified representation is reference and component is not URI
// or
// user-specified representation is inline and component is a standard class

if (null == userSpecifiedRepresentation) {
return;
}
if (((Representation.Reference.equals(userSpecifiedRepresentation))
&&
(!URI.class.equals(componentType))
)
||
((Representation.Inline.equals(userSpecifiedRepresentation))
&&
(CLASS_TO_VALUE_TYPE.containsKey(componentType))
)
) {
throw new OslcCoreInvalidRepresentationException(resourceClass, method, userSpecifiedRepresentation);
}
if (null == userSpecifiedRepresentation) {
return;
}
if (((Representation.Reference.equals(userSpecifiedRepresentation))
&&
(!URI.class.equals(componentType))
)
||
((Representation.Inline.equals(userSpecifiedRepresentation))
&&
(CLASS_TO_VALUE_TYPE.containsKey(componentType))
)
) {
throw new OslcCoreInvalidRepresentationException(resourceClass, method, userSpecifiedRepresentation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ else if (!(bean instanceof AbstractResource))
final String uri = predicate.getURI();
final Method setMethod = setMethodMap.get(uri);

//TODO: split into readExtendedProperty and readAnnotatedProperty
if (setMethod == null)
{
if (RDF_TYPE_URI.equals(uri))
Expand Down Expand Up @@ -809,17 +810,18 @@ else if (Collection.class.isAssignableFrom(setMethodComponentParameterClass))

for (RDFNode o : objects)
{
// read/set a non-generic non-collection annotated property
Object parameter = null;
if (o.isLiteral())
{
final Literal literal = o.asLiteral();
final String stringValue = literal.getString();

if (String.class == setMethodComponentParameterClass)
{
if (String.class == setMethodComponentParameterClass) {
parameter = stringValue;
}
else if ((Boolean.class == setMethodComponentParameterClass) ||
} else if (XMLLiteral.class == setMethodComponentParameterClass) {
parameter = new XMLLiteral(literal.getString());
} else if ((Boolean.class == setMethodComponentParameterClass) ||
(Boolean.TYPE == setMethodComponentParameterClass))
{
// XML supports both 'true' and '1' for a true Boolean.
Expand Down Expand Up @@ -1337,6 +1339,7 @@ private static void buildResource(final Object object,

if (value != null)
{
// serializing an annotated property value
Map<String, Object> nestedProperties = null;
boolean onlyNested = false;

Expand Down Expand Up @@ -1909,22 +1912,24 @@ private static void handleLocalResource(final Class<?> resourceClass,
return;
}

if (value instanceof String)
{
if (onlyNested)
{
if (value instanceof String) {
if (onlyNested) {
return;
}

if (xmlLiteral)
{
if (xmlLiteral) {
nestedNode = model.createTypedLiteral(value.toString(),
XMLLiteralType.theXMLLiteralType);
}
else
{
XMLLiteralType.theXMLLiteralType);
} else {
nestedNode = model.createLiteral(value.toString());
}
} else if (value instanceof XMLLiteral) {
if (xmlLiteral) {
nestedNode = model.createTypedLiteral(((XMLLiteral) value).getValue(),
XMLLiteralType.theXMLLiteralType);
} else {
throw new IllegalStateException("xmlLiteral flag not set on a value of type XMLLiteral");
}
}
// Floats need special handling for infinite values.
else if (value instanceof Float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreMissingSetMethodException;
import org.junit.Test;

import org.eclipse.lyo.oslc4j.core.exception.OslcCoreInvalidValueTypeException;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreMissingSetMethodException;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreMissingSetMethodException;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;

/**
* @version $version-stub$
Expand Down Expand Up @@ -49,6 +59,26 @@ public void findSetterCollectionMismachGenType()
ResourceShapeFactory.validateSetMethodExists(Dummy.class, Dummy.class.getMethod("getValueDifferent"));
}

@Test
public void validateUserSpecifiedValueType() {
assertEquals(true, isValidOslcJavaTypePair(ValueType.XMLLiteral, String.class));
assertEquals(true, isValidOslcJavaTypePair(ValueType.XMLLiteral, XMLLiteral.class));
assertEquals(true, isValidOslcJavaTypePair(ValueType.String, String.class));
assertEquals(false, isValidOslcJavaTypePair(ValueType.String, XMLLiteral.class));
}

private boolean isValidOslcJavaTypePair(ValueType specifiedValueType, Class<?> componentType) {
try {
ResourceShapeFactory.validateUserSpecifiedValueType(Dummy.class, Dummy.class.getMethod("getValueDifferent"),
specifiedValueType, null, componentType);
return true;
} catch (OslcCoreInvalidValueTypeException e) {
return false;
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
}
}

interface Dummy {
public ArrayList<BigDecimal> getValue();

Expand Down
Loading
Loading