org.openrdf.repository.contextaware
Class ContextAwareConnection

java.lang.Object
  extended by org.openrdf.repository.base.RepositoryConnectionBase
      extended by org.openrdf.repository.base.RepositoryConnectionWrapper
          extended by org.openrdf.repository.contextaware.ContextAwareConnection
All Implemented Interfaces:
DelegatingRepositoryConnection, RepositoryConnection

public class ContextAwareConnection
extends RepositoryConnectionWrapper

Allows contexts to be specified at the connection level or the method level.

Author:
James Leigh

Field Summary
 
Fields inherited from class org.openrdf.repository.base.RepositoryConnectionBase
logger
 
Constructor Summary
ContextAwareConnection(Repository repository)
           
ContextAwareConnection(Repository repository, RepositoryConnection connection)
           
 
Method Summary
 void add(File file, String baseURI, RDFFormat dataFormat)
          Adds RDF data from the specified file to a specific contexts in the repository.
 void add(InputStream in, String baseURI, RDFFormat dataFormat)
          Adds RDF data from an InputStream to the repository, optionally to one or more named contexts.
 void add(Iterable<? extends Statement> statements)
          Adds the supplied statements to this repository, optionally to one or more named contexts.
 void add(Iteration<? extends Statement,RepositoryException> statementIter)
          Adds the supplied statements to this repository, optionally to one or more named contexts.
 void add(Reader reader, String baseURI, RDFFormat dataFormat)
          Adds RDF data from a Reader to the repository, optionally to one or more named contexts.
 void add(Resource subject, URI predicate, Value object)
          Adds a statement with the specified subject, predicate and object to this repository, optionally to one or more named contexts.
 void add(Statement st)
          Adds the supplied statement to this repository, optionally to one or more named contexts.
 void add(URL url, String baseURI, RDFFormat dataFormat)
          Adds the RDF data that can be found at the specified URL to the repository, optionally to one or more named contexts.
 void clear()
          Removes all statements from a specific contexts in the repository.
 void export(RDFHandler handler)
          Exports all explicit statements in the specified contexts to the supplied RDFHandler.
 void exportStatements(Resource subj, URI pred, Value obj, RDFHandler hander)
          Exports all statements with a specific subject, predicate and/or object from the repository, optionally from the specified contexts.
 Resource[] getAddContexts()
          The contexts to add the statements to.
 Resource[] getArchiveContexts()
          Before Statements are removed, they are first copied to these contexts.
 QueryLanguage getQueryLanguage()
           
 Resource[] getReadContexts()
          The context(s) to get the data from.
 Resource[] getRemoveContexts()
          The context(s) to remove the data from.
 RepositoryResult<Statement> getStatements(Resource subj, URI pred, Value obj)
          Gets all statements with a specific subject, predicate and/or object from the repository.
 boolean hasStatement(Resource subj, URI pred, Value obj)
          Checks whether the repository contains statements with a specific subject, predicate and/or object, optionally in the specified contexts.
 boolean hasStatement(Statement st)
          Checks whether the repository contains the specified statement, optionally in the specified contexts.
