aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Pitschke2012-11-08 14:21:59 (EST)
committerSteve Pitschke2012-11-08 14:21:59 (EST)
commit1d1c913ae9b3e89d96c5691e9f9e77a6b8ae8de0 (patch)
tree9064c8f3e38ccbb4962a793080e240b17ed25057
parentf47a879b83a4d13cf823c64df37470853462cac1 (diff)
downloadorg.eclipse.lyo.core-1d1c913ae9b3e89d96c5691e9f9e77a6b8ae8de0.zip
org.eclipse.lyo.core-1d1c913ae9b3e89d96c5691e9f9e77a6b8ae8de0.tar.gz
org.eclipse.lyo.core-1d1c913ae9b3e89d96c5691e9f9e77a6b8ae8de0.tar.bz2
Bug 390919: Add in Support for rdf:Alt, rdf:Bag and rdf:Seqrefs/changes/10/8610/1
Discovered a couple of errors in further testing.
-rw-r--r--OSLC4JJenaProvider/src/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java81
-rw-r--r--OSLC4JJson4JProvider/src/org/eclipse/lyo/oslc4j/provider/json4j/JsonHelper.java148
2 files changed, 178 insertions, 51 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 6fef21b..f0fad97 100644
--- a/OSLC4JJenaProvider/src/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java
+++ b/OSLC4JJenaProvider/src/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java
@@ -90,9 +90,11 @@ import org.w3c.dom.Element;
import com.hp.hpl.jena.datatypes.BaseDatatype;
import com.hp.hpl.jena.datatypes.DatatypeFormatException;
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
+import com.hp.hpl.jena.rdf.model.Container;
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.NodeIterator;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
@@ -112,6 +114,11 @@ public final class JenaModelHelper
private static final String RDF_TYPE_URI = OslcConstants.RDF_NAMESPACE + "type";
private static final BaseDatatype RDF_TYPE_XMLLITERAL = new BaseDatatype(OslcConstants.RDF_NAMESPACE + "XMLLiteral");
+
+ private static final String RDF_LIST = "List";
+ private static final String RDF_ALT = "Alt";
+ private static final String RDF_BAG = "Bag";
+ private static final String RDF_SEQ = "Seq";
private static final String METHOD_NAME_START_GET = "get";
private static final String METHOD_NAME_START_IS = "is";
@@ -539,6 +546,25 @@ public final class JenaModelHelper
visitedResources.put(getVisitedResourceName(object.asResource()), objects);
}
+ else if (multiple && object.canAs(Container.class))
+ {
+ objects = new ArrayList<RDFNode>();
+ NodeIterator iterator = object.as(Container.class).iterator();
+
+ while (iterator.hasNext())
+ {
+ RDFNode o = iterator.nextNode();
+
+ if (o.isResource())
+ {
+ visitedResources.put(getVisitedResourceName(o.asResource()), new Object());
+ }
+
+ objects.add(o);
+ }
+
+ visitedResources.put(getVisitedResourceName(object.asResource()), objects);
+ }
else
{
objects = Collections.singletonList(object);
@@ -1324,7 +1350,10 @@ public final class JenaModelHelper
if (collectionType != null &&
OslcConstants.RDF_NAMESPACE.equals(collectionType.namespaceURI()) &&
- "List".equals(collectionType.collectionType()))
+ (RDF_LIST.equals(collectionType.collectionType())
+ || RDF_ALT.equals(collectionType.collectionType())
+ || RDF_BAG.equals(collectionType.collectionType())
+ || RDF_SEQ.equals(collectionType.collectionType())))
{
rdfNodeContainer = new ArrayList<RDFNode>();
}
@@ -1362,11 +1391,13 @@ public final class JenaModelHelper
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);
+ RDFNode container = createRdfContainer(collectionType,
+ rdfNodeContainer,
+ model);
+ Statement s = model.createStatement(resource, attribute, container);
+
model.add(s);
}
}
@@ -1389,11 +1420,13 @@ public final class JenaModelHelper
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);
+ RDFNode container = createRdfContainer(collectionType,
+ rdfNodeContainer,
+ model);
+ Statement s = model.createStatement(resource, attribute, container);
+
model.add(s);
}
}
@@ -1408,8 +1441,40 @@ public final class JenaModelHelper
attribute,
nestedProperties,
onlyNested,
- rdfNodeContainer);
+ null);
+ }
+ }
+
+ private static RDFNode createRdfContainer(final OslcRdfCollectionType collectionType,
+ final List<RDFNode> rdfNodeContainer,
+ final Model model)
+ {
+ if (RDF_LIST.equals(collectionType.collectionType()))
+ {
+ return model.createList(rdfNodeContainer.iterator());
+ }
+
+ Container container;
+
+ if (RDF_ALT.equals(collectionType.collectionType()))
+ {
+ container = model.createAlt();
+ }
+ else if (RDF_BAG.equals(collectionType.collectionType()))
+ {
+ container = model.createBag();
}
+ else
+ {
+ container = model.createSeq();
+ }
+
+ for (RDFNode node : rdfNodeContainer)
+ {
+ container.add(node);
+ }
+
+ return container;
}
private static void handleLocalResource(final Class<?> resourceClass,
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 bf0dd48..6eb5255 100644
--- a/OSLC4JJson4JProvider/src/org/eclipse/lyo/oslc4j/provider/json4j/JsonHelper.java
+++ b/OSLC4JJson4JProvider/src/org/eclipse/lyo/oslc4j/provider/json4j/JsonHelper.java
@@ -97,6 +97,10 @@ final class JsonHelper
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 JSON_PROPERTY_SUFFIX_LIST = "List";
+ private static final String JSON_PROPERTY_SUFFIX_ALT = "Alt";
+ private static final String JSON_PROPERTY_SUFFIX_BAG = "Bag";
+ private static final String JSON_PROPERTY_SUFFIX_SEQ = "Seq";
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;
@@ -394,7 +398,7 @@ final class JsonHelper
propertyDefinitionAnnotation);
}
- final boolean isRdfList;
+ final boolean isRdfContainer;
final OslcRdfCollectionType collectionType =
InheritedMethodAnnotationHelper.getAnnotation(method,
@@ -402,13 +406,16 @@ final class JsonHelper
if (collectionType != null &&
OslcConstants.RDF_NAMESPACE.equals(collectionType.namespaceURI()) &&
- "List".equals(collectionType.collectionType()))
+ (JSON_PROPERTY_SUFFIX_LIST.equals(collectionType.collectionType())
+ || JSON_PROPERTY_SUFFIX_ALT.equals(collectionType.collectionType())
+ || JSON_PROPERTY_SUFFIX_BAG.equals(collectionType.collectionType())
+ || JSON_PROPERTY_SUFFIX_SEQ.equals(collectionType.collectionType())))
{
- isRdfList = true;
+ isRdfContainer = true;
}
else
{
- isRdfList = false;
+ isRdfContainer = false;
}
final Object localResourceValue;
@@ -443,24 +450,11 @@ final class JsonHelper
}
}
- if (isRdfList)
+ if (isRdfContainer)
{
- 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;
-
+ localResourceValue = buildContainer(namespaceMappings,
+ reverseNamespaceMappings,
+ collectionType, jsonArray);
}
else
{
@@ -496,24 +490,11 @@ final class JsonHelper
}
}
- if (isRdfList)
+ if (isRdfContainer)
{
- 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;
-
+ localResourceValue = buildContainer(namespaceMappings,
+ reverseNamespaceMappings,
+ collectionType, jsonArray);
}
else
{
@@ -554,6 +535,47 @@ final class JsonHelper
localResourceValue);
}
}
+
+ private static Object buildContainer(final Map<String, String> namespaceMappings,
+ final Map<String, String> reverseNamespaceMappings,
+ final OslcRdfCollectionType collectionType,
+ final JSONArray jsonArray)
+ throws JSONException
+ {
+ // Ensure we have an rdf prefix
+ final String rdfPrefix = ensureNamespacePrefix(OslcConstants.RDF_NAMESPACE_PREFIX,
+ OslcConstants.RDF_NAMESPACE,
+ namespaceMappings,
+ reverseNamespaceMappings);
+
+ if (JSON_PROPERTY_SUFFIX_LIST.equals(collectionType.collectionType()))
+ {
+ JSONObject listObject = new JSONObject();
+
+ listObject.put(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_RESOURCE,
+ OslcConstants.RDF_NAMESPACE + JSON_PROPERTY_SUFFIX_NIL);
+
+ for (int i = jsonArray.size() - 1; i >= 0; i --)
+ {
+ Object o = jsonArray.get(i);
+
+ JSONObject newListObject = new JSONObject();
+ newListObject.put(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_FIRST, o);
+ newListObject.put(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_REST, listObject);
+
+ listObject = newListObject;
+ }
+
+ return listObject;
+ }
+
+ JSONObject container = new JSONObject();
+
+ container.put(rdfPrefix + JSON_PROPERTY_DELIMITER + collectionType.collectionType(),
+ jsonArray);
+
+ return container;
+ }
private static void buildResource(final Map<String, String> namespaceMappings,
final Map<String, String> reverseNamespaceMappings,
@@ -1591,9 +1613,39 @@ final class JsonHelper
OslcCoreApplicationException,
URISyntaxException
{
- boolean isRdfListNode = isRdfListNode(rdfPrefix, beanClass, setMethod, jsonValue);
+ boolean isRdfContainerNode = isRdfListNode(rdfPrefix, beanClass, setMethod, jsonValue);
+ JSONArray container = null;
+
+ if (! isRdfContainerNode && jsonValue instanceof JSONObject) {
+
+ JSONObject parent = (JSONObject)jsonValue;
+
+ try
+ {
+ container = parent.optJSONArray(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_ALT,
+ null);
+
+ if (container == null)
+ {
+ container = parent.optJSONArray(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_BAG,
+ null);
+ }
+
+ if (container == null)
+ {
+ container = parent.optJSONArray(rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_SEQ,
+ null);
+ }
+ }
+ catch (JSONException e)
+ {
+ throw new IllegalArgumentException(e);
+ }
+
+ isRdfContainerNode = container != null;
+ }
- if (!isRdfListNode && jsonValue instanceof JSONObject)
+ if (!isRdfContainerNode && jsonValue instanceof JSONObject)
{
final JSONObject nestedJSONObject = (JSONObject) jsonValue;
@@ -1628,11 +1680,11 @@ final class JsonHelper
return nestedBean;
}
- else if (jsonValue instanceof JSONArray || isRdfListNode)
+ else if (jsonValue instanceof JSONArray || isRdfContainerNode)
{
final Collection<Object> jsonArray;
- if (isRdfListNode)
+ if (isRdfContainerNode && container == null)
{
jsonArray = new ArrayList<Object>();
@@ -1649,9 +1701,19 @@ final class JsonHelper
rdfPrefix + JSON_PROPERTY_DELIMITER + JSON_PROPERTY_SUFFIX_REST);
}
}
+ else if (isRdfContainerNode)
+ {
+ @SuppressWarnings("unchecked")
+ final Collection<Object> array = container;
+
+ jsonArray = array;
+ }
else
{
- jsonArray = (JSONArray) jsonValue;
+ @SuppressWarnings("unchecked")
+ final Collection<Object> array = (JSONArray)jsonValue;
+
+ jsonArray = array;
}
final ArrayList<Object> tempList = new ArrayList<Object>();