de.fuberlin.wiwiss.ng4j.impl
Class GraphReaderService

java.lang.Object
  extended by de.fuberlin.wiwiss.ng4j.impl.GraphReaderService

public class GraphReaderService
extends Object

Reads RDF graphs from external sources (URLs, InputStreams, Readers, Strings). Supports TriX, TriG and all serializations supported by Jena. Supports reading of Named Graphs into NamedGraphSets.

To read from a source, three steps must be performed:

  1. Set a source using setSourceURL(java.lang.String), setSourceReader(java.io.Reader, java.lang.String), setSourceInputStream(java.io.InputStream, java.lang.String), setSourceString(java.lang.String, java.lang.String) or setSourceFile(java.io.File).
  2. Set the serialization language of the source using setLanguage(java.lang.String). This step is optional. If no language is given, the class will try to guess the language based on MIME types and filename extensions of the URL or base URI.
  3. Read the source into a Model (ignoring graph names) or into a NamedGraphSet (using graph names) using readInto(Model) or readInto(NamedGraphSet).

Design note: This class wraps the document reading functions of Jena and the Named Graph reading functions of NG4J into a single interface without adding more methods and logic to Model.

TODO: The baseURI argument to setXXX should be optional for languages that are able provide a base URI within the document (that is, RDF/XML). This is hard because Jena's RDF parser doesn't expose the base URI.

Author:
Richard Cyganiak (richard@cyganiak.de)

Constructor Summary
GraphReaderService()
           
 
Method Summary
 void readInto(Model model)
          Adds all statements from the source to a Jena Model.
 void readInto(NamedGraphSet set)
          Adds one or more Named Graphs from the source to a NamedGraphSet.
 void setLanguage(String lang)
          Sets the language of the source.
 String setReaderClassName(String lang, String className)
          Adds support for additional languages.
 void setSourceFile(File sourceFile)
          Sets a File as the source.
 void setSourceInputStream(InputStream inputStream, String baseURI)
          Sets an InputStream as the source.
 void setSourceReader(Reader reader, String baseURI)
          Sets a Reader as the source.
 void setSourceString(String sourceString, String baseURI)
          Sets a String as the source.
 void setSourceURL(String url)
          Sets an URL as the source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GraphReaderService

public GraphReaderService()
Method Detail

setSourceURL

public void setSourceURL(String url)
Sets an URL as the source.

Parameters:
url - An URL of a RDF document

setSourceReader

public void setSourceReader(Reader reader,
                            String baseURI)
Sets a Reader as the source. A base URI must also be given. It is used to resolve relative URIs in the document, and as a graph name when reading documents with unnamed graphs into a NamedGraphSet.

Parameters:
reader - A Reader of a RDF document
baseURI - The base URI of the document

setSourceInputStream

public void setSourceInputStream(InputStream inputStream,
                                 String baseURI)
Sets an InputStream as the source. A base URI must also be given. It is used to resolve relative URIs in the document, and as a graph name when reading documents with unnamed graphs into a NamedGraphSet.

Parameters:
inputStream - An InputStream of a RDF document

setSourceString

public void setSourceString(String sourceString,
                            String baseURI)
Sets a String as the source. A base URI must also be given. It is used to resolve relative URIs in the document, and as a graph name when reading documents with unnamed graphs into a NamedGraphSet.

Parameters:
sourceString - A String containing an RDF document

setSourceFile

public void setSourceFile(File sourceFile)
Sets a File as the source.

Parameters:
sourceFile - A file containing an RDF document

setLanguage

public void setLanguage(String lang)
Sets the language of the source. Supported languages are: Setting the language is optional for URL sources. If no language is given, the implementation will try to guess the language based on MIME types and filename extensions. This will not work in all cases and is not tested very well, so it's safest to specify the language in any case.

Parameters:
lang - One of the above language IDs

readInto

public void readInto(NamedGraphSet set)
Adds one or more Named Graphs from the source to a NamedGraphSet. If the serialization of the source does not support Named Graphs natively, then it will be treated like a single Named Graph with the source's URL or base URI as a graph name.

Existing graphs in the NamedGraphSet will be replaced by graphs with the same name from the source.

setSourceXXX and setLanguage must have been called before readInto.

Parameters:
set - The NamedGraphSet to which statements will be added

readInto

public void readInto(Model model)
Adds all statements from the source to a Jena Model. Ignores graph names if the serialization language supports them. setSourceXXX and setLanguage must have been called before readInto.

Parameters:
model - The model to which statements will be added

setReaderClassName

public String setReaderClassName(String lang,
                                 String className)
Adds support for additional languages.

Parameters:
lang - A language name
className - A class implementing RDFReader
See Also:
RDFReaderF.setReaderClassName(java.lang.String, java.lang.String)