protected  boolean isDelegatingRemove()
           
 boolean isIncludeInferred()
          if false, no inferred statements are considered; if true, inferred statements are considered if available
 BooleanQuery prepareBooleanQuery(QueryLanguage ql, String query)
           
 BooleanQuery prepareBooleanQuery(QueryLanguage ql, String query, String baseURI)
           
 GraphQuery prepareGraphQuery(QueryLanguage ql, String query)
           
 GraphQuery prepareGraphQuery(QueryLanguage ql, String query, String baseURI)
           
 GraphQuery prepareGraphQuery(String query)
           
 GraphQuery prepareGraphQueryWithinContext(QueryLanguage qlang, String query, String baseURI)
           
 GraphQuery prepareGraphQueryWithinContext(String query)
           
 Query prepareQuery(QueryLanguage ql, String query)
           
 Query prepareQuery(QueryLanguage ql, String query, String baseURI)
           
 Query prepareQuery(String query)
           
 Query prepareQueryWithinContext(QueryLanguage qlang, String query, String baseURI)
           
 Query prepareQueryWithinContext(String query)
           
 TupleQuery prepareTupleQuery(QueryLanguage ql, String query)
           
 TupleQuery prepareTupleQuery(QueryLanguage ql, String query, String baseURI)
           
 TupleQuery prepareTupleQuery(String query)
           
 TupleQuery prepareTupleQueryWithinContext(QueryLanguage qlang, String query, String baseURI)
           
 TupleQuery prepareTupleQueryWithinContext(String query)
           
 void remove(Iterable<? extends Statement> statements)
          Removes the supplied statements from the specified contexts in this repository.
 void remove(Iteration<? extends Statement,RepositoryException> statementIter)
          Removes the supplied statements from a specific context in this repository, ignoring any context information carried by the statements themselves.
 void remove(Resource subject, URI predicate, Value object)
          Removes the statement with the specified subject, predicate and object from the repository, optionally restricted to the specified contexts.
 void remove(Statement st)
          Removes the supplied statement from the specified contexts in the repository.
protected  void removeWithoutCommit(Resource subject, URI predicate, Value object, Resource... contexts)
           
 void setAddContexts(Resource... addContexts)
          The contexts to add the statements to.
 void setArchiveContexts(Resource... archiveContexts)
          Before Statements are removed, they are first copied to these contexts.
 void setIncludeInferred(boolean includeInferred)
          if false, no inferred statements are considered; if true, inferred statements are considered if available
 void setQueryLanguage(QueryLanguage ql)
           
 void setReadContexts(Resource... readContexts)
          The context(s) to get the data from.
 void setRemoveContexts(Resource... removeContexts)
          The context(s) to remove the data from.
 long size()
          Returns the number of (explicit) statements that are in the specified contexts in this repository.
 
Methods inherited from class org.openrdf.repository.base.RepositoryConnectionWrapper
add, add, add, add, add, add, add, add, addWithoutCommit, clear, clearNamespaces, close, commit, exportStatements, exportStatements, getContextIDs, getDelegate, getNamespace, getNamespaces, getStatements, hasStatement, hasStatement, isAutoCommit, isDelegatingAdd, isDelegatingRead, isEmpty, isOpen, remove, remove, remove, remove, removeNamespace, rollback, setAutoCommit, setDelegate, setNamespace, size
 
Methods inherited from class org.openrdf.repository.base.RepositoryConnectionBase
addInputStreamOrReader, addWithoutCommit, autoCommit, export, finalize, getRepository, removeWithoutCommit
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.openrdf.repository.RepositoryConnection
export, getRepository
 

Constructor Detail

ContextAwareConnection

public ContextAwareConnection(Repository repository)
                       throws RepositoryException
Throws:
RepositoryException

ContextAwareConnection

public ContextAwareConnection(Repository repository,
                              RepositoryConnection connection)
Method Detail

isDelegatingRemove

protected boolean isDelegatingRemove()
Overrides:
isDelegatingRemove in class RepositoryConnectionWrapper

isIncludeInferred

public boolean isIncludeInferred()
if false, no inferred statements are considered; if true, inferred statements are considered if available


setIncludeInferred

public void setIncludeInferred(boolean includeInferred)
if false, no inferred statements are considered; if true, inferred statements are considered if available


getQueryLanguage

public QueryLanguage getQueryLanguage()

setQueryLanguage

public void setQueryLanguage(QueryLanguage ql)

getReadContexts

public Resource[] getReadContexts()
The context(s) to get the data from. Note that this parameter is a vararg and as such is optional. If no contexts are supplied the method operates on the entire repository.


setReadContexts

public void setReadContexts(Resource... readContexts)
The context(s) to get the data from. Note that this parameter is a vararg and as such is optional. If no contexts are supplied the method operates on the entire repository.


getAddContexts

