From d914372597d1d2b0296d8fb01e1d95989f42c670 Mon Sep 17 00:00:00 2001
From: Andy Seaborne
Date: Sun, 25 Nov 2018 18:19:34 +0000
Subject: [PATCH] JENA-1639: Model.getList and Statement.getList
---
.../java/org/apache/jena/rdf/model/Model.java | 8 +-
.../org/apache/jena/rdf/model/ModelCon.java | 34 +++-
.../org/apache/jena/rdf/model/Statement.java | 6 +
.../apache/jena/rdf/model/impl/ModelCom.java | 12 +-
.../jena/rdf/model/impl/StatementImpl.java | 5 +
.../apache/jena/rdf/model/test/TestList.java | 171 ++++++------------
.../jena/permissions/model/SecuredModel.java | 28 ++-
.../model/impl/SecuredModelImpl.java | 44 +++--
.../model/impl/SecuredStatementImpl.java | 18 +-
9 files changed, 174 insertions(+), 152 deletions(-)
diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/Model.java b/jena-core/src/main/java/org/apache/jena/rdf/model/Model.java
index 924256d4cff..9c972297842 100644
--- a/jena-core/src/main/java/org/apache/jena/rdf/model/Model.java
+++ b/jena-core/src/main/java/org/apache/jena/rdf/model/Model.java
@@ -287,7 +287,6 @@ will be equal to any other bnode with the same AnonId (even if they are in
*/
public RDFList createList();
-
/**
*
Answer a new list containing the resources from the given iterator, in order.
* @param members An iterator, each value of which is expected to be an RDFNode
@@ -297,11 +296,14 @@ will be equal to any other bnode with the same AnonId (even if they are in
/**
- *
Answer a new list containing the nodes from the given array, in order
+ *
Answer a new list containing the nodes from the given eleemnts, in order.
+ * If the list of elements is empty, until the list is made the object or subject in the model, or has an element added,
+ * it will not appear in the model (e.g. when written out).
+ *
* @param members An array of RDF nodes that will be the members of the list
* @return An RDF-encoded list
*/
- public RDFList createList( RDFNode[] members );
+ public RDFList createList( RDFNode... members );
/** Add a statement to this model.
diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/ModelCon.java b/jena-core/src/main/java/org/apache/jena/rdf/model/ModelCon.java
index 0a30710dd8d..949d28fcf5e 100644
--- a/jena-core/src/main/java/org/apache/jena/rdf/model/ModelCon.java
+++ b/jena-core/src/main/java/org/apache/jena/rdf/model/ModelCon.java
@@ -111,16 +111,17 @@ public interface ModelCon {
* It is in effect an unsafe downcast.
*
*
Subsequent operations on the returned Alt may modify this model.
- *
The bag is assumed to already exist in the model. If it does not,
+ *
The Alt is assumed to already exist in the model. If it does not,
* createAlt should be used instead.
* @return an Alt instance
* @param r an untyped Resource instance
*/
Alt getAlt(Resource r) ;
+
/** Return a Seq instance in this model.
*
- *
Subsequent operations on the returned bag may modify this model.
+ *
Subsequent operations on the returned sequence may modify this model.
*
The seq is assumed to already exist in the model. If it does not,
* createSeq should be used instead.
* @return a seq instance
@@ -128,7 +129,7 @@ public interface ModelCon {
*/
Seq getSeq(String uri) ;
-
+
/** Return a Seq instance based on a given resource.
*
*
This method enables an application to treat any resource as a Seq.
@@ -136,13 +137,36 @@ public interface ModelCon {
*
*
Subsequent operations on the returned Seq may modify this model.
*
The Seq is assumed to already exist in the model. If it does not,
- * createAlt should be used instead.
- * @return an Alt instance
+ * createSeq should be used instead.
+ * @return an Seq instance
* @param r an untyped Resource instance
*/
Seq getSeq(Resource r) ;
+/** Return a RDF List instance in this model.
+ *
+ *
Subsequent operations on the returned list may modify this model.
+ *
The list is assumed to already exist in the model. If it does not,
+ * createList should be used instead.
+ * @return a list instance
+ * @param uri the URI of the list
+ */
+ RDFList getList(String uri) ;
+
+/** Return a RDF List based on a given resource.
+ *
+ *
This method enables an application to treat any resource as a list.
+ * It is in effect an unsafe downcast.
+ *
+ *
Subsequent operations on the returned list may modify this model.
+ *
The list is assumed to already exist in the model. If it does not,
+ * createList should be used instead.
+ * @return a list instance
+ * @param r the resource of the list
+ */
+ RDFList getList(Resource r) ;
+
/** Create a new anonymous resource with a given type.
*
*
Subsequent operations on the returned resource may modify this model.
diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/Statement.java b/jena-core/src/main/java/org/apache/jena/rdf/model/Statement.java
index 376659d759b..5568df0c46a 100644
--- a/jena-core/src/main/java/org/apache/jena/rdf/model/Statement.java
+++ b/jena-core/src/main/java/org/apache/jena/rdf/model/Statement.java
@@ -238,6 +238,12 @@ public interface Statement extends FrontsTriple
*/
public Seq getSeq() ;
+ /** Return the object of the statement as an RDF List.
+ *
An exception will be thrown if the object is not an {@link RDFList}.
+ * @return The object of the statement interpreted as a value as RDFList.
+ */
+ public RDFList getList() ;
+
/** Return the language of the object of the statement.
*
*
An exception will be thrown if the object is not a Literal.
diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/ModelCom.java b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/ModelCom.java
index ab90d30675e..09124e28e28 100644
--- a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/ModelCom.java
+++ b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/ModelCom.java
@@ -876,13 +876,13 @@ public Seq createSeq()
/**
Answer a (the) new empty list
+ Until this is made the object or subject in the model, it will not appear in a written form.
@return An RDF-encoded list of no elements (ie nil)
*/
@Override
public RDFList createList()
{ return getResource( RDF.nil.getURI() ).as( RDFList.class ); }
-
/**
*
Answer a new list containing the resources from the given iterator, in order.
* @param members An iterator, each value of which is expected to be an RDFNode.
@@ -914,7 +914,7 @@ public RDFList createList( Iterator extends RDFNode> members )
* @return An RDF-encoded list
*/
@Override
- public RDFList createList( RDFNode[] members ) {
+ public RDFList createList( RDFNode... members ) {
return createList( Arrays.asList( members ).iterator() );
}
@@ -953,6 +953,14 @@ public Bag getBag( String uri )
public Bag getBag( Resource r )
{ return r.inModel( this ).as( Bag.class ); }
+ @Override
+ public RDFList getList( String uri )
+ { return (RDFList)IteratorFactory.asResource( makeURI( uri ), RDFList.class, this ); }
+
+ @Override
+ public RDFList getList( Resource r )
+ { return r.inModel( this ).as( RDFList.class ); }
+
static private Node makeURI(String uri)
{ return uri == null ? NodeFactory.createBlankNode() : NodeFactory.createURI( uri ); }
diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/StatementImpl.java b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/StatementImpl.java
index e4a8f44b147..aa9b45ce4c3 100644
--- a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/StatementImpl.java
+++ b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/StatementImpl.java
@@ -129,6 +129,11 @@ public Seq getSeq() {
return object.as( Seq.class );
}
+ @Override
+ public RDFList getList() {
+ return object.as( RDFList.class );
+ }
+
/** it turns out to be handy to return the new StatementImpl as the result */
@Override protected StatementImpl replace( RDFNode n ) {
StatementImpl s = new StatementImpl( subject, predicate, n, model );
diff --git a/jena-core/src/test/java/org/apache/jena/rdf/model/test/TestList.java b/jena-core/src/test/java/org/apache/jena/rdf/model/test/TestList.java
index 826c3aa709e..87efc457e39 100644
--- a/jena-core/src/test/java/org/apache/jena/rdf/model/test/TestList.java
+++ b/jena-core/src/test/java/org/apache/jena/rdf/model/test/TestList.java
@@ -50,8 +50,7 @@ public class TestList extends AbstractModelTestBase
public static final String NS = "uri:urn:x-rdf:test#";
/** Test that an iterator delivers the expected values */
- protected static void iteratorTest( final Iterator> i,
- final Object[] expected )
+ protected static void iteratorTest( final Iterator> i, final Object[] expected )
{
final Logger logger = LoggerFactory.getLogger(TestList.class);
final List