org.vivoweb.harvester.util.repo
Class JenaConnect

java.lang.Object
  extended by org.vivoweb.harvester.util.repo.JenaConnect
Direct Known Subclasses:
DBJenaConnect, TDBJenaConnect

public abstract class JenaConnect
extends java.lang.Object

Connection Helper for Jena Models

Author:
Christopher Haines (hainesc@ctrip.ufl.edu)

Field Summary
protected static java.util.HashMap<java.lang.String,com.hp.hpl.jena.sparql.resultset.ResultSetFormat> formatSymbols
          RDF formats
 
Constructor Summary
JenaConnect()
           
 
Method Summary
abstract  void close()
          Closes the model and the jdbc connection
 boolean containsURI(java.lang.String uri)
          Checks if the model contains the given uri
 boolean executeAskQuery(java.lang.String queryString)
          Executes a sparql describe query against the JENA model and returns the description result model
 JenaConnect executeConstructQuery(java.lang.String queryString)
          Executes a sparql construct query against the JENA model and returns the constructed result model
 JenaConnect executeDescribeQuery(java.lang.String queryString)
          Executes a sparql describe query against the JENA model and returns the description result model
 void executeQuery(java.lang.String queryParam, java.lang.String resultFormatParam, boolean datasetMode)
          Execute a Query and output result to System.out
 void executeQuery(java.lang.String queryParam, java.lang.String resultFormatParam, java.io.OutputStream output, boolean datasetMode)
          Execute a Query
 com.hp.hpl.jena.query.ResultSet executeSelectQuery(java.lang.String queryString)
          Executes a sparql select query against the JENA model and returns the selected result set
 com.hp.hpl.jena.query.ResultSet executeSelectQuery(java.lang.String queryString, boolean copyResultSet)
          Executes a sparql select query against the JENA model and returns the selected result set
 void executeUpdateQuery(java.lang.String queryString)
          Executes a sparql update query against the JENA model
 void exportRdfToFile(java.lang.String fileName)
          Export the RDF to a file
 void exportRdfToStream(java.io.OutputStream out)
          Export all RDF
 java.lang.String exportRdfToString()
          Export all RDF
abstract  com.hp.hpl.jena.query.Dataset getDataSet()
          Get the dataset for this connection Can be very expensive when using RDB connections (SDB, TDB, and Mem are fine)
 com.hp.hpl.jena.rdf.model.Model getJenaModel()
          Accessor for Jena Model
 java.lang.String getModelName()
          Get the modelName
 boolean isEmpty()
          Is this model empty
 void loadRdfFromFile(java.lang.String fileName, java.lang.String namespace, java.lang.String language)
          Load the RDF from a file
 void loadRdfFromJC(JenaConnect jc)
          Load in RDF from a model
 int loadRdfFromRH(RecordHandler rh, java.lang.String namespace)
          Adds all records in a RecordHandler to the model
 void loadRdfFromStream(java.io.InputStream in, java.lang.String namespace, java.lang.String language)
          Load in RDF
 void loadRdfFromString(java.lang.String rdf, java.lang.String namespace, java.lang.String language)
          Load in RDF
static void main(java.lang.String... args)
          Main method
abstract  JenaConnect neighborConnectClone(java.lang.String newModelName)
          Factory (connects to the same jena triple store as another jena connect, but uses a different named model)
static JenaConnect parseConfig(java.io.File configFile)
          Config File Based Factory
static JenaConnect parseConfig(java.io.File configFile, java.util.Map<java.lang.String,java.lang.String> overrideParams)
          Config File Based Factory
static JenaConnect parseConfig(org.apache.commons.vfs.FileObject configFile)
          Config File Based Factory
static JenaConnect parseConfig(org.apache.commons.vfs.FileObject configFile, java.util.Map<java.lang.String,java.lang.String> overrideParams)
          Config File Based Factory that overrides parameters
static JenaConnect parseConfig(java.io.InputStream configStream, java.util.Map<java.lang.String,java.lang.String> overrideParams)
          Config Stream Based Factory that overrides parameters
static JenaConnect parseConfig(java.lang.String configFileName)
          Config File Based Factory