public Resource[] getAddContexts()
The contexts to add the statements to. Note that this parameter is a vararg and as such is optional. If no contexts are specified, each statement is added to any context specified in the statement, or if the statement contains no context, it is added without context. If one or more contexts are specified each statement is added to these contexts, ignoring any context information in the statement itself. ignored.


setAddContexts

public void setAddContexts(Resource... addContexts)
The contexts to add the statements to. Note that this parameter is a vararg and as such is optional. If no contexts are specified, each statement is added to any context specified in the statement, or if the statement contains no context, it is added without context. If one or more contexts are specified each statement is added to these contexts, ignoring any context information in the statement itself. ignored.


getRemoveContexts

public Resource[] getRemoveContexts()
The context(s) to remove the data from. Note that this parameter is a vararg and as such is optional. If no contexts are supplied the method operates on the contexts associated with the statement itself, and if no context is associated with the statement, on the entire repository.


setRemoveContexts

public void setRemoveContexts(Resource... removeContexts)
The context(s) to remove the data from. Note that this parameter is a vararg and as such is optional. If no contexts are supplied the method operates on the contexts associated with the statement itself, and if no context is associated with the statement, on the entire repository.


getArchiveContexts

public Resource[] getArchiveContexts()
Before Statements are removed, they are first copied to these contexts.


setArchiveContexts

public void setArchiveContexts(Resource... archiveContexts)
Before Statements are removed, they are first copied to these contexts.


add

public void add(File file,
                String baseURI,
                RDFFormat dataFormat)
         throws IOException,
                RDFParseException,
                RepositoryException
Adds RDF data from the specified file to a specific contexts in the repository.

Parameters:
file - A file containing RDF data.
baseURI - The base URI to resolve any relative URIs that are in the data against. This defaults to the value of file.toURI() if the value is set to null.
dataFormat - The serialization format of the data.
Throws:
IOException - If an I/O error occurred while reading from the file.
UnsupportedRDFormatException - If no parser is available for the specified RDF format.
RDFParseException - If an error was found while parsing the RDF data.
RepositoryException - If the data could not be added to the repository, for example because the repository is not writable.
See Also:
getAddContexts()

add

public void add(InputStream in,
                String baseURI,
                RDFFormat dataFormat)
         throws IOException,
                RDFParseException,
                RepositoryException
Adds RDF data from an InputStream to the repository, optionally to one or more named contexts.

Parameters:
in - An InputStream from which RDF data can be read.
baseURI - The base URI to resolve any relative URIs that are in the data against.
dataFormat - The serialization format of the data.
Throws:
IOException - If an I/O error occurred while reading from the input stream.
UnsupportedRDFormatException - If no parser is available for the specified RDF format.
RDFParseException - If an error was found while parsing the RDF data.
RepositoryException - If the data could not be added to the repository, for example because the repository is not writable.
See Also:
getAddContexts()

add

public void add(Iterable<? extends Statement> statements)
         throws RepositoryException
Adds the supplied statements to this repository, optionally to one or more named contexts.

Parameters:
statements - The statements that should be added.
Throws:
RepositoryException - If the statements could not be added to the repository, for example because the repository is not writable.
See Also:
getAddContexts()

add

public void add(Iteration<? extends Statement,RepositoryException> statementIter)
         throws RepositoryException
Adds the supplied statements to this repository, optionally to one or more named contexts.

Parameters:
statementIter - The statements to add. In case the iterator is a CloseableIteration, it will be closed before this method returns.
Throws:
RepositoryException - If the statements could not be added to the repository, for example because the repository is not writable.
See Also:
getAddContexts()

add

public void add(Reader reader,
                String baseURI,
                RDFFormat dataFormat)
         throws IOException,
                RDFParseException,
                RepositoryException
Adds RDF data from a Reader to the repository, optionally to one or more named contexts. Note: using a Reader to upload byte-based data means that you have to be careful not to destroy the data's character encoding by enforcing a default character encoding upon the bytes. If possible, adding such data using an InputStream is to be preferred.

