Class Helpers


  • public class Helpers
    extends java.lang.Object
    This class provides various static helper methods for some common parsing and formatting tasks.
    • Constructor Summary

      Constructors 
      Constructor Description
      Helpers()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method 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 void unescapeJava​(java.io.Writer out, java.lang.String str)
      Unescapes any Java literals found in the String to a Writer.
      static java.lang.String unescapeJava​(java.lang.String str)
      Unescapes any Java literals found in the String.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Helpers

        public Helpers()
    • Method Detail

      • parseLiteral

        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
        Parse a SPARQL string representing a literal to an actual Jena 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.
        Parameters:
        lex - the string representing the literal.
        model - the model linked to the ontology in which the literal is to be created
        Returns:
        a RDFNode holding the literal
        Throws:
        IllegalStatementException
        See Also:
        Details regarding the syntax of literals., literalToSparqlSyntax(Literal)
      • literalToSparqlSyntax

        public static java.lang.String literalToSparqlSyntax​(com.hp.hpl.jena.rdf.model.Literal lit)
        Formats a literal to a SPARQL-compatible string.
        Parameters:
        lit - a literal to be formatted
        Returns:
        a string representing the literal with SPARQL syntax.
        See Also:
        parseLiteral(String, ModelCom)
      • asSparqlRow

        public static java.lang.String asSparqlRow​(com.hp.hpl.jena.rdf.model.Statement stmt)
        Formats a statement for inclusion in a SPARQL query.
        Returns:
        the formatted statement.
        See Also:
        asSparqlRow() in PartialStatement class
      • getDateFromXSD

        public static java.util.Date getDateFromXSD​(java.lang.String xsdDateTime)
                                             throws java.text.ParseException
        Returns a Java Date object from its XML Schema Dataype (XSD) representation in the GMT timezone. Note: the reverse operation (Date to XSD) can be achieved by OntModel.createTypedLiteral(Calendar.getInstance()).
        Parameters:
        xsdDateTime - a XSD formatted date
        Returns:
        the corresponding Java Date object.
        Throws:
        java.text.ParseException
      • getLabel

        public static java.lang.String getLabel​(com.hp.hpl.jena.ontology.OntResource resource)
      • getLabel

        public static java.lang.String getLabel​(com.hp.hpl.jena.ontology.OntResource resource,
                                                java.lang.String languageCode)
      • getId

        public static java.lang.String getId​(com.hp.hpl.jena.ontology.OntResource resource)
      • getType

        public static ResourceType getType​(com.hp.hpl.jena.ontology.OntResource resource)
      • tokenize

        public static java.util.List<java.lang.String> tokenize​(java.lang.String str,
                                                                char delimiter)
        Split a string into tokens separated by the given delimiter. It protects quoted strings and arrays delimited by [] or {}.
        Parameters:
        str - A string to tokenize.
        Returns:
        A list of tokens
      • tokenizeRule

        public static Pair<java.util.List<java.lang.String>,​java.util.List<java.lang.String>> tokenizeRule​(java.lang.String rule)
                                                                                                          throws InvalidRuleException
        Tokenizes rules in the SWRL syntax. For instance: Male(?y), hasParent(?x, ?y) -> hasFather(?x, ?y) would be converted into 2 lists: ['Male(?y)', 'hasParent(?x, ?y)'] and ['hasFather(?x, ?y)']
        Parameters:
        rule - the literal form of the rule, using the SWRL syntax
        Returns:
        a pair of list of string: the first list is the body of the rule, the second list is the head. Each atom is left in its literal form
        Throws:
        InvalidRuleException
      • sortByValue

        public 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.
        Type Parameters:
        K - The type of the map keys
        V - The type of the map values
        Parameters:
        m - The map whose key must by sorted
        Returns:
        A list of key, sorted by their values in ascending order.
      • reverseMap

        public 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.
        Type Parameters:
        K - The type of the map keys
        V - The type of the map values
        Parameters:
        m - The initial map.
        Returns:
        A new map built by inversing the keys and values of the initial one. For each new key, a set is built for all the values (ie, the former keys).
      • reverseSetMap

        public static <K,​V> java.util.Map<V,​java.util.Set<K>> reverseSetMap​(java.util.Map<K,​java.util.Set<V>> m)
      • implementsInterface

        public 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. It uses the Java reflection API.
        Parameters:
        o - Any object
        i - An interface
        Returns:
        true if the object implements the interface.
      • stringify

        public static <T> java.lang.String stringify​(T o)
        Convert primitive and collection objects to a JSON-like string. The conversion should be correct in standard cases, but it doesn't handle corner cases.
        Parameters:
        o - The object to convert
        Returns:
        A JSON-like string representating the object.
        See Also:
        The JSON website
      • deserialize

        public static <T> T deserialize​(java.lang.String val,
                                        java.lang.Class<T> type)
                                 throws OntologyServerException,
                                        java.lang.IllegalArgumentException
        Deserializes a string received from a client to a Java native object.
        Type Parameters:
        T - type of the expected deserialized object.
        Parameters:
        val - the input string, in a JSON-like format
        type - type of the expected deserialized object.
        Returns:
        the newly created object
        Throws:
        OntologyServerException - thrown if an unparsable unicode character is encountered
        java.lang.IllegalArgumentException - thrown if the string couldn't be deserialized to the expect object.
      • cleanValue

        public static java.lang.String cleanValue​(java.lang.String value)
                                           throws OntologyServerException
        Remove leading and trailing quotes and whitespace if needed from a string and unescape escaped sequences.
        Parameters:
        value - the string to clean.
        Returns:
        The same, unescaped string with quotes and whitespaces trimmed.
        Throws:
        OntologyServerException - if a unparsable unicode character is found
      • protectValue

        public static java.lang.String protectValue​(java.lang.String value)
        Protect a string by escaping the quotes and surrounding the string with quotes.
        Parameters:
        value - The string to protect.
        Returns:
        The same string between double quotes and with inside quotes escaped.
      • pickRandom

        public static <T> T pickRandom​(java.util.Set<T> set)
      • unescapeJava

        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 '\'.

        This method comes from Apache Foundation, is released under Apache License 2.0 and can be found in org.apache.commons.lang.StringEscapeUtils
        Parameters:
        str - the String to unescape, may be null
        Returns:
        a new unescaped String, null if null string input
        Throws:
        OntologyServerException - if a unparsable unicode character is found
      • unescapeJava

        public 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.

        This method comes from Apache Foundation, is released under Apache License 2.0 and can be found in org.apache.commons.lang.StringEscapeUtils
        Parameters:
        out - the Writer used to output unescaped characters
        str - the String to unescape, may be null
        Throws:
        java.lang.IllegalArgumentException - if the Writer is null
        java.io.IOException - if error occurs on underlying Writer
        OntologyServerException - if a unparsable unicode character is found