org.apache.any23.rdf
Class RDFUtils

java.lang.Object
  extended by org.apache.any23.rdf.RDFUtils

public class RDFUtils
extends Object

Basic class providing a set of utility methods when dealing with RDF.

Author:
Davide Palmisano (dpalmisano@gmail.com), Michele Mostarda (mostarda@fbk.eu)

Nested Class Summary
static class RDFUtils.Parser
          List of supported RDF parsers.
 
Method Summary
static org.openrdf.model.BNode bnode()
           
static org.openrdf.model.BNode bnode(String id)
          Creates a BNode.
static String fixAbsoluteURI(String uri)
          Fixes typical errors in an absolute URI, such as unescaped spaces.
static String fixURIWithException(String unescapedURI)
          Tries to fix a potentially broken relative or absolute URI.
static org.openrdf.model.BNode getBNode(String id)
          Creates a BNode.
static RDFUtils.Parser getParserFromExtension(String ext)
          Returns a parser type from the given extension.
static org.openrdf.rio.RDFParser getRDFParser(RDFUtils.Parser p)
          Creates a new RDFParser instance.
static String getXSDDate(String dateToBeParsed, String format)
          This method allows to obtain an XML Schema compliant date providing a textual representation of a date and textual a pattern for parsing it.
static boolean isAbsoluteURI(String href)
          Checks if href is absolute or not.
static org.openrdf.model.Literal literal(boolean b)
          Creates a Literal.
static org.openrdf.model.Literal literal(byte b)
          Creates a Literal.
static org.openrdf.model.Literal literal(double d)
          Creates a Literal.
static org.openrdf.model.Literal literal(float f)
          Creates a Literal.
static org.openrdf.model.Literal literal(int i)
          Creates a Literal.
static org.openrdf.model.Literal literal(long l)
          Creates a Literal.
static org.openrdf.model.Literal literal(short s)
          Creates a Literal.
static org.openrdf.model.Literal literal(String s)
          Creates a Literal.
static org.openrdf.model.Literal literal(String s, String l)
          Creates a Literal.
static org.openrdf.model.Literal literal(String s, org.openrdf.model.URI datatype)
          Creates a Literal.
static org.openrdf.model.Statement[] parseRDF(RDFUtils.Parser p, InputStream is)
          Parses the content of is input stream with the specified parser p using '' as base URI.
static org.openrdf.model.Statement[] parseRDF(RDFUtils.Parser p, InputStream is, String baseURI)
          Parses the content of is input stream with the specified parser p using baseURI.
static org.openrdf.model.Statement[] parseRDF(RDFUtils.Parser p, String in)
          Parses the content of in string with the specified parser p using '' as base URI.
static org.openrdf.model.Statement[] parseRDF(String resource)
          Parses the content of the resource file guessing the content format from the extension.
static org.openrdf.model.Statement quad(org.openrdf.model.Resource s, org.openrdf.model.URI p, org.openrdf.model.Value o, org.openrdf.model.Resource g)
          Creates a Statement.
static org.openrdf.model.Value toRDF(String s)
          Creates a Value.
static org.openrdf.model.Statement toTriple(String s, String p, String o)
          Creates a statement of type: toRDF(s), toRDF(p), toRDF(o)
static String toXSDDateTime(Date date)
          Prints a date to the XSD datetime format.
static org.openrdf.model.Statement triple(org.openrdf.model.Resource s, org.openrdf.model.URI p, org.openrdf.model.Value o)
          Creates a Statement.
static org.openrdf.model.URI uri(String uri)
          Creates a URI.
static org.openrdf.model.URI uri(String namespace, String localName)
          Creates a URI.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fixAbsoluteURI

public static String fixAbsoluteURI(String uri)
Fixes typical errors in an absolute URI, such as unescaped spaces.

Parameters:
uri - An absolute URI, can have typical syntax errors
Returns:
An absolute URI that is valid against the URI syntax
Throws:
IllegalArgumentException - if URI is not fixable

getXSDDate

public static String getXSDDate(String dateToBeParsed,
                                String format)
                         throws ParseException,
                                DatatypeConfigurationException
This method allows to obtain an XML Schema compliant date providing a textual representation of a date and textual a pattern for parsing it.

Parameters:
dateToBeParsed - the String containing the date.
format - the pattern as descibed in SimpleDateFormat
Returns:
a String representing the date
Throws:
ParseException
DatatypeConfigurationException

toXSDDateTime

public static String toXSDDateTime(Date date)
Prints a date to the XSD datetime format.

Parameters:
date - date to be printed.
Returns:
the string representation of the input date.

fixURIWithException

public static String fixURIWithException(String unescapedURI)
Tries to fix a potentially broken relative or absolute URI.