Parameters:
reader - A Reader from which RDF data can be read.
baseURI - The base URI to resolve any relative URIs that are in the data against.
dataFormat - The serialization format of the data.
Throws:
IOException - If an I/O error occurred while reading from the reader.
UnsupportedRDFormatException - If no parser is available for the specified RDF format.
RDFParseException - If an error was found while parsing the RDF data.
RepositoryException - If the data could not be added to the repository, for example because the repository is not writable.
See Also:
getAddContexts()

add

public void add(Resource subject,
                URI predicate,
                Value object)
         throws RepositoryException
Adds a statement with the specified subject, predicate and object to this repository, optionally to one or more named contexts.

Parameters:
subject - The statement's subject.
predicate - The statement's predicate.
object - The statement's object.
Throws:
RepositoryException - If the data could not be added to the repository, for example because the repository is not writable.
See Also:
getAddContexts()

add

public void add(Statement st)
         throws RepositoryException
Adds the supplied statement to this repository, optionally to one or more named contexts.

Parameters:
st - The statement to add.
Throws:
RepositoryException - If the statement could not be added to the repository, for example because the repository is not writable.
See Also:
getAddContexts()

add

public void add(URL url,
                String baseURI,
                RDFFormat dataFormat)
         throws IOException,
                RDFParseException,
                RepositoryException
Adds the RDF data that can be found at the specified URL to the repository, optionally to one or more named contexts.

Parameters:
url - The URL of the RDF data.
baseURI - The base URI to resolve any relative URIs that are in the data against. This defaults to the value of url.toExternalForm() if the value is set to null.
dataFormat - The serialization format of the data.
Throws:
IOException - If an I/O error occurred while reading from the URL.
UnsupportedRDFormatException - If no parser is available for the specified RDF format.
RDFParseException - If an error was found while parsing the RDF data.
RepositoryException - If the data could not be added to the repository, for example because the repository is not writable.
See Also:
getAddContexts()

clear

public void clear()
           throws RepositoryException
Removes all statements from a specific contexts in the repository.

Throws:
RepositoryException - If the statements could not be removed from the repository, for example because the repository is not writable.
See Also:
getRemoveContexts()

export

public void export(RDFHandler handler)
            throws RepositoryException,
                   RDFHandlerException
Exports all explicit statements in the specified contexts to the supplied RDFHandler.

Parameters:
handler - The handler that will handle the RDF data.
Throws:
RDFHandlerException - If the handler encounters an unrecoverable error.
RepositoryException
See Also:
getReadContexts()

exportStatements

public void exportStatements(Resource subj,
                             URI pred,
                             Value obj,
                             RDFHandler hander)
                      throws RepositoryException,
                             RDFHandlerException
Exports all statements with a specific subject, predicate and/or object from the repository, optionally from the specified contexts.

Parameters:
subj - The subject, or null if the subject doesn't matter.
pred - The predicate, or null if the predicate doesn't matter.
obj - The object, or null if the object doesn't matter.
handler - The handler that will handle the RDF data.
Throws:
RDFHandlerException - If the handler encounters an unrecoverable error.
RepositoryException
See Also:
getReadContexts(), isIncludeInferred()

getStatements

public RepositoryResult<Statement> getStatements(Resource subj,
                                                 URI pred,
                                                 Value obj)
                                          throws RepositoryException
Gets all statements with a specific subject, predicate and/or object from the repository. The result is optionally restricted to the specified set of named contexts.

Parameters:
subj - A Resource specifying the subject, or null for a wildcard.
pred - A URI specifying the predicate, or null for a wildcard.
obj - A Value specifying the object, or null for a wildcard.
Returns:
The statements matching the specified pattern. The result object is a RepositoryResult object, a lazy Iterator-like object containing Statements and optionally throwing a RepositoryException when an error when a problem occurs during retrieval.
Throws:
RepositoryException
See Also:
getReadContexts(), isIncludeInferred()

hasStatement