static JenaConnect parseConfig(java.lang.String configFileName, java.util.Map<java.lang.String,java.lang.String> overrideParams)
          Config File Based Factory
 void printParameters()
          Output the jena model information
 void removeRdfFromFile(java.lang.String fileName, java.lang.String namespace, java.lang.String language)
          Remove the RDF from a file
 void removeRdfFromJC(JenaConnect inputJC)
          Remove RDF from another JenaConnect
 int removeRdfFromRH(RecordHandler rh, java.lang.String namespace)
          Removes all records in a RecordHandler from the model
 void removeRdfFromStream(java.io.InputStream in, java.lang.String namespace, java.lang.String language)
          Remove RDF from an input stream
protected  void setJenaModel(com.hp.hpl.jena.rdf.model.Model jena)
          Setter
protected  void setModelName(java.lang.String modelName)
          Set the modelName
 int size()
          Get the size of a jena model
abstract  void truncate()
          Remove all statements from model
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

formatSymbols

protected static java.util.HashMap<java.lang.String,com.hp.hpl.jena.sparql.resultset.ResultSetFormat> formatSymbols
RDF formats

Constructor Detail

JenaConnect

public JenaConnect()
Method Detail

neighborConnectClone

public abstract JenaConnect neighborConnectClone(java.lang.String newModelName)
                                          throws java.io.IOException
Factory (connects to the same jena triple store as another jena connect, but uses a different named model)

Parameters:
newModelName - the model name to use
Returns:
the new jenaconnect
Throws:
java.io.IOException - unable to secure db connection

parseConfig

public static JenaConnect parseConfig(java.io.InputStream configStream,
                                      java.util.Map<java.lang.String,java.lang.String> overrideParams)
                               throws java.io.IOException
Config Stream Based Factory that overrides parameters

Parameters:
configStream - the config input stream
overrideParams - the parameters to override the file with
Returns:
JenaConnect instance
Throws:
java.io.IOException - error connecting

parseConfig

public static JenaConnect parseConfig(org.apache.commons.vfs.FileObject configFile)
                               throws java.io.IOException
Config File Based Factory

Parameters:
configFile - the vfs config file descriptor
Returns:
JenaConnect instance
Throws:
java.io.IOException - error connecting

parseConfig

public static JenaConnect parseConfig(org.apache.commons.vfs.FileObject configFile,
                                      java.util.Map<java.lang.String,java.lang.String> overrideParams)
                               throws java.io.IOException
Config File Based Factory that overrides parameters

Parameters:
configFile - the vfs config file descriptor
overrideParams - the parameters to override the file with
Returns:
JenaConnect instance
Throws:
java.io.IOException - error connecting

parseConfig

public static JenaConnect parseConfig(java.io.File configFile)
                               throws java.io.IOException
Config File Based Factory

Parameters:
configFile - the config file descriptor
Returns:
JenaConnect instance
Throws:
java.io.IOException - error connecting

parseConfig

public static JenaConnect parseConfig(java.io.File configFile,
                                      java.util.Map<java.lang.String,java.lang.String> overrideParams)
                               throws java.io.IOException
Config File Based Factory

Parameters:
configFile - the config file descriptor
overrideParams - the parameters to override the file with
Returns:
JenaConnect instance
Throws:
java.io.IOException - error connecting

parseConfig

public static JenaConnect parseConfig(java.lang.String configFileName)
                               throws java.io.IOException
Config File Based Factory

Parameters:
configFileName - the config file path
Returns:
JenaConnect instance
Throws:
java.io.IOException - xml parse error

parseConfig

public static JenaConnect parseConfig(java.lang.String configFileName,
                                      java.util.Map<java.lang.String,java.lang.String> overrideParams)
                               throws java.io.IOException
Config File Based Factory

Parameters:
configFileName - the config file path
overrideParams - the parameters to override the file with
Returns:
JenaConnect instance
Throws:
java.io.IOException - xml parse error

size

public int size()
Get the size of a jena model

Returns:
the number of statement in this model

getDataSet

public abstract com.hp.hpl.jena.query.Dataset getDataSet()
                                                  throws java.io.IOException
