Class Converters

java.lang.Object
org.apache.jena.arq.querybuilder.Converters

public class Converters extends Object
A collection of static methods to convert from Objects to various types used in Query and Update construction.
  • Method Details

    • checkVar

      public static Node checkVar(Node n)
      Converts any Node_Variable nodes into Var nodes.
      Parameters:
      n - the node to check
      Returns:
      the node n or a new Var if n is an instance of Node_Variable
    • makeLiteral

      public static Node makeLiteral(Object o)
      Creates a literal from an object. If the object type is registered with the TypeMapper the associated literal string is returned. If the object is not registered an IllegalArgumentException is thrown.
      Parameters:
      o - the object to convert.
      Returns:
      the literal node.
      Throws:
      IllegalArgumentException - if object type is not registered.
    • makeLiteral

      public static Node makeLiteral(String value, String typeUri)
      Creates a literal from the value and type URI. There are several possible outcomes:
      • If the URI is registered with TypeMapper and the value is the proper lexical form for the type, the registered TypeMapper is used and calling getLiteralValue() on the returned node will return a proper object.
      • If the URI is unregistered a Datatype is created but not registered with the TypeMapper. The resulting node is properly constructed for used in output serialization, queries, or updates. Calling getLiteralValue() on the returned node will throw DatatypeFormatException. Note that if JenaParameters.enableEagerLiteralValidation is true the DatatypeFormatException will be thrown by this method.
      • If the URI is registered but the value is not a proper lexical form a DatatypeFormatException will be thrown by this method.
      Parameters:
      value - the value for the literal
      typeUri - the type URI for the literal node.
      Returns:
      the literal node.
      Throws:
      DatatypeFormatException - on errors noted above
    • makeNode

      public static Node makeNode(Object o, PrefixMapping pMapping)
      Makes a node from an object while using the associated prefix mapping.
      • Will return Node.ANY if object is null.
      • Will return the enclosed Node from a FrontsNode
      • Will return the object if it is a Node.
      • Will call NodeFactoryExtra.parseNode() using the currently defined prefixes if the object is a String
      • Will call makeLiteral() to create a literal representation if the parseNode() fails or for any other object type.
      Parameters:
      o - The object to convert (may be null).
      pMapping - The prefix mapping to use for prefix resolution.
      Returns:
      The Node value.
      See Also:
    • makeNodeOrPath

      public static Object makeNodeOrPath(Object o, PrefixMapping pMapping)
      Creates a Path or Node as appropriate.
      • Will return Node.ANY if object is null.
      • Will return the object if it is a Path
      • Will return the enclosed Node from a FrontsNode
      • Will return the object if it is a Node.
      • Will call PathParser.parse() using the prefix mapping if the object is a String
      • Will call NodeFactoryExtra.parseNode() using the currently defined prefixes if the object is a String and the PathParser.parse() fails.
      • Will call makeLiteral() to create a literal representation if the parseNode() fails or for any other object type.
      Parameters:
      o - the object that should be interpreted as a path or a node.
      pMapping - the prefix mapping to resolve path or node with
      Returns:
      the Path or Node
      See Also:
    • makeVar

      public static Var makeVar(Object o) throws ARQInternalErrorException
      Makes a Var from an object.
      • Will return Var.ANON if object is null.
      • Will return null if the object is "*" or Node_RuleVariable.WILD
      • Will return the object if it is a Var
      • Will return resolve FrontsNode to Node and then resolve to Var
      • Will return resolve Node if the Node implements Node_Variable, otherwise throws an NotAVariableException (instance of ARQInternalErrorException)
      • Will return ?x if object is "?x"
      • Will return ?x if object is "x"
      • Will return the enclosed Var of a ExprVar
      • For all other objects will return the "?" prefixed to the toString() value.
      Parameters:
      o - The object to convert.
      Returns:
      the Var value.
      Throws:
      ARQInternalErrorException
    • quoted

      public static String quoted(String q)
      A convenience method to quote a string.
      Parameters:
      q - the string to quote. Will use single quotes if there are no single quotes in the string or if the double quote is before the single quote in the string. Will use double quote otherwise.
      Returns:
      the quoted string.
    • makeValueNodes

      public static Collection<Node> makeValueNodes(Iterator<?> iter, PrefixMapping prefixMapping)
      Creates a collection of nodes from an iterator of Objects.
      Parameters:
      iter - the iterator of objects, may be null or empty.
      prefixMapping - the PrefixMapping to use when nodes are created.
      Returns:
      a Collection of nodes or null if iter is null or empty.
    • makeTriplePaths

      public static List<TriplePath> makeTriplePaths(Object s, Object p, Object o, PrefixMapping prefixMapping)
      Creates a collection of TriplePaths from the s, p, o. If s, p, or o is a collection or a String representation of a collection like "(a, b, c)" the makeCollectionTriplePaths() conversions are applied. If s and/or o is not a collection the makeNode() conversions are applied. if p is not a collection the @{code makeNodeOrPath()} conversion is applied.

      Note: Path objects are not supported in RDF collections. A custom Datatype would need to be registered to place them in collections

      Parameters:
      s - the object for the subject.
      p - the object for the predicate.
      o - the object for the object.
      prefixMapping - the PrefixMapping to resolve nodes.
      Returns:
      A list of TriplePath objects.
      See Also:
    • makeTriples

      public static List<Triple> makeTriples(Object s, Object p, Object o, PrefixMapping prefixMapping)
      Creates a collection of Triples from the s, p, o. If s, p, or o is a collection or a String representation of a collection like "(a, b, c)" the makeCollectionTriples() conversions are applied. If s, p, or o is not a collection the makeNode() conversions are applied. This differs from makeTriplePaths(Object, Object, Object, PrefixMapping) in that the p may not be a path.

      Note: Path objects are not supported in RDF collections. A custom Datatype would need to be registered to place them in collections

      Parameters:
      s - the object for the subject.
      p - the object for the predicate.
      o - the object for the object.
      prefixMapping - the PrefixMapping to resolve nodes.
      Returns:
      A list of Triple objects.
      See Also:
    • makeCollectionTriples

      public static List<Triple> makeCollectionTriples(Object collection, PrefixMapping prefixMapping)
      Create an RDF collection from a collection of objects as per http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#collections

      Embedded collections are recursively expanded and added to the resulting list. Other object types in the collections are converted using makeNode PrefixMapping)}.

      Note: Path objects are not supported in RDF collections. A custom Datatype would need to be registered to place them in collections

      Usage:
      • In most cases direct calls to makeCollection are unnecessary as passing the collection to methods like addWhere(Object, Object, Object) will correctly create and add the list.
      • In cases where makeCollectionTriples is called the Subject of the first Triple is the RDF Collection node.

      Parameters:
      collection - the collections of objects for the list.
      Returns:
      A list of Triple objects.
      See Also:
    • makeCollectionTriplePaths

      public static List<TriplePath> makeCollectionTriplePaths(Object n, PrefixMapping prefixMapping)
      Create an RDF collection from a collection of objects as per http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#collections.

      Embedded collections are recursively expanded and added to the resulting list. Other object types in the collections are converted using makeNode PrefixMapping)}.

      Note: Path objects are not supported in RDF collections. A custom Datatype would need to be registered to place them in collections

      Usage:
      • In most cases direct calls to makeCollectionTriplePaths are unnecessary as passing the collection to methods like addWhere(Object, Object, Object) will correctly create and add the list.
      • In cases where makeCollectionTriplePath is called the Subject of the first TriplePath is the RDF Collection node.

      Parameters:
      n - the collections of objects for the list.
      Returns:
      A list of TriplePath objects.
      See Also: