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.BNodebnode()static org.eclipse.rdf4j.model.BNodebnode(String id)Creates aBNode.static StringfixAbsoluteIRI(String uri)Fixes typical errors in an absolute org.eclipse.rdf4j.model.IRI, such as unescaped spaces.static StringfixIRIWithException(String unescapedIRI)Tries to fix a potentially broken relative or absolute URI.static org.eclipse.rdf4j.model.BNodegetBNode(String id)Creates aBNodewith 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 availableRDFFormats.static org.eclipse.rdf4j.rio.RDFParsergetParser(org.eclipse.rdf4j.rio.RDFFormat format)Creates a newRDFParserinstance.static org.eclipse.rdf4j.rio.RDFWritergetWriter(org.eclipse.rdf4j.rio.RDFFormat format, OutputStream os)Creates a newRDFWriterinstance.static org.eclipse.rdf4j.rio.RDFWritergetWriter(org.eclipse.rdf4j.rio.RDFFormat format, Writer writer)Creates a newRDFWriterinstance.static StringgetXSDDate(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.IRIiri(String iri)Creates aIRI.static org.eclipse.rdf4j.model.IRIiri(String namespace, String localName)Creates aIRI.static booleanisAbsoluteIRI(String href)Checks ifhrefis absolute or not.static org.eclipse.rdf4j.model.Literalliteral(boolean b)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(byte b)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(double d)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(float f)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(int i)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(long l)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(short s)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(String s)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(String s, String l)Creates aLiteral.static org.eclipse.rdf4j.model.Literalliteral(String s, org.eclipse.rdf4j.model.IRI datatype)Creates aLiteral.static org.eclipse.rdf4j.model.ValuemakeIRI()static org.eclipse.rdf4j.model.ValuemakeIRI(String inString)Convert string to either IRI or Literal.static org.eclipse.rdf4j.model.ResourcemakeIRI(String type, org.eclipse.rdf4j.model.IRI docIRI)static org.eclipse.rdf4j.model.ResourcemakeIRI(String type, org.eclipse.rdf4j.model.IRI docIRI, boolean addId)Creates implementation ofResourcefrom given arguments: type and docIRI.static org.eclipse.rdf4j.model.ResourcemakeIRI(org.eclipse.rdf4j.model.IRI docUri)static org.eclipse.rdf4j.model.Statement[]parseRDF(String resource)Parses the content of theresourcefile 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 ofisinput stream with the specified parserpusing''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 ofisinput stream with the specified parserpusingbaseIRI.static org.eclipse.rdf4j.model.Statement[]parseRDF(org.eclipse.rdf4j.rio.RDFFormat format, String in)Parses the content ofinstring with the specified parserpusing''as base org.eclipse.rdf4j.model.IRI.static org.eclipse.rdf4j.model.Statementquad(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.Statementquad(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.ValuetoValue(String s)Creates aValue.static StringtoXSDDateTime(Date date)Prints adateto the XSD datetime format.static org.eclipse.rdf4j.model.Statementtriple(String s, String p, String o)Creates a statement of type:toValue(s), toValue(p), toValue(o)static org.eclipse.rdf4j.model.Statementtriple(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
Stringrepresenting 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 adateto 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 theIRIlocalName- 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
bnodewith unique id.
-
getBNode
public static org.eclipse.rdf4j.model.BNode getBNode(String id)
Creates aBNodewith an MD5 digest as part of the ID.- Parameters:
id- string representation of theBNodename 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- subjectResourcep- predicateURIo- 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 availableRDFFormats.- Returns:
- an unmodifiable collection of formats.
-
getParser
public static org.eclipse.rdf4j.rio.RDFParser getParser(org.eclipse.rdf4j.rio.RDFFormat format)
Creates a newRDFParserinstance.- 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 newRDFWriterinstance.- 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 newRDFWriterinstance.- 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 IOExceptionParses the content ofisinput stream with the specified parserpusingbaseIRI.- 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 IOExceptionParses the content ofisinput stream with the specified parserpusing''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 IOExceptionParses the content ofinstring with the specified parserpusing''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 theresourcefile 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 ifhrefis absolute or not.- Parameters:
href- candidate org.eclipse.rdf4j.model.IRI.- Returns:
trueifhrefis absolute,falseotherwise.
-
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 ofResourcefrom 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 thanIRIis created. Otherwise method creates simpleLiteralxsd:string.- Parameters:
inString- an input string to manifest asValue- Returns:
- either
IRIorLiteral.
-
makeIRI
public static org.eclipse.rdf4j.model.Value makeIRI()
-
-