public class Helpers
extends java.lang.Object
Constructor and Description |
---|
Helpers() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
asSparqlRow(com.hp.hpl.jena.rdf.model.Statement stmt)
Formats a statement for inclusion in a SPARQL query.
|
static java.lang.String |
cleanValue(java.lang.String value)
Remove leading and trailing quotes and whitespace if needed from a
string and unescape escaped sequences.
|
static <T> T |
deserialize(java.lang.String val,
java.lang.Class<T> type)
Deserializes a string received from a client to a Java native object.
|
static java.util.Date |
getDateFromXSD(java.lang.String xsdDateTime)
Returns a Java Date object from its XML Schema Dataype (XSD) representation in the GMT timezone.
|
static java.lang.String |
getId(com.hp.hpl.jena.ontology.OntResource resource) |
static java.lang.String |
getLabel(com.hp.hpl.jena.ontology.OntResource resource) |
static java.lang.String |
getLabel(com.hp.hpl.jena.ontology.OntResource resource,
java.lang.String languageCode) |
static ResourceType |
getType(com.hp.hpl.jena.ontology.OntResource resource) |
static <T,I> boolean |
implementsInterface(T o,
java.lang.Class<I> i)
This simple method return true is a given object implements a given
interface.
|
static java.lang.String |
literalToSparqlSyntax(com.hp.hpl.jena.rdf.model.Literal lit)
Formats a literal to a SPARQL-compatible string.
|
static com.hp.hpl.jena.rdf.model.RDFNode |
parseLiteral(java.lang.String lex,
com.hp.hpl.jena.rdf.model.impl.ModelCom model)
Parse a SPARQL string representing a literal to an actual Jena
Literal . |
static <T> T |
pickRandom(java.util.Set<T> set) |
static java.lang.String |
protectValue(java.lang.String value)
Protect a string by escaping the quotes and surrounding the string with quotes.
|
static <K,V> java.util.Map<V,java.util.Set<K>> |
reverseMap(java.util.Map<K,V> m)
Creates a new map, using the values of the initial one as keys for the new
one, and keys of the initial one as a set of values for the new one.
|
static <K,V> java.util.Map<V,java.util.Set<K>> |
reverseSetMap(java.util.Map<K,java.util.Set<V>> m) |
static <K,V> java.util.List<K> |
sortByValue(java.util.Map<K,V> m)
This static method takes a map and return the list of the key sorted by
their values in ascending order.
|
static <T> java.lang.String |
stringify(T o)
Convert primitive and collection objects to a JSON-like string.
|
static java.util.List<java.lang.String> |
tokenize(java.lang.String str,
char delimiter)
Split a string into tokens separated by the given delimiter.
|
static Pair<java.util.List<java.lang.String>,java.util.List<java.lang.String>> |
tokenizeRule(java.lang.String rule)
Tokenizes rules in the SWRL syntax.
|
static java.lang.String |
unescapeJava(java.lang.String str)
Unescapes any Java literals found in the
String . |
static void |
unescapeJava(java.io.Writer out,
java.lang.String str)
Unescapes any Java literals found in the
String to a
Writer . |
public static com.hp.hpl.jena.rdf.model.RDFNode parseLiteral(java.lang.String lex, com.hp.hpl.jena.rdf.model.impl.ModelCom model) throws IllegalStatementException
Literal
. The method actually returns a RDFNode
because if the literal is not recognized, it falls back on a generic RDFNode. To be sure the result is actually a literal, the RDFNode.isLiteral()
method can be used.lex
- the string representing the literal.model
- the model linked to the ontology in which the literal is to be createdIllegalStatementException
Details regarding the syntax of literals.
,
literalToSparqlSyntax(Literal)
public static java.lang.String literalToSparqlSyntax(com.hp.hpl.jena.rdf.model.Literal lit)
lit
- a literal to be formattedparseLiteral(String, ModelCom)
public static java.lang.String asSparqlRow(com.hp.hpl.jena.rdf.model.Statement stmt)
asSparqlRow() in PartialStatement class
public static java.util.Date getDateFromXSD(java.lang.String xsdDateTime) throws java.text.ParseException
OntModel.createTypedLiteral(Calendar.getInstance())
.xsdDateTime
- a XSD formatted datejava.text.ParseException
public static java.lang.String getLabel(com.hp.hpl.jena.ontology.OntResource resource)
public static java.lang.String getLabel(com.hp.hpl.jena.ontology.OntResource resource, java.lang.String languageCode)
public static java.lang.String getId(com.hp.hpl.jena.ontology.OntResource resource)
public static ResourceType getType(com.hp.hpl.jena.ontology.OntResource resource)
public static java.util.List<java.lang.String> tokenize(java.lang.String str, char delimiter)
str
- A string to tokenize.public static Pair<java.util.List<java.lang.String>,java.util.List<java.lang.String>> tokenizeRule(java.lang.String rule) throws InvalidRuleException
rule
- the literal form of the rule, using the SWRL syntaxInvalidRuleException
public static <K,V> java.util.List<K> sortByValue(java.util.Map<K,V> m)
K
- The type of the map keysV
- The type of the map valuesm
- The map whose key must by sortedpublic static <K,V> java.util.Map<V,java.util.Set<K>> reverseMap(java.util.Map<K,V> m)
K
- The type of the map keysV
- The type of the map valuesm
- The initial map.public static <K,V> java.util.Map<V,java.util.Set<K>> reverseSetMap(java.util.Map<K,java.util.Set<V>> m)
public static <T,I> boolean implementsInterface(T o, java.lang.Class<I> i)
o
- Any objecti
- An interfacepublic static <T> java.lang.String stringify(T o)
o
- The object to convertpublic static <T> T deserialize(java.lang.String val, java.lang.Class<T> type) throws OntologyServerException, java.lang.IllegalArgumentException
T
- type of the expected deserialized object.val
- the input string, in a JSON-like formattype
- type of the expected deserialized object.OntologyServerException
- thrown if an unparsable unicode character
is encounteredjava.lang.IllegalArgumentException
- thrown if the string couldn't be
deserialized to the expect object.public static java.lang.String cleanValue(java.lang.String value) throws OntologyServerException
value
- the string to clean.OntologyServerException
- if a unparsable unicode character is foundpublic static java.lang.String protectValue(java.lang.String value)
value
- The string to protect.public static <T> T pickRandom(java.util.Set<T> set)
public static java.lang.String unescapeJava(java.lang.String str) throws OntologyServerException
Unescapes any Java literals found in the String
.
For example, it will turn a sequence of '\'
and
'n'
into a newline character, unless the '\'
is preceded by another '\'
.
str
- the String
to unescape, may be nullString
, null
if null string inputOntologyServerException
- if a unparsable unicode character is foundpublic static void unescapeJava(java.io.Writer out, java.lang.String str) throws java.io.IOException, OntologyServerException
Unescapes any Java literals found in the String
to a
Writer
.
For example, it will turn a sequence of '\'
and
'n'
into a newline character, unless the '\'
is preceded by another '\'
.
A null
string input has no effect.
out
- the Writer
used to output unescaped charactersstr
- the String
to unescape, may be nulljava.lang.IllegalArgumentException
- if the Writer is null
java.io.IOException
- if error occurs on underlying WriterOntologyServerException
- if a unparsable unicode character is foundORO is a part of the OpenRobots framework.
openrobots@laas.fr
LAAS-CNRS 2009-2011