aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Pitschke2012-11-07 17:14:50 (EST)
committerSteve Pitschke2012-11-07 17:14:50 (EST)
commitf47a879b83a4d13cf823c64df37470853462cac1 (patch)
tree118caa42565700a92e0a2513ca77f6eaa026b922
parentd18871670a2686d3fafdcbfc17d18c3b00b071c6 (diff)
downloadorg.eclipse.lyo.core-f47a879b83a4d13cf823c64df37470853462cac1.zip
org.eclipse.lyo.core-f47a879b83a4d13cf823c64df37470853462cac1.tar.gz
org.eclipse.lyo.core-f47a879b83a4d13cf823c64df37470853462cac1.tar.bz2
Bug 390919: Factor in Susumu Fukuda's Bug 390436 Workrefs/changes/90/8590/1
Hand edited in the changes to JenaModelHelper.java and JsonHelper.java which has a better implementation of RDF collections.
-rw-r--r--OSLC4JJenaProvider/src/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java467
-rw-r--r--OSLC4JJson4JProvider/src/org/eclipse/lyo/oslc4j/provider/json4j/JsonHelper.java178
2 files changed, 423 insertions, 222 deletions
diff --git a/OSLC4JJenaProvider/src/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java b/OSLC4JJenaProvider/src/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java
index 5648435..6fef21b 100644
--- a/OSLC4JJenaProvider/src/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java
+++ b/OSLC4JJenaProvider/src/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java
@@ -34,6 +34,7 @@ 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;
@@ -93,6 +94,7 @@ import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.RSIterator;
import com.hp.hpl.jena.rdf.model.ReifiedStatement;
@@ -490,8 +492,6 @@ public final class JenaModelHelper
{
Class<?> setMethodComponentParameterClass = setMethod.getParameterTypes()[0];
-
-
boolean multiple = setMethodComponentParameterClass.isArray();
if (multiple)
{
@@ -518,6 +518,32 @@ public final class JenaModelHelper
}
}
+ final List<RDFNode> objects;
+ if (multiple && object.isResource() && (
+ (object.asResource().hasProperty(RDF.first)
+ && object.asResource().hasProperty(RDF.rest))
+ || (RDF.nil.equals(object))
+ || object.canAs(RDFList.class)))
+ {
+ objects = new ArrayList<RDFNode>();
+ Resource listNode = object.asResource();
+ while (listNode != null && !RDF.nil.getURI().equals(listNode.getURI()))
+ {
+ visitedResources.put(getVisitedResourceName(listNode), new Object());
+
+ RDFNode o = listNode.getPropertyResourceValue(RDF.first);
+ objects.add(o);
+
+ listNode = listNode.getPropertyResourceValue(RDF.rest);
+ }
+
+ visitedResources.put(getVisitedResourceName(object.asResource()), objects);
+ }
+ else
+ {
+ objects = Collections.singletonList(object);
+ }
+
Class<?> reifiedClass = null;
if (IReifiedResource.class.isAssignableFrom(setMethodComponentParameterClass))
{
@@ -539,176 +565,179 @@ public final class JenaModelHelper
}
}
- Object parameter = null;
- if (object.isLiteral())
+ for (RDFNode o : objects)
{
- final Literal literal = object.asLiteral();
- final String stringValue = literal.getString();
-
- if (String.class == setMethodComponentParameterClass)
- {
- parameter = stringValue;
- }
- else if ((Boolean.class == setMethodComponentParameterClass) ||
- (Boolean.TYPE == setMethodComponentParameterClass))
+ Object parameter = null;
+ if (o.isLiteral())
{
- // XML supports both 'true' and '1' for a true Boolean.
- // Cannot use Boolean.parseBoolean since it supports case-insensitive TRUE.
- if ((Boolean.TRUE.toString().equals(stringValue)) ||
- ("1".equals(stringValue)))
+ final Literal literal = o.asLiteral();
+ final String stringValue = literal.getString();
+
+ if (String.class == setMethodComponentParameterClass)
{
- parameter = Boolean.TRUE;
+ parameter = stringValue;
}
- // XML supports both 'false' and '0' for a false Boolean.
- else if ((Boolean.FALSE.toString().equals(stringValue)) ||
- ("0".equals(stringValue)))
+ else if ((Boolean.class == setMethodComponentParameterClass) ||
+ (Boolean.TYPE == setMethodComponentParameterClass))
{
- parameter = Boolean.FALSE;
+ // XML supports both 'true' and '1' for a true Boolean.
+ // Cannot use Boolean.parseBoolean since it supports case-insensitive TRUE.
+ if ((Boolean.TRUE.toString().equals(stringValue)) ||
+ ("1".equals(stringValue)))
+ {
+ parameter = Boolean.TRUE;
+ }
+ // XML supports both 'false' and '0' for a false Boolean.
+ else if ((Boolean.FALSE.toString().equals(stringValue)) ||
+ ("0".equals(stringValue)))
+ {
+ parameter = Boolean.FALSE;
+ }
+ else
+ {
+ throw new IllegalArgumentException("'" + stringValue + "' has wrong format for Boolean.");
+ }
}
- else
+ else if ((Byte.class == setMethodComponentParameterClass) ||
+ (Byte.TYPE == setMethodComponentParameterClass))
{
- throw new IllegalArgumentException("'" + stringValue + "' has wrong format for Boolean.");
+ parameter = Byte.valueOf(stringValue);
}
- }
- else if ((Byte.class == setMethodComponentParameterClass) ||
- (Byte.TYPE == setMethodComponentParameterClass))
- {
- parameter = Byte.valueOf(stringValue);
- }
- else if ((Short.class == setMethodComponentParameterClass) ||
- (Short.TYPE == setMethodComponentParameterClass))
- {
- parameter = Short.valueOf(stringValue);
- }
- else if ((Integer.class == setMethodComponentParameterClass) ||
- (Integer.TYPE == setMethodComponentParameterClass))
- {
- parameter = Integer.valueOf(stringValue);
- }
- else if ((Long.class == setMethodComponentParameterClass) ||
- (Long.TYPE == setMethodComponentParameterClass))
- {
- parameter = Long.valueOf(stringValue);
- }
- else if (BigInteger.class == setMethodComponentParameterClass)
- {
- parameter = new BigInteger(stringValue);
- }
- else if ((Float.class == setMethodComponentParameterClass) ||
- (Float.TYPE == setMethodComponentParameterClass))
- {
- parameter = Float.valueOf(stringValue);
- }
- else if ((Double.class == setMethodComponentParameterClass) ||
- (Double.TYPE == setMethodComponentParameterClass))
- {
- parameter = Double.valueOf(stringValue);
- }
- else if (Date.class == setMethodComponentParameterClass)
- {
- parameter = DatatypeFactory.newInstance().newXMLGregorianCalendar(stringValue).toGregorianCalendar().getTime();
- }
- }
- else if (object.isResource())
- {
- final Resource nestedResource = object.asResource();
-
- if (URI.class == setMethodComponentParameterClass)
- {
- final String nestedResourceURIString = nestedResource.getURI();
-
- if (nestedResourceURIString != null)
+ else if ((Short.class == setMethodComponentParameterClass) ||
+ (Short.TYPE == setMethodComponentParameterClass))
{
- final URI nestedResourceURI = new URI(nestedResourceURIString);
-
- if (!nestedResourceURI.isAbsolute())
- {
- throw new OslcCoreRelativeURIException(beanClass,
- setMethod.getName(),
- nestedResourceURI);
- }
-
- parameter = nestedResourceURI;
+ parameter = Short.valueOf(stringValue);
+ }
+ else if ((Integer.class == setMethodComponentParameterClass) ||
+ (Integer.TYPE == setMethodComponentParameterClass))
+ {
+ parameter = Integer.valueOf(stringValue);
+ }
+ else if ((Long.class == setMethodComponentParameterClass) ||
+ (Long.TYPE == setMethodComponentParameterClass))
+ {
+ parameter = Long.valueOf(stringValue);
+ }
+ else if (BigInteger.class == setMethodComponentParameterClass)
+ {
+ parameter = new BigInteger(stringValue);
+ }
+ else if ((Float.class == setMethodComponentParameterClass) ||
+ (Float.TYPE == setMethodComponentParameterClass))
+ {
+ parameter = Float.valueOf(stringValue);
+ }
+ else if ((Double.class == setMethodComponentParameterClass) ||
+ (Double.TYPE == setMethodComponentParameterClass))
+ {
+ parameter = Double.valueOf(stringValue);
+ }
+ else if (Date.class == setMethodComponentParameterClass)
+ {
+ parameter = DatatypeFactory.newInstance().newXMLGregorianCalendar(stringValue).toGregorianCalendar().getTime();
}
}
- else
- {
- final Object nestedBean = setMethodComponentParameterClass.newInstance();
-
- fromResource(classPropertyDefinitionsToSetMethods,
- setMethodComponentParameterClass,
- nestedBean,
- nestedResource,
- visitedResources);
-
- parameter = nestedBean;
- }
- }
-
- if (parameter != null)
- {
- if (reifiedClass != null)
- {
- // This property supports reified statements. Create the
- // new resource to hold the value and any metadata.
- final Object reifiedResource = reifiedClass.newInstance();
-
- // Find a setter for the actual value.
- for (Method method : reifiedClass.getMethods())
- {
- if (!"setValue".equals(method.getName()))
- {
- continue;
- }
- final Class<?>[] parameterTypes = method.getParameterTypes();
- if (parameterTypes.length == 1 && parameterTypes[0].isAssignableFrom(setMethodComponentParameterClass))
- {
- method.invoke(reifiedResource, parameter);
- break;
- }
- }
-
- // Fill in any reified statements.
- RSIterator rsIter = statement.listReifiedStatements();
- while (rsIter.hasNext())
- {
- ReifiedStatement reifiedStatement = rsIter.next();
- fromResource(classPropertyDefinitionsToSetMethods,
- reifiedClass,
- reifiedResource,
- reifiedStatement,
- visitedResources);
- }
-
- parameter = reifiedResource;
- }
-
- if (multiple)
+ else if (o.isResource())
{
- List<Object> values = propertyDefinitionsToArrayValues.get(uri);
- if (values == null)
+ final Resource nestedResource = o.asResource();
+
+ if (URI.class == setMethodComponentParameterClass)
{
- values = new ArrayList<Object>();
-
- propertyDefinitionsToArrayValues.put(uri,
- values);
+ final String nestedResourceURIString = nestedResource.getURI();
+
+ if (nestedResourceURIString != null)
+ {
+ final URI nestedResourceURI = new URI(nestedResourceURIString);
+
+ if (!nestedResourceURI.isAbsolute())
+ {
+ throw new OslcCoreRelativeURIException(beanClass,
+ setMethod.getName(),
+ nestedResourceURI);
+ }
+
+ parameter = nestedResourceURI;
+ }
+ }
+ else
+ {
+ final Object nestedBean = setMethodComponentParameterClass.newInstance();
+
+ fromResource(classPropertyDefinitionsToSetMethods,
+ setMethodComponentParameterClass,
+ nestedBean,
+ nestedResource,
+ visitedResources);
+
+ parameter = nestedBean;
}
-
- values.add(parameter);
}
- else
+
+ if (parameter != null)
{
- if (singleValueMethodsUsed.contains(setMethod))
+ if (reifiedClass != null)
+ {
+ // This property supports reified statements. Create the
+ // new resource to hold the value and any metadata.
+ final Object reifiedResource = reifiedClass.newInstance();
+
+ // Find a setter for the actual value.
+ for (Method method : reifiedClass.getMethods())
+ {
+ if (!"setValue".equals(method.getName()))
+ {
+ continue;
+ }
+ final Class<?>[] parameterTypes = method.getParameterTypes();
+ if (parameterTypes.length == 1 && parameterTypes[0].isAssignableFrom(setMethodComponentParameterClass))
+ {
+ method.invoke(reifiedResource, parameter);
+ break;
+ }
+ }
+
+ // Fill in any reified statements.
+ RSIterator rsIter = statement.listReifiedStatements();
+ while (rsIter.hasNext())
+ {
+ ReifiedStatement reifiedStatement = rsIter.next();
+ fromResource(classPropertyDefinitionsToSetMethods,
+ reifiedClass,
+ reifiedResource,
+ reifiedStatement,
+ visitedResources);
+ }
+
+ parameter = reifiedResource;
+ }
+
+ if (multiple)
{
- throw new OslcCoreMisusedOccursException(beanClass,
- setMethod);
+ List<Object> values = propertyDefinitionsToArrayValues.get(uri);
+ if (values == null)
+ {
+ values = new ArrayList<Object>();
+
+ propertyDefinitionsToArrayValues.put(uri,
+ values);
+ }
+
+ values.add(parameter);
+ }
+ else
+ {
+ if (singleValueMethodsUsed.contains(setMethod))
+ {
+ throw new OslcCoreMisusedOccursException(beanClass,
+ setMethod);
+ }
+
+
+ setMethod.invoke(bean,
+ new Object[] {parameter});
+
+ singleValueMethodsUsed.add(setMethod);
}
-
-
- setMethod.invoke(bean,
- new Object[] {parameter});
-
- singleValueMethodsUsed.add(setMethod);
}
}
}
@@ -1202,7 +1231,8 @@ public final class JenaModelHelper
resource,
property,
nestedProperties,
- onlyNested);
+ onlyNested,
+ null);
}
else if (value instanceof Date)
{
@@ -1284,26 +1314,27 @@ public final class JenaModelHelper
final boolean xmlLiteral = valueTypeAnnotation == null ? false : ValueType.XMLLiteral.equals(valueTypeAnnotation.value());
- Property attribute = model.createProperty(propertyDefinition);
+ final Property attribute = model.createProperty(propertyDefinition);
final Class<?> returnType = method.getReturnType();
+ final OslcRdfCollectionType collectionType =
+ InheritedMethodAnnotationHelper.getAnnotation(method,
+ OslcRdfCollectionType.class);
+ final List<RDFNode> rdfNodeContainer;
+
+ if (collectionType != null &&
+ OslcConstants.RDF_NAMESPACE.equals(collectionType.namespaceURI()) &&
+ "List".equals(collectionType.collectionType()))
+ {
+ rdfNodeContainer = new ArrayList<RDFNode>();
+ }
+ else
+ {
+ rdfNodeContainer = null;
+ }
if (returnType.isArray())
{
- final OslcRdfCollectionType collectionType =
- InheritedMethodAnnotationHelper.getAnnotation(method,
- OslcRdfCollectionType.class);
- if (collectionType != null)
- {
- Resource origResource = resource;
-
- resource = model.createResource(model.createProperty(collectionType.namespaceURI(),
- collectionType.collectionType()));
- model.add(model.createStatement(origResource, attribute, resource));
-
- attribute = model.createProperty(OslcConstants.RDFS_NAMESPACE + "member");
- }
-
// We cannot cast to Object[] in case this is an array of
// primitives. We will use Array reflection instead.
// Strange case about primitive arrays: they cannot be cast to
@@ -1327,25 +1358,20 @@ public final class JenaModelHelper
resource,
attribute,
nestedProperties,
- onlyNested);
+ onlyNested,
+ rdfNodeContainer);
+ }
+
+ // XXX - Need to generalize to arbitrary RDF collections
+ if (rdfNodeContainer != null)
+ {
+ RDFList list = model.createList(rdfNodeContainer.iterator());
+ Statement s = model.createStatement(resource, attribute, list);
+ model.add(s);
}
}
else if (Collection.class.isAssignableFrom(returnType))
{
- final OslcRdfCollectionType collectionType =
- InheritedMethodAnnotationHelper.getAnnotation(method,
- OslcRdfCollectionType.class);
- if (collectionType != null)
- {
- Resource origResource = resource;
-
- resource = model.createResource(model.createProperty(collectionType.namespaceURI(),
- collectionType.collectionType()));
- model.add(model.createStatement(origResource, attribute, resource));
-
- attribute = model.createProperty(OslcConstants.RDFS_NAMESPACE + "member");
- }
-
@SuppressWarnings("unchecked")
final Collection<Object> collection = (Collection<Object>) value;
@@ -1359,7 +1385,16 @@ public final class JenaModelHelper
resource,
attribute,
nestedProperties,
- onlyNested);
+ onlyNested,
+ rdfNodeContainer);
+ }
+
+ // XXX - Need to generalize to arbitrary RDF collections
+ if (rdfNodeContainer != null)
+ {
+ RDFList list = model.createList(rdfNodeContainer.iterator());
+ Statement s = model.createStatement(resource, attribute, list);
+ model.add(s);
}
}
else
@@ -1372,7 +1407,8 @@ public final class JenaModelHelper
resource,
attribute,
nestedProperties,
- onlyNested);
+ onlyNested,
+ rdfNodeContainer);
}
}
@@ -1384,7 +1420,8 @@ public final class JenaModelHelper
final Resource resource,
final Property attribute,
final Map<String, Object> nestedProperties,
- final boolean onlyNested)
+ final boolean onlyNested,
+ final List<RDFNode> rdfNodeContainer)
throws DatatypeConfigurationException,
IllegalAccessException,
IllegalArgumentException,
@@ -1393,7 +1430,7 @@ public final class JenaModelHelper
{
final Class<? extends Object> objectClass = object.getClass();
- Statement statement = null;
+ RDFNode nestedNode = null;
final IReifiedResource<?> reifiedResource = (object instanceof IReifiedResource) ? (IReifiedResource<?>) object : null;
final Object value = (reifiedResource == null) ? object : reifiedResource.getValue();
@@ -1406,14 +1443,12 @@ public final class JenaModelHelper
if (xmlLiteral)
{
- statement = model.createStatement(resource,
- attribute,
- model.createTypedLiteral(value.toString(),
- RDF_TYPE_XMLLITERAL));
+ nestedNode = model.createTypedLiteral(value.toString(),
+ RDF_TYPE_XMLLITERAL);
}
else
{
- statement = model.createStatement(resource, attribute, value.toString());
+ nestedNode = model.createLiteral(value.toString());
}
}
else if ((value instanceof Boolean) ||
@@ -1424,7 +1459,7 @@ public final class JenaModelHelper
return;
}
- statement = model.createStatement(resource, attribute, value.toString());
+ nestedNode = model.createLiteral(value.toString());
}
else if (value instanceof URI)
{
@@ -1443,7 +1478,7 @@ public final class JenaModelHelper
}
// URIs represent references to other resources identified by their IDs, so they need to be managed as such
- statement = model.createStatement(resource, attribute, model.createResource(value.toString()));
+ nestedNode = model.createResource(value.toString());
}
else if (value instanceof Date)
{
@@ -1457,7 +1492,7 @@ public final class JenaModelHelper
final String string = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar).toString();
- statement = model.createStatement(resource, attribute, string);
+ nestedNode = model.createLiteral(string);
}
else if (objectClass.getAnnotation(OslcResourceShape.class) != null)
{
@@ -1496,19 +1531,37 @@ public final class JenaModelHelper
nestedResource,
nestedProperties);
- statement = model.createStatement(resource, attribute, nestedResource);
+ nestedNode = nestedResource;
}
- if (statement != null)
+ if (nestedNode != null)
{
- if (reifiedResource != null &&
- nestedProperties != OSLC4JConstants.OSL4J_PROPERTY_SINGLETON)
- {
- addReifiedStatements(model, statement, reifiedResource, nestedProperties);
- }
-
- // Finally, add the statement to the model.
- model.add(statement);
+ if (rdfNodeContainer != null)
+ {
+ if (reifiedResource != null)
+ {
+ // Reified resource is not supported for rdf collection resources
+ throw new OslcCoreInvalidPropertyDefinitionException(resourceClass,
+ method,
+ null);
+ }
+
+ // Instead of adding a nested node to model, add it to a list
+ rdfNodeContainer.add(nestedNode);
+ }
+ else
+ {
+ Statement statement = model.createStatement(resource, attribute, nestedNode);
+
+ if (reifiedResource != null &&
+ nestedProperties != OSLC4JConstants.OSL4J_PROPERTY_SINGLETON)
+ {
+ addReifiedStatements(model, statement, reifiedResource, nestedProperties);
+ }
+
+ // Finally, add the statement to the model.
+ model.add(statement);
+ }
}
}
diff --git a/OSLC4JJson4JProvider/src/org/eclipse/lyo/oslc4j/provider/json4j/JsonHelper.java b/OSLC4JJson4JProvider/src/org/eclipse/lyo/oslc4j/provider/json4j/JsonHelper.java
index ffe246b..bf0dd48 100644
--- a/OSLC4JJson4JProvider/src/org/eclipse/lyo/oslc4j/provider/json4j/JsonHelper.java
+++ b/OSLC4JJson4JProvider/src/org/eclipse/lyo/oslc4j/provider/json4j/JsonHelper.java
@@ -63,6 +63,7 @@ import org.eclipse.lyo.oslc4j.core.SingletonWildcardProperties;
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.exception.OslcCoreApplicationException;
@@ -73,13 +74,13 @@ import org.eclipse.lyo.oslc4j.core.exception.OslcCoreMissingNamespacePrefixExcep
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreMissingSetMethodException;
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.IExtendedResource;
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.OslcConstants;
import org.eclipse.lyo.oslc4j.core.model.TypeFactory;
-import org.eclipse.lyo.oslc4j.core.model.AnyResource;
final class JsonHelper
{
@@ -93,10 +94,14 @@ final class JsonHelper
private static final String JSON_PROPERTY_SUFFIX_TOTAL_COUNT = "totalCount";
private static final String JSON_PROPERTY_SUFFIX_NEXT_PAGE = "nextPage";
private static final String JSON_PROPERTY_SUFFIX_TYPE = "type";
+ private static final String JSON_PROPERTY_SUFFIX_FIRST = "first";
+ private static final String JSON_PROPERTY_SUFFIX_REST = "rest";
+ private static final String JSON_PROPERTY_SUFFIX_NIL = "nil";
private static final String RDF_ABOUT_URI = OslcConstants.RDF_NAMESPACE + JSON_PROPERTY_SUFFIX_ABOUT;
private static final String RDF_TYPE_URI = OslcConstants.RDF_NAMESPACE + JSON_PROPERTY_SUFFIX_TYPE;
-
+ private static final String RDF_NIL_URI = OslcConstants.RDF_NAMESPACE + JSON_PROPERTY_SUFFIX_NIL;
+
private static final String METHOD_NAME_START_GET = "get";
private static final String METHOD_NAME_START_IS = "is";
private static final String METHOD_NAME_START_SET = "set";
@@ -388,6 +393,23 @@ final class JsonHelper
method,
propertyDefinitionAnnotation);
}
+
+ final boolean isRdfList;
+
+ final OslcRdfCollectionType collectionType =
+ InheritedMethodAnnotationHelper.getAnnotation(method,
+ OslcRdfCollectionType.class);
+
+ if (collectionType != null &&
+ OslcConstants.RDF_NAMESPACE.equals(collectionType.namespaceURI()) &&
+ "List".equals(collectionType.collectionType()))
+ {
+ isRdfList = true;
+ }
+ else
+ {
+ isRdfList = false;
+ }
final Object localResourceValue;
@@ -421,13 +443,35 @@ final class JsonHelper
}
}
- if (jsonArray.size() > 0)
+ if (isRdfList)
{
- localResourceValue = jsonArray;
- }
- else
+ JSONObject listObject = new JSONObject();
+ listObject.put("rdf" + JSON_PROPERTY_DELIMITER + "resource", OslcConstants.RDF_NAMESPACE + "nil");
+
+ for (int i = jsonArray.size() - 1; i >= 0; i --)
+ {
+ Object o = jsonArray.get(i);
+
+ JSONObject newListObject = new JSONObject();
+ newListObject.put("rdf" + JSON_PROPERTY_DELIMITER + "first", o);
+ newListObject.put("rdf" + JSON_PROPERTY_DELIMITER + "rest", listObject);
+
+ listObject = newListObject;
+ }
+
+ localResourceValue = listObject;
+
+ }
+ else
{
- localResourceValue = null;
+ if (jsonArray.size() > 0)
+ {
+ localResourceValue = jsonArray;
+ }
+ else
+ {
+ localResourceValue = null;
+ }
}
}
else if (Collection.class.isAssignableFrom(returnType))
@@ -452,13 +496,35 @@ final class JsonHelper
}
}
- if (jsonArray.size() > 0)
+ if (isRdfList)
{
- localResourceValue = jsonArray;
- }
- else
+ JSONObject listObject = new JSONObject();
+ listObject.put("rdf" + JSON_PROPERTY_DELIMITER + "resource", OslcConstants.RDF_NAMESPACE + "nil");
+
+ for (int i = jsonArray.size() - 1; i >= 0; i --)
+ {
+ Object o = jsonArray.get(i);
+
+ JSONObject newListObject = new JSONObject();
+ newListObject.put("rdf" + JSON_PROPERTY_DELIMITER + "first", o);
+ newListObject.put("rdf" + JSON_PROPERTY_DELIMITER + "rest", listObject);
+
+ listObject = newListObject;
+ }
+
+ localResourceValue = listObject;
+
+ }
+ else
{
- localResourceValue = null;
+ if (jsonArray.size() > 0)
+ {
+ localResourceValue = jsonArray;
+ }
+ else
+ {
+ localResourceValue = null;
+ }
}
}
else
@@ -1451,6 +1517,64 @@ final class JsonHelper
}
}
+ private static boolean isRdfListNode(final String rdfPrefix,
+ final Class<?> beanClass,
+ final Method setMethod,
+ final Object jsonValue)
+ {
+ if (!(jsonValue instanceof JSONObject))
+ {
+ return false;
+ }
+
+ final JSONObject jsonObject = (JSONObject)jsonValue;
+
+ final boolean isListNode =
+ jsonObject.has(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_FIRST)
+ && jsonObject.has(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_REST);
+ if (isListNode)
+ {
+ return true;
+ }
+
+ final boolean isNilResource = RDF_NIL_URI.equals(
+ jsonObject.optString(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_RESOURCE));
+ if (!isNilResource)
+ {
+ return false;
+ }
+
+ final String setMethodName = setMethod.getName();
+ if (setMethodName.startsWith(METHOD_NAME_START_SET)) {
+ String getMethodName = METHOD_NAME_START_GET + setMethodName.substring(METHOD_NAME_START_GET_LENGTH);
+ Method getMethod;
+ try {
+ getMethod = beanClass.getMethod(getMethodName, new Class[0]);
+ } catch (NoSuchMethodException e) {
+
+ String isMethodName = METHOD_NAME_START_IS + setMethodName.substring(METHOD_NAME_START_GET_LENGTH);
+ try {
+ getMethod = beanClass.getMethod(isMethodName, new Class[0]);
+ } catch (NoSuchMethodException e1) {
+ return false;
+ }
+ }
+
+ final OslcRdfCollectionType collectionType =
+ InheritedMethodAnnotationHelper.getAnnotation(getMethod,
+ OslcRdfCollectionType.class);
+
+ if (collectionType != null &&
+ OslcConstants.RDF_NAMESPACE.equals(collectionType.namespaceURI()) &&
+ "List".equals(collectionType.collectionType()))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
private static Object fromJSONValue(final String rdfPrefix,
final Map<String, String> jsonNamespaceMappings,
final Map<Class<?>, Map<String, Method>> classPropertyDefinitionsToSetMethods,
@@ -1467,7 +1591,9 @@ final class JsonHelper
OslcCoreApplicationException,
URISyntaxException
{
- if (jsonValue instanceof JSONObject)
+ boolean isRdfListNode = isRdfListNode(rdfPrefix, beanClass, setMethod, jsonValue);
+
+ if (!isRdfListNode && jsonValue instanceof JSONObject)
{
final JSONObject nestedJSONObject = (JSONObject) jsonValue;
@@ -1502,9 +1628,31 @@ final class JsonHelper
return nestedBean;
}
- else if (jsonValue instanceof JSONArray)
+ else if (jsonValue instanceof JSONArray || isRdfListNode)
{
- final JSONArray jsonArray = (JSONArray) jsonValue;
+ final Collection<Object> jsonArray;
+
+ if (isRdfListNode)
+ {
+ jsonArray = new ArrayList<Object>();
+
+ JSONObject listNode = (JSONObject) jsonValue;
+ while (listNode != null
+ && !RDF_NIL_URI.equals(
+ listNode.opt(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_RESOURCE))) {
+
+ Object o = listNode.opt(
+ rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_FIRST);
+ jsonArray.add(o);
+
+ listNode = listNode.optJSONObject(
+ rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_REST);
+ }
+ }
+ else
+ {
+ jsonArray = (JSONArray) jsonValue;
+ }
final ArrayList<Object> tempList = new ArrayList<Object>();