Package org.apache.any23.rdf
Class RDFUtils
- java.lang.Object
-
- org.apache.any23.rdf.RDFUtils
-
public class RDFUtils extends Object
Basic class providing a set of utility methods when dealing with RDF.- Author:
- Michele Mostarda (mostarda@fbk.eu), Davide Palmisano (dpalmisano@gmail.com), Jacek Grzebyta (jgrzebyta@apache.org)
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.eclipse.rdf4j.model.BNode
bnode()
static org.eclipse.rdf4j.model.BNode
bnode(String id)
Creates aBNode
.static String
fixAbsoluteIRI(String uri)
Fixes typical errors in an absolute org.eclipse.rdf4j.model.IRI, such as unescaped spaces.static String
fixIRIWithException(String unescapedIRI)
Tries to fix a potentially broken relative or absolute URI.static org.eclipse.rdf4j.model.BNode
getBNode(String id)
Creates aBNode
with an MD5 digest as part of the ID.static Optional<org.eclipse.rdf4j.rio.RDFFormat>
getFormatByExtension(String ext)
Returns a parser type from the given extension.static Collection<org.eclipse.rdf4j.rio.RDFFormat>
getFormats()
Returns all the availableRDFFormat
s.static org.eclipse.rdf4j.rio.RDFParser
getParser(org.eclipse.rdf4j.rio.RDFFormat format)
Creates a newRDFParser
instance.static org.eclipse.rdf4j.rio.RDFWriter
getWriter(org.eclipse.rdf4j.rio.RDFFormat format, OutputStream os)
Creates a newRDFWriter
instance.static org.eclipse.rdf4j.rio.RDFWriter
getWriter(org.eclipse.rdf4j.rio.RDFFormat format, Writer writer)
Creates a newRDFWriter
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 org.eclipse.rdf4j.model.IRI
iri(String iri)
Creates aIRI
.static org.eclipse.rdf4j.model.IRI
iri(String namespace, String localName)
Creates aIRI
.static boolean
isAbsoluteIRI(String href)
Checks ifhref
is absolute or not.static org.eclipse.rdf4j.model.Literal
literal(boolean b)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(byte b)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(double d)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(float f)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(int i)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(long l)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(short s)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(String s)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(String s, String l)
Creates aLiteral
.static org.eclipse.rdf4j.model.Literal
literal(String s, org.eclipse.rdf4j.model.IRI datatype)
Creates aLiteral
.static org.eclipse.rdf4j.model.Value
makeIRI()
static org.eclipse.rdf4j.model.Value
makeIRI(String inString)
Convert string to either IRI or Literal.static org.eclipse.rdf4j.model.Resource
makeIRI(String type, org.eclipse.rdf4j.model.IRI docIRI)
static org.eclipse.rdf4j.model.Resource
makeIRI(String type, org.eclipse.rdf4j.model.IRI docIRI, boolean addId)
Creates implementation ofResource
from given arguments: type and docIRI.static org.eclipse.rdf4j.model.Resource
makeIRI(org.eclipse.rdf4j.model.IRI docUri)
static org.eclipse.rdf4j.model.Statement[]
parseRDF(String resource)
Parses the content of theresource
file guessing the content format from the extension.static org.eclipse.rdf4j.model.Statement[]
parseRDF(org.eclipse.rdf4j.rio.RDFFormat format, InputStream is)
Parses the content ofis
input stream with the specified parserp
using''
as base org.eclipse.rdf4j.model.IRI.static org.eclipse.rdf4j.model.Statement[]
parseRDF(org.eclipse.rdf4j.rio.RDFFormat format, InputStream is, String baseIRI)
Parses the content ofis
input stream with the specified parserp
usingbaseIRI
.static org.eclipse.rdf4j.model.Statement[]
parseRDF(org.eclipse.rdf4j.rio.RDFFormat format, String in)
Parses the content ofin
string with the specified parserp
using''
as base org.eclipse.rdf4j.model.IRI.static org.eclipse.rdf4j.model.Statement
quad(String s, String p, String o, String g)
Creates a statement of type:toValue(s), toValue(p), toValue(o), toValue(g)
static org.eclipse.rdf4j.model.Statement
quad(org.eclipse.rdf4j.model.Resource s, org.eclipse.rdf4j.model.IRI p, org.eclipse.rdf4j.model.Value o, org.eclipse.rdf4j.model.Resource g)
Creates aStatement
.static org.eclipse.rdf4j.model.Value
toValue(String s)
Creates aValue
.static String
toXSDDateTime(Date date)
Prints adate
to the XSD datetime format.static org.eclipse.rdf4j.model.Statement
triple(String s, String p, String o)
Creates a statement of type:toValue(s), toValue(p), toValue(o)
static org.eclipse.rdf4j.model.Statement
triple(org.eclipse.rdf4j.model.Resource s, org.eclipse.rdf4j.model.IRI p, org.eclipse.rdf4j.model.Value o)
Creates aStatement
.
-
-
-
Method Detail
-
fixAbsoluteIRI
public static String fixAbsoluteIRI(String uri)
Fixes typical errors in an absolute org.eclipse.rdf4j.model.IRI, such as unescaped spaces.- Parameters:
uri
- An absolute org.eclipse.rdf4j.model.IRI, can have typical syntax errors- Returns:
- An absolute org.eclipse.rdf4j.model.IRI that is valid against the org.eclipse.rdf4j.model.IRI syntax
- Throws:
IllegalArgumentException
- if org.eclipse.rdf4j.model.IRI 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 inSimpleDateFormat
- Returns:
- a
String
representing the date - Throws:
ParseException
- if there is an error parsing the given date.DatatypeConfigurationException
- if there is a serious configuration error.
-
toXSDDateTime
public static String toXSDDateTime(Date date)
Prints adate
to the XSD datetime format.- Parameters:
date
- date to be printed.- Returns:
- the string representation of the input date.
-
fixIRIWithException
public static String fixIRIWithException(String unescapedIRI)
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:
unescapedIRI
- uri string to be unescaped.- Returns:
- the unescaped string.
-
iri
public static org.eclipse.rdf4j.model.IRI iri(String iri)
Creates aIRI
.- Parameters:
iri
- a base string for theIRI
- Returns:
- a valid
IRI
-
iri
public static org.eclipse.rdf4j.model.IRI iri(String namespace, String localName)
Creates aIRI
.- Parameters:
namespace
- a base namespace for theIRI
localName
- a local name to associate with the namespace- Returns:
- a valid
IRI
-
literal
public static org.eclipse.rdf4j.model.Literal literal(String s)
Creates aLiteral
.- Parameters:
s
- string representation of theLiteral
- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(boolean b)
Creates aLiteral
.- Parameters:
b
- boolean representation of theLiteral
- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(byte b)
Creates aLiteral
.- Parameters:
b
- byte representation of theLiteral
- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(short s)
Creates aLiteral
.- Parameters:
s
- short representation of theLiteral
- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(int i)
Creates aLiteral
.- Parameters:
i
- int representation of theLiteral
- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(long l)
Creates aLiteral
.- Parameters:
l
- long representation of theLiteral
- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(float f)
Creates aLiteral
.- Parameters:
f
- float representation of theLiteral
- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(double d)
Creates aLiteral
.- Parameters:
d
- double representation of theLiteral
- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(String s, String l)
Creates aLiteral
.- Parameters:
s
- the literal's labell
- the literal's language- Returns:
- valid
Literal
-
literal
public static org.eclipse.rdf4j.model.Literal literal(String s, org.eclipse.rdf4j.model.IRI datatype)
Creates aLiteral
.- Parameters:
s
- the literal's labeldatatype
- the literal's datatype- Returns:
- valid
Literal
-
bnode
public static org.eclipse.rdf4j.model.BNode bnode(String id)
Creates aBNode
.- Parameters:
id
- string representation of theBNode
- Returns:
- the valid
BNode
-
bnode
public static org.eclipse.rdf4j.model.BNode bnode()
- Returns:
- a
bnode
with unique id.
-
getBNode
public static org.eclipse.rdf4j.model.BNode getBNode(String id)
Creates aBNode
with an MD5 digest as part of the ID.- Parameters:
id
- string representation of theBNode
name for which we will create a md5 hash.- Returns:
- the valid
BNode
-
triple
public static org.eclipse.rdf4j.model.Statement triple(org.eclipse.rdf4j.model.Resource s, org.eclipse.rdf4j.model.IRI p, org.eclipse.rdf4j.model.Value o)
Creates aStatement
.- Parameters:
s
- subjectResource
p
- predicateURI
o
- objectValue
- Returns:
- valid
Statement
-
triple
public static org.eclipse.rdf4j.model.Statement triple(String s, String p, String o)
Creates a statement of type:toValue(s), toValue(p), toValue(o)
- Parameters:
s
- subject.p
- predicate.o
- object.- Returns:
- a statement instance.
-
quad
public static org.eclipse.rdf4j.model.Statement quad(org.eclipse.rdf4j.model.Resource s, org.eclipse.rdf4j.model.IRI p, org.eclipse.rdf4j.model.Value o, org.eclipse.rdf4j.model.Resource g)
Creates aStatement
.- Parameters:
s
- subject.p
- predicate.o
- object.g
- quad resource- Returns:
- a statement instance.
-
quad
public static org.eclipse.rdf4j.model.Statement quad(String s, String p, String o, String g)
Creates a statement of type:toValue(s), toValue(p), toValue(o), toValue(g)
- Parameters:
s
- subject.p
- predicate.o
- object.g
- quad resource- Returns:
- a statement instance.
-
toValue
public static org.eclipse.rdf4j.model.Value toValue(String s)
Creates aValue
. Ifs == 'a'
returns anRDF.TYPE
. Ifs.matches('[a-z0-9]+:.*')
expands the corresponding prefix usingPopularPrefixes
.- Parameters:
s
- string representation of value.- Returns:
- a value instance.
-
getFormats
public static Collection<org.eclipse.rdf4j.rio.RDFFormat> getFormats()
Returns all the availableRDFFormat
s.- Returns:
- an unmodifiable collection of formats.
-
getParser
public static org.eclipse.rdf4j.rio.RDFParser getParser(org.eclipse.rdf4j.rio.RDFFormat format)
Creates a newRDFParser
instance.- Parameters:
format
- parser format.- Returns:
- parser instance.
- Throws:
IllegalArgumentException
- if format is not supported.
-
getWriter
public static org.eclipse.rdf4j.rio.RDFWriter getWriter(org.eclipse.rdf4j.rio.RDFFormat format, Writer writer)
Creates a newRDFWriter
instance.- Parameters:
format
- output format.writer
- data output writer.- Returns:
- writer instance.
- Throws:
IllegalArgumentException
- if format is not supported.
-
getWriter
public static org.eclipse.rdf4j.rio.RDFWriter getWriter(org.eclipse.rdf4j.rio.RDFFormat format, OutputStream os)
Creates a newRDFWriter
instance.- Parameters:
format
- output format.os
- output stream.- Returns:
- writer instance.
- Throws:
IllegalArgumentException
- if format is not supported.
-
getFormatByExtension
public static Optional<org.eclipse.rdf4j.rio.RDFFormat> getFormatByExtension(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.eclipse.rdf4j.model.Statement[] parseRDF(org.eclipse.rdf4j.rio.RDFFormat format, InputStream is, String baseIRI) throws IOException
Parses the content ofis
input stream with the specified parserp
usingbaseIRI
.- Parameters:
format
- input format type.is
- input stream containingRDF
.baseIRI
- base uri.- Returns:
- list of statements detected within the input stream.
- Throws:
IOException
- if there is an error reading theInputStream
-
parseRDF
public static org.eclipse.rdf4j.model.Statement[] parseRDF(org.eclipse.rdf4j.rio.RDFFormat format, InputStream is) throws IOException
Parses the content ofis
input stream with the specified parserp
using''
as base org.eclipse.rdf4j.model.IRI.- Parameters:
format
- input format type.is
- input stream containingRDF
.- Returns:
- list of statements detected within the input stream.
- Throws:
IOException
- if there is an error reading theInputStream
-
parseRDF
public static org.eclipse.rdf4j.model.Statement[] parseRDF(org.eclipse.rdf4j.rio.RDFFormat format, String in) throws IOException
Parses the content ofin
string with the specified parserp
using''
as base org.eclipse.rdf4j.model.IRI.- Parameters:
format
- input format type.in
- input string containingRDF
.- Returns:
- list of statements detected within the input string.
- Throws:
IOException
- if there is an error reading theInputStream
-
parseRDF
public static org.eclipse.rdf4j.model.Statement[] parseRDF(String resource) throws IOException
Parses the content of theresource
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.
-
isAbsoluteIRI
public static boolean isAbsoluteIRI(String href)
Checks ifhref
is absolute or not.- Parameters:
href
- candidate org.eclipse.rdf4j.model.IRI.- Returns:
true
ifhref
is absolute,false
otherwise.
-
makeIRI
public static org.eclipse.rdf4j.model.Resource makeIRI(org.eclipse.rdf4j.model.IRI docUri)
- Parameters:
docUri
- It is a namespace. If it ends with '/' character than stays unchanged otherwise the hash character '#' is added to the end.- Returns:
- instance of
Resource
.
-
makeIRI
public static org.eclipse.rdf4j.model.Resource makeIRI(String type, org.eclipse.rdf4j.model.IRI docIRI)
- Parameters:
type
- This argument is converted following Java naming conventions withStringUtils.implementJavaNaming(java.lang.String)
.docIRI
- It is a namespace. If it ends with '/' character than stays unchanged otherwise the hash character '#' is added to the end.- Returns:
- instance of
Resource
.
-
makeIRI
public static org.eclipse.rdf4j.model.Resource makeIRI(String type, org.eclipse.rdf4j.model.IRI docIRI, boolean addId)
Creates implementation ofResource
from given arguments: type and docIRI. NB: The Java Naming Conventions is described by GeeksForGeeks.- Parameters:
type
- This argument is converted following Java naming conventions withStringUtils.implementJavaNaming(java.lang.String)
.docIRI
- It is a namespace. If it ends with '/' character than stays unchanged otherwise the hash character '#' is added to the end.addId
- If argument is TRUE than the node identifier is added to the end formated'_{int}'
.- Returns:
- instance of
Resource
.
-
makeIRI
public static org.eclipse.rdf4j.model.Value makeIRI(String inString)
Convert string to either IRI or Literal. If string value expresses valid IRI thanIRI
is created. Otherwise method creates simpleLiteral
xsd:string.- Parameters:
inString
- an input string to manifest asValue
- Returns:
- either
IRI
orLiteral
.
-
makeIRI
public static org.eclipse.rdf4j.model.Value makeIRI()
-
-