Get the dataset for this connection Can be very expensive when using RDB connections (SDB, TDB, and Mem are fine)

Returns:
the database connection's dataset
Throws:
java.io.IOException - error connecting

loadRdfFromStream

public void loadRdfFromStream(java.io.InputStream in,
                              java.lang.String namespace,
                              java.lang.String language)
Load in RDF

Parameters:
in - input stream to read rdf from
namespace - the base uri to use for imported uris
language - the language the rdf is in. Predefined values for lang are "RDF/XML", "N-TRIPLE", "TURTLE" (or "TTL") and "N3". null represents the default language, "RDF/XML". "RDF/XML-ABBREV" is a synonym for "RDF/XML"

loadRdfFromFile

public void loadRdfFromFile(java.lang.String fileName,
                            java.lang.String namespace,
                            java.lang.String language)
                     throws org.apache.commons.vfs.FileSystemException
Load the RDF from a file

Parameters:
fileName - the file to read from
namespace - the base uri to use for imported uris
language - the language the rdf is in. Predefined values for lang are "RDF/XML", "N-TRIPLE", "TURTLE" (or "TTL") and "N3". null represents the default language, "RDF/XML". "RDF/XML-ABBREV" is a synonym for "RDF/XML"
Throws:
org.apache.commons.vfs.FileSystemException - error accessing file

loadRdfFromString

public void loadRdfFromString(java.lang.String rdf,
                              java.lang.String namespace,
                              java.lang.String language)
Load in RDF

Parameters:
rdf - rdf string
namespace - the base uri to use for imported uris
language - the language the rdf is in. Predefined values for lang are "RDF/XML", "N-TRIPLE", "TURTLE" (or "TTL") and "N3". null represents the default language, "RDF/XML". "RDF/XML-ABBREV" is a synonym for "RDF/XML"

loadRdfFromJC

public void loadRdfFromJC(JenaConnect jc)
Load in RDF from a model

Parameters:
jc - the model to load in

exportRdfToStream

public void exportRdfToStream(java.io.OutputStream out)
                       throws java.io.IOException
Export all RDF

Parameters:
out - output stream to write rdf to
Throws:
java.io.IOException - error writing to stream

exportRdfToString

public java.lang.String exportRdfToString()
                                   throws java.io.IOException
Export all RDF

Returns:
the rdf
Throws:
java.io.IOException - error writing to string

exportRdfToFile

public void exportRdfToFile(java.lang.String fileName)
                     throws java.io.IOException
Export the RDF to a file

Parameters:
fileName - the file to write to
Throws:
java.io.IOException - error writing to file

removeRdfFromJC

public void removeRdfFromJC(JenaConnect inputJC)
Remove RDF from another JenaConnect

Parameters:
inputJC - the Model to read from

removeRdfFromStream

public void removeRdfFromStream(java.io.InputStream in,
                                java.lang.String namespace,
                                java.lang.String language)
                         throws java.io.IOException
Remove RDF from an input stream

Parameters:
in - input stream to read rdf from
namespace - the base uri to use for imported uris
language - the language the rdf is in. Predefined values for lang are "RDF/XML", "N-TRIPLE", "TURTLE" (or "TTL") and "N3". null represents the default language, "RDF/XML". "RDF/XML-ABBREV" is a synonym for "RDF/XML"
Throws:
java.io.IOException - error connecting

removeRdfFromFile

public void removeRdfFromFile(java.lang.String fileName,
                              java.lang.String namespace,
                              java.lang.String language)
                       throws java.io.IOException
Remove the RDF from a file

Parameters:
fileName - the file to read from
namespace - the base uri to use for imported uris
language - the language the rdf is in. Predefined values for lang are "RDF/XML", "N-TRIPLE", "TURTLE" (or "TTL") and "N3". null represents the default language, "RDF/XML". "RDF/XML-ABBREV" is a synonym for "RDF/XML"
Throws:
java.io.IOException - error connecting

removeRdfFromRH

public int removeRdfFromRH(RecordHandler rh,
                           java.lang.String namespace)
                    throws java.io.IOException