These appear to be good rules: Remove whitespace or '\' or '"' in beginning and end Replace space with %20 Drop the triple if it matches this regex (only protocol): ^[a-zA-Z0-9]+:(//)?$ Drop the triple if it matches this regex: ^javascript: Truncate ">.*$ from end of lines (Neko didn't quite manage to fix broken markup) Drop the triple if any of these appear in the URL: <>[]|*{}"<>\

Parameters:
unescapedURI - uri string to be unescaped.
Returns:
the unescaped string.

uri

public static org.openrdf.model.URI uri(String uri)
Creates a URI.


uri

public static org.openrdf.model.URI uri(String namespace,
                                        String localName)
Creates a URI.


literal

public static org.openrdf.model.Literal literal(String s)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(boolean b)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(byte b)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(short s)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(int i)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(long l)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(float f)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(double d)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(String s,
                                                String l)
Creates a Literal.


literal

public static org.openrdf.model.Literal literal(String s,
                                                org.openrdf.model.URI datatype)
Creates a Literal.


bnode

public static org.openrdf.model.BNode bnode(String id)
Creates a BNode.


bnode

public static org.openrdf.model.BNode bnode()
Returns:
a bnode with unique id.

getBNode

public static org.openrdf.model.BNode getBNode(String id)
Creates a BNode.


triple

public static org.openrdf.model.Statement triple(org.openrdf.model.Resource s,
                                                 org.openrdf.model.URI p,
                                                 org.openrdf.model.Value o)
Creates a Statement.


quad

public static org.openrdf.model.Statement quad(org.openrdf.model.Resource s,
                                               org.openrdf.model.URI p,
                                               org.openrdf.model.Value o,
                                               org.openrdf.model.Resource g)
Creates a Statement.


toRDF

public static org.openrdf.model.Value toRDF(String s)
Creates a Value. If s == 'a' returns an RDF.TYPE. If s.matches('[a-z0-9]+:.*') expands the corresponding prefix using PopularPrefixes.

Parameters:
s -
Returns:
a value instance.

toTriple

public static org.openrdf.model.Statement toTriple(String s,
                                                   String p,
                                                   String o)
Creates a statement of type: toRDF(s), toRDF(p), toRDF(o)

Parameters:
s - subject.
p - predicate.
o - object.
Returns:
a statement instance.

getRDFParser

public static org.openrdf.rio.RDFParser getRDFParser(RDFUtils.Parser p)
Creates a new RDFParser instance.

Parameters:
p - parser type.
Returns:
parser instance.
Throws:
IllegalArgumentException - if parser is unsupported.

getParserFromExtension

public static RDFUtils.Parser getParserFromExtension(String ext)
Returns a parser type from the given extension.

Parameters:
ext - input extension.
Returns:
parser matching the extension.
Throws:
IllegalArgumentException - if no extension matches.

parseRDF

public static org.openrdf.model.Statement[] parseRDF(RDFUtils.Parser p,
                                                     InputStream is,
                                                     String baseURI)
                                              throws org.openrdf.rio.RDFHandlerException,
                                                     IOException,
                                                     org.openrdf.rio.RDFParseException
Parses the content of is input stream with the specified parser p using baseURI.

Parameters:
p - parser type.
is - input stream containing RDF.
baseURI - base uri.
Returns:
list of statements detected within the input stream.
Throws:
org.openrdf.rio.RDFHandlerException
IOException
org.openrdf.rio.RDFParseException

parseRDF

public static org.openrdf.model.Statement[] parseRDF(RDFUtils.Parser p,
                                                     InputStream is)
                                              throws org.openrdf.rio.RDFHandlerException,
                                                     IOException,
                                                     org.openrdf.rio.RDFParseException
Parses the content of is input stream with the specified parser p using '' as base URI.

Parameters:
p - parser type.
is - input stream containing RDF.
Returns:
list of statements detected within the input stream.
Throws:
org.openrdf.rio.RDFHandlerException
IOException
org.openrdf.rio.RDFParseException

parseRDF

public static org.openrdf.model.Statement[] parseRDF(RDFUtils.Parser p,
                                                     String in)
                                              throws org.openrdf.rio.RDFHandlerException,
                                                     IOException,
                                                     org.openrdf.rio.RDFParseException
Parses the content of in string with the specified parser p using '' as base URI.

Parameters:
p - parser type.
in - input string containing RDF.
Returns:
list of statements detected within the input string.
Throws:
org.openrdf.rio.RDFHandlerException
IOException
org.openrdf.rio.RDFParseException

parseRDF

public static org.openrdf.model.Statement[] parseRDF(String resource)
                                              throws org.openrdf.rio.RDFHandlerException,
                                                     IOException,
                                                     org.openrdf.rio.RDFParseException
Parses the content of the resource file guessing the content format from the extension.

Parameters:
resource - resource name.
Returns:
the statements declared within the resource file.
Throws:
IOException - if an error occurs while reading file.
org.openrdf.rio.RDFHandlerException - if an error occurs while parsing file.
org.openrdf.rio.RDFParseException - if an error occurs while parsing file.

isAbsoluteURI

public static boolean isAbsoluteURI(String href)
Checks if href is absolute or not.

Parameters:
href - candidate URI.
Returns:
true if href is absolute, false otherwise.


Copyright © 2010-2012 The Apache Software Foundation. All Rights Reserved.