public boolean hasStatement(Resource subj,
                            URI pred,
                            Value obj)
                     throws RepositoryException
Checks whether the repository contains statements with a specific subject, predicate and/or object, optionally in the specified contexts.

Parameters:
subj - A Resource specifying the subject, or null for a wildcard.
pred - A URI specifying the predicate, or null for a wildcard.
obj - A Value specifying the object, or null for a wildcard.
Returns:
true If a matching statement is in the repository in the specified context, false otherwise.
Throws:
RepositoryException
See Also:
getReadContexts(), isIncludeInferred()

hasStatement

public boolean hasStatement(Statement st)
                     throws RepositoryException
Checks whether the repository contains the specified statement, optionally in the specified contexts.

Parameters:
st - The statement to look for. Context information in the statement is ignored.
Returns:
true If the repository contains the specified statement, false otherwise.
Throws:
RepositoryException
See Also:
getReadContexts(), isIncludeInferred()

prepareGraphQueryWithinContext

public GraphQuery prepareGraphQueryWithinContext(String query)
                                          throws MalformedQueryException,
                                                 RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareQueryWithinContext

public Query prepareQueryWithinContext(String query)
                                throws MalformedQueryException,
                                       RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareTupleQueryWithinContext

public TupleQuery prepareTupleQueryWithinContext(String query)
                                          throws MalformedQueryException,
                                                 RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareGraphQueryWithinContext

public GraphQuery prepareGraphQueryWithinContext(QueryLanguage qlang,
                                                 String query,
                                                 String baseURI)
                                          throws MalformedQueryException,
                                                 RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareQueryWithinContext

public Query prepareQueryWithinContext(QueryLanguage qlang,
                                       String query,
                                       String baseURI)
                                throws MalformedQueryException,
                                       RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareTupleQueryWithinContext

public TupleQuery prepareTupleQueryWithinContext(QueryLanguage qlang,
                                                 String query,
                                                 String baseURI)
                                          throws MalformedQueryException,
                                                 RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareGraphQuery

public GraphQuery prepareGraphQuery(String query)
                             throws MalformedQueryException,
                                    RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareQuery

public Query prepareQuery(String query)
                   throws MalformedQueryException,
                          RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareTupleQuery

public TupleQuery prepareTupleQuery(String query)
                             throws MalformedQueryException,
                                    RepositoryException
Throws:
MalformedQueryException
RepositoryException

prepareGraphQuery

public GraphQuery prepareGraphQuery(QueryLanguage ql,
                                    String query)
                             throws MalformedQueryException,
                                    RepositoryException
Specified by:
prepareGraphQuery in interface RepositoryConnection
Overrides:
prepareGraphQuery in class RepositoryConnectionBase
Throws:
MalformedQueryException
RepositoryException

prepareQuery

public Query prepareQuery(QueryLanguage ql,
                          String query)
                   throws MalformedQueryException,
                          RepositoryException
Specified by:
prepareQuery in interface RepositoryConnection
Overrides:
prepareQuery in class RepositoryConnectionBase
Throws:
MalformedQueryException
RepositoryException

prepareTupleQuery

public TupleQuery prepareTupleQuery(QueryLanguage ql,
                                    String query)
                             throws MalformedQueryException,
                                    RepositoryException
Specified by:
prepareTupleQuery in interface RepositoryConnection
Overrides:
prepareTupleQuery in class RepositoryConnectionBase
Throws:
MalformedQueryException
RepositoryException

prepareBooleanQuery

public BooleanQuery prepareBooleanQuery(QueryLanguage ql,
                                        String query)
                                 throws MalformedQueryException,
                                        RepositoryException
Specified by:
prepareBooleanQuery in interface RepositoryConnection
Overrides:
prepareBooleanQuery in class RepositoryConnectionBase
Throws:
MalformedQueryException
RepositoryException

prepareGraphQuery

public GraphQuery prepareGraphQuery(QueryLanguage ql,
                                    String query,
                                    String baseURI)
                             throws MalformedQueryException,