Removes all records in a RecordHandler from the model

Parameters:
rh - the RecordHandler to pull records from
namespace - the base uri to use for imported uris
Returns:
number of records removed
Throws:
java.io.IOException - error connecting

loadRdfFromRH

public int loadRdfFromRH(RecordHandler rh,
                         java.lang.String namespace)
Adds all records in a RecordHandler to the model

Parameters:
rh - the RecordHandler to pull records from
namespace - the base uri to use for imported uris
Returns:
number of records added

close

public abstract void close()
Closes the model and the jdbc connection


executeSelectQuery

public com.hp.hpl.jena.query.ResultSet executeSelectQuery(java.lang.String queryString)
Executes a sparql select query against the JENA model and returns the selected result set

Parameters:
queryString - the query to execute against the model
Returns:
the executed query result set

executeSelectQuery

public com.hp.hpl.jena.query.ResultSet executeSelectQuery(java.lang.String queryString,
                                                          boolean copyResultSet)
Executes a sparql select query against the JENA model and returns the selected result set

Parameters:
queryString - the query to execute against the model
copyResultSet - copy the resultset
Returns:
the executed query result set

executeConstructQuery

public JenaConnect executeConstructQuery(java.lang.String queryString)
                                  throws java.io.IOException
Executes a sparql construct query against the JENA model and returns the constructed result model

Parameters:
queryString - the query to execute against the model
Returns:
the executed query result model
Throws:
java.io.IOException - error connecting

executeDescribeQuery

public JenaConnect executeDescribeQuery(java.lang.String queryString)
                                 throws java.io.IOException
Executes a sparql describe query against the JENA model and returns the description result model

Parameters:
queryString - the query to execute against the model
Returns:
the executed query result model
Throws:
java.io.IOException - error connecting

executeAskQuery

public boolean executeAskQuery(java.lang.String queryString)
Executes a sparql describe query against the JENA model and returns the description result model

Parameters:
queryString - the query to execute against the model
Returns:
the executed query result model

executeUpdateQuery

public void executeUpdateQuery(java.lang.String queryString)
Executes a sparql update query against the JENA model

Parameters:
queryString - the query to execute against the model

executeQuery

public void executeQuery(java.lang.String queryParam,
                         java.lang.String resultFormatParam,
                         boolean datasetMode)
                  throws java.io.IOException
Execute a Query and output result to System.out

Parameters:
queryParam - the query
resultFormatParam - the format to return the results in ('RS_RDF',etc for select queries / 'RDF/XML',etc for construct/describe queries)
datasetMode - run against dataset rather than model
Throws:
java.io.IOException - error writing to output

executeQuery

public void executeQuery(java.lang.String queryParam,
                         java.lang.String resultFormatParam,
                         java.io.OutputStream output,
                         boolean datasetMode)
                  throws java.io.IOException
Execute a Query

Parameters:
queryParam - the query
resultFormatParam - the format to return the results in ('RS_TEXT' default for select queries / 'RDF/XML' default for construct/describe queries)
output - output stream to write to - null uses System.out
datasetMode - run against dataset rather than model
Throws:
java.io.IOException - error writing to output

getJenaModel

public com.hp.hpl.jena.rdf.model.Model getJenaModel()
Accessor for Jena Model

Returns:
the Jena Model

setJenaModel

protected void setJenaModel(com.hp.hpl.jena.rdf.model.Model jena)
Setter

Parameters:
jena - the new model

containsURI

public boolean containsURI(java.lang.String uri)
Checks if the model contains the given uri

Parameters:
uri - the uri to check for
Returns:
true if found, false otherwise

truncate

public abstract void truncate()
Remove all statements from model


setModelName

protected void setModelName(java.lang.String modelName)
Set the modelName

Parameters:
modelName - the model name

getModelName

public java.lang.String getModelName()
Get the modelName

Returns:
the modelName

isEmpty

public boolean isEmpty()
Is this model empty

Returns:
true if empty, false otherwise

printParameters

public void printParameters()
Output the jena model information


main

public static void main(java.lang.String... args)
Main method

Parameters:
args - commandline arguments