Class ParameterizedSparqlString

java.lang.Object
org.apache.jena.query.ParameterizedSparqlString
All Implemented Interfaces:
org.apache.jena.shared.PrefixMapping

public class ParameterizedSparqlString extends Object implements org.apache.jena.shared.PrefixMapping

A Parameterized SPARQL String is a SPARQL query/update into which values may be injected.

Injecting Values

Values may be injected in several ways:

  • By treating a variable in the SPARQL string as a parameter
  • Using JDBC style positional parameters
  • Appending values directly to the command text being built

Variable Parameters

Any variable in the command may have a value injected to it, injecting a value replaces all usages of that variable in the command i.e. substitutes the variable for a constant, injection is done by textual substitution.

Positional Parameters

You can use JDBC style positional parameters if you prefer, a JDBC style parameter is a single ? followed by whitespace or certain punctuation characters (currently ; , .). Positional parameters have a unique index which reflects the order in which they appear in the string. Positional parameters use a zero based index.

Buffer Usage

Additionally you may use this purely as a StringBuffer replacement for creating queries since it provides a large variety of convenience methods for appending things either as-is or as nodes (which causes appropriate formatting to be applied).

Intended Usage

The intended usage of this is where using a QuerySolutionMap as initial bindings is either inappropriate or not possible e.g.

  • Generating query/update strings in code without lots of error prone and messy string concatenation
  • Preparing a query/update for remote execution
  • Where you do not want to simply say some variable should have a certain value but rather wish to insert constants into the query/update in place of variables
  • Defending against SPARQL injection when creating a query/update using some external input, see SPARQL Injection notes for limitations.
  • Provide a more convenient way to prepend common prefixes to your query

This class is useful for preparing both queries and updates hence the generic name as it provides programmatic ways to replace variables in the query with constants and to add prefix and base declarations. A Query or UpdateRequest can be created using the asQuery() and asUpdate() methods assuming the command an instance represents is actually valid as a query/update.

Warnings

  1. Note that this class does not in any way check that your command is syntactically correct until such time as you try and parse it as a Query or UpdateRequest.
  2. Also note that injection is done purely based on textual replacement, it does not understand or respect variable scope in any way. For example if your command text contains sub queries you should ensure that variables within the sub query which you don't want replaced have distinct names from those in the outer query you do want replaced (or vice versa)

SPARQL Injection Notes

While this class was in part designed to prevent SPARQL injection it is by no means foolproof because it works purely at the textual level. The current version of the code addresses some possible attack vectors that the developers have identified but we do not claim to be sufficiently devious to have thought of and prevented every possible attack vector.

Therefore we strongly recommend that users concerned about SPARQL Injection attacks perform their own validation on provided parameters and test their use of this class themselves prior to its use in any security conscious deployment. We also recommend that users do not use easily guess-able variable names for their parameters as these can allow a chained injection attack though generally speaking the code should prevent these.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.apache.jena.shared.PrefixMapping

    org.apache.jena.shared.PrefixMapping.Factory, org.apache.jena.shared.PrefixMapping.IllegalPrefixException, org.apache.jena.shared.PrefixMapping.JenaLockedException
  • Field Summary

    Fields inherited from interface org.apache.jena.shared.PrefixMapping

    Extended, Standard
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new parameterized string with an empty command text
    Creates a new parameterized string
    Creates a new parameterized string
    ParameterizedSparqlString(String command, String base, org.apache.jena.shared.PrefixMapping prefixes)
    Creates a new parameterized string
    Creates a new parameterized string
    Creates a new parameterized string
    ParameterizedSparqlString(String command, QuerySolutionMap map, String base, org.apache.jena.shared.PrefixMapping prefixes)
    Creates a new parameterized string
    ParameterizedSparqlString(String command, QuerySolutionMap map, org.apache.jena.shared.PrefixMapping prefixes)
    Creates a new parameterized string
    ParameterizedSparqlString(String command, org.apache.jena.shared.PrefixMapping prefixes)
    Creates a new parameterized string
    Creates a new parameterized string
    ParameterizedSparqlString(QuerySolutionMap map, org.apache.jena.shared.PrefixMapping prefixes)
    Creates a new parameterized string
    ParameterizedSparqlString(org.apache.jena.shared.PrefixMapping prefixes)
    Creates a new parameterized string
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append(boolean b)
    Appends a boolean as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(boolean) method
    void
    append(char c)
    Appends a character as-is to the existing command text, to ensure correct formatting when used as a constant consider using one of the appendLiteral() methods
    void
    append(double d)
    Appends a double as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(double) method
    void
    append(float f)
    Appends a float as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(float) method
    void
    append(int i)
    Appends an integer as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(int) method
    void
    append(long l)
    Appends a long as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(long) method
    void
    Appends an object as-is to the existing command text, to ensure correct formatting when used as a constant consider converting into a more specific type and using the appropriate appendLiteral(), appendIri() or appendNode methods
    void
    append(String text)
    Appends some text as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(String) or appendIri(String) method as appropriate
    void
    Appends a URI to the command text as a constant using appropriate formatting
    void
    appendIri(org.apache.jena.irix.IRIx iri)
    Appends an IRI to the command text as a constant using appropriate formatting
    void
    appendLiteral(boolean b)
    Appends a boolean to the command text as a constant using appropriate formatting
    void
    appendLiteral(double d)
    Appends a double to the command text as a constant using appropriate formatting
    void
    appendLiteral(float f)
    Appends a float to the command text as a constant using appropriate formatting
    void
    Appends an integer to the command text as a constant using appropriate formatting
    void
    appendLiteral(long l)
    Appends a long to the command text as a constant using appropriate formatting
    void
    Appends a simple literal as a constant using appropriate formatting
    void
    appendLiteral(String value, String lang)
    Appends a literal with a lexical value and language to the command text as a constant using appropriate formatting
    void
    appendLiteral(String value, org.apache.jena.datatypes.RDFDatatype datatype)
    Appends a Typed Literal to the command text as a constant using appropriate formatting
    void
    Appends a date time to the command text as a constant using appropriate formatting
    void
    appendNode(org.apache.jena.graph.Node n)
    Appends a Node to the command text as a constant using appropriate formatting
    void
    appendNode(org.apache.jena.rdf.model.RDFNode n)
    Appends a Node to the command text as a constant using appropriate formatting
    Attempts to take the command text with parameters injected from the toString() method and parse it as a Query
    asQuery(Syntax syntax)
    Attempts to take the command text with parameters injected from the toString() method and parse it as a Query using the given Syntax syntax
    Attempts to take the command text with parameters injected from the toString() method and parse it as a UpdateRequest
    asUpdate(Syntax syntax)
    Attempts to take the command text with parameters injected from the toString() method and parse it as a UpdateRequest using the given Syntax
    org.apache.jena.shared.PrefixMapping
     
    void
    clearParam(int index)
    Clears the value for a positional parameter
    void
    Clears the value for a variable or values parameter so the given variable will not * have a value injected
    void
    Clears all values for variable, values and positional parameters
    Makes a full copy of this parameterized string
    copy(boolean copyParams)
    Makes a copy of the command text, base URI and prefix mapping and optionally copies parameter values
    copy(boolean copyParams, boolean copyBase, boolean copyPrefixes)
    Makes a copy of the command text and optionally copies other aspects
    expandPrefix(String prefixed)
     
    Gets the Base URI which will be prepended to a query
    Gets the basic Command Text
    Gets the eligible positional parameters i.e.
     
     
     
    org.apache.jena.graph.Node
    getParam(int index)
    Gets the current value for a positional parameter
    org.apache.jena.graph.Node
    Gets the current value for a variable parameter
    Map<Integer,org.apache.jena.graph.Node>
    Gets the map of currently set positional parameters, this will be an unmodifiable map
    Gets the syntax used for parsing when calling asQuery() or asUpdate()
    Map<String,org.apache.jena.graph.Node>
    Gets the map of currently set variable parameters, this will be an unmodifiable map
    Gets the variable names which are currently treated as variable parameters (i.e.
    boolean
     
    org.apache.jena.shared.PrefixMapping
     
    int
     
     
    org.apache.jena.shared.PrefixMapping
     
    boolean
    samePrefixMappingAs(org.apache.jena.shared.PrefixMapping other)
     
    void
    Sets the Base URI which will be prepended to the query/update
    void
    Sets the command text, overwriting any existing command text.
    void
    setIri(int index, String iri)
    Sets a positional parameter to an IRI
    void
    setIri(int index, URL url)
    Sets a positional parameter to an IRI
    void
    setIri(int index, org.apache.jena.irix.IRIx iri)
    Sets a positional parameter to an IRI
    void
    setIri(String var, String iri)
    Sets a variable parameter to an IRI
    void
    setIri(String var, URL url)
    Sets a variable parameter to an IRI
    void
    setIri(String var, org.apache.jena.irix.IRIx iri)
    Sets a variable parameter to an IRI
    void
    setLiteral(int index, boolean value)
    Sets a positional parameter to a boolean literal
    void
    setLiteral(int index, double d)
    Sets a positional parameter to a double literal
    void
    setLiteral(int index, float f)
    Sets a positional parameter to a float literal
    void
    setLiteral(int index, int i)
    Sets a positional parameter to an integer literal
    void
    setLiteral(int index, long l)
    Sets a positional parameter to an integer literal
    void
    setLiteral(int index, String value)
    Sets a positional parameter to a literal
    void
    setLiteral(int index, String value, String lang)
    Sets a positional parameter to a literal with a language
    void
    setLiteral(int index, String value, org.apache.jena.datatypes.RDFDatatype datatype)
    Sets a positional parameter to a typed literal
    void
    setLiteral(int index, Calendar dt)
    Sets a positional parameter to a date time literal
    void
    setLiteral(int index, org.apache.jena.rdf.model.Literal lit)
    Sets a positional parameter to a Literal
    void
    setLiteral(String var, boolean value)
    Sets a variable parameter to a boolean literal
    void
    setLiteral(String var, double d)
    Sets a variable parameter to a double literal
    void
    setLiteral(String var, float f)
    Sets a variable parameter to a float literal
    void
    setLiteral(String var, int i)
    Sets a variable parameter to an integer literal
    void
    setLiteral(String var, long l)
    Sets a variable parameter to an integer literal
    void
    setLiteral(String var, String value)
    Sets a variable parameter to a literal
    void
    setLiteral(String var, String value, String lang)
    Sets a variable parameter to a literal with a language
    void
    setLiteral(String var, String value, org.apache.jena.datatypes.RDFDatatype datatype)
    Sets a variable parameter to a typed literal
    void
    Sets a variable parameter to a date time literal
    void
    setLiteral(String var, org.apache.jena.rdf.model.Literal lit)
    Sets a variable parameter to a Literal
    org.apache.jena.shared.PrefixMapping
    setNsPrefix(String prefix, String uri)
     
    org.apache.jena.shared.PrefixMapping
     
    org.apache.jena.shared.PrefixMapping
    setNsPrefixes(org.apache.jena.shared.PrefixMapping other)
     
    void
    setParam(int index, org.apache.jena.graph.Node n)
    Sets a Positional Parameter
    void
    setParam(int index, org.apache.jena.rdf.model.RDFNode n)
    Sets a positional parameter
    void
    setParam(String var, org.apache.jena.graph.Node n)
    Sets a variable parameter
    void
    setParam(String var, org.apache.jena.rdf.model.RDFNode n)
    Sets a variable parameter
    void
    Sets the Parameters
    void
    setRowValues(String valueName, Collection<List<? extends org.apache.jena.rdf.model.RDFNode>> rowItems)
    Allocate multiple lists of variables to a single VALUES valueName.
    Using "valuesName" with list(list(prop_A, obj_A), list(prop_B, obj_B)) on query "VALUES (?p ?o) {?valuesName}" would produce "VALUES (?p ?o) {(prop_A obj_A) * (prop_B obj_B)}".
    void
    setSyntax(Syntax syntax)
    Sets the syntax used for parsing when calling asQuery() or asUpdate()
    void
    setValues(String valueName, Collection<? extends org.apache.jena.rdf.model.RDFNode> items)
    Assign a VALUES valueName with a multiple items.
    Can be used to assign multiple values to a single variable or single value to multiple variables (if using a List) in the SPARQL query.
    See setRowValues to assign multiple values to multiple variables.
    Using "valueName" with list(prop_A, obj_A) on query "VALUES (?p ?o) {?valueName}" * would produce "VALUES (?p ?o) {(prop_A obj_A)}".
    void
    setValues(String valueName, org.apache.jena.rdf.model.RDFNode item)
    Assign a VALUES valueName with a single item.
    Using "valueName" with Literal obj_A on query "VALUES ?o {?valueName}" would produce * "VALUES ?o {obj_A}".
    void
    setValues(Map<String,Collection<? extends org.apache.jena.rdf.model.RDFNode>> itemsMap)
    ** Sets a map of VALUES valueNames and their items.
    Can be used to assign multiple values to a single variable or single value to multiple variables (if using a List) in the SPARQL query.
    See setRowValues to assign multiple values to multiple variables.
     
    This method is where the actual work happens, the original command text is always preserved and we just generated a temporary command string by prepending the defined Base URI and namespace prefixes at the start of the command and injecting the set parameters into a copy of that base command string and return the resulting command.
    org.apache.jena.shared.PrefixMapping
    withDefaultMappings(org.apache.jena.shared.PrefixMapping map)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ParameterizedSparqlString

      public ParameterizedSparqlString(String command, QuerySolutionMap map, String base, org.apache.jena.shared.PrefixMapping prefixes)
      Creates a new parameterized string
      Parameters:
      command - Raw Command Text
      map - Initial Parameters to inject
      base - Base URI
      prefixes - Prefix Mapping
    • ParameterizedSparqlString

      public ParameterizedSparqlString(String command, QuerySolutionMap map, String base)
      Creates a new parameterized string
      Parameters:
      command - Raw Command Text
      map - Initial Parameters to inject
      base - Base URI
    • ParameterizedSparqlString

      public ParameterizedSparqlString(String command, QuerySolutionMap map, org.apache.jena.shared.PrefixMapping prefixes)
      Creates a new parameterized string
      Parameters:
      command - Raw Command Text
      map - Initial Parameters to inject
      prefixes - Prefix Mapping
    • ParameterizedSparqlString

      public ParameterizedSparqlString(String command, QuerySolutionMap map)
      Creates a new parameterized string
      Parameters:
      command - Raw Command Text
      map - Initial Parameters to inject
    • ParameterizedSparqlString

      public ParameterizedSparqlString(String command, String base, org.apache.jena.shared.PrefixMapping prefixes)
      Creates a new parameterized string
      Parameters:
      command - Raw Command Text
      base - Base URI
      prefixes - Prefix Mapping
    • ParameterizedSparqlString

      public ParameterizedSparqlString(String command, org.apache.jena.shared.PrefixMapping prefixes)
      Creates a new parameterized string
      Parameters:
      command - Raw Command Text
      prefixes - Prefix Mapping
    • ParameterizedSparqlString

      public ParameterizedSparqlString(String command, String base)
      Creates a new parameterized string
      Parameters:
      command - Raw Command Text
      base - Base URI
    • ParameterizedSparqlString

      public ParameterizedSparqlString(String command)
      Creates a new parameterized string
      Parameters:
      command - Raw Command Text
    • ParameterizedSparqlString

      public ParameterizedSparqlString(QuerySolutionMap map, org.apache.jena.shared.PrefixMapping prefixes)
      Creates a new parameterized string
      Parameters:
      map - Initial Parameters to inject
      prefixes - Prefix Mapping
    • ParameterizedSparqlString

      public ParameterizedSparqlString(QuerySolutionMap map)
      Creates a new parameterized string
      Parameters:
      map - Initial Parameters to inject
    • ParameterizedSparqlString

      public ParameterizedSparqlString(org.apache.jena.shared.PrefixMapping prefixes)
      Creates a new parameterized string
      Parameters:
      prefixes - Prefix Mapping
    • ParameterizedSparqlString

      public ParameterizedSparqlString()
      Creates a new parameterized string with an empty command text
  • Method Details

    • getSyntax

      public Syntax getSyntax()
      Gets the syntax used for parsing when calling asQuery() or asUpdate()
      Returns:
      Syntax
    • setSyntax

      public void setSyntax(Syntax syntax)
      Sets the syntax used for parsing when calling asQuery() or asUpdate()
      Parameters:
      syntax - Syntax
    • setCommandText

      public void setCommandText(String command)
      Sets the command text, overwriting any existing command text. If you want to append to the command text use one of the append(String), appendIri(String), appendLiteral(String) or appendNode(Node) methods instead
      Parameters:
      command - Command Text
    • append

      public void append(String text)
      Appends some text as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(String) or appendIri(String) method as appropriate
      Parameters:
      text - Text to append
    • append

      public void append(char c)
      Appends a character as-is to the existing command text, to ensure correct formatting when used as a constant consider using one of the appendLiteral() methods
      Parameters:
      c - Character to append
    • append

      public void append(boolean b)
      Appends a boolean as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(boolean) method
      Parameters:
      b - Boolean to append
    • append

      public void append(double d)
      Appends a double as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(double) method
      Parameters:
      d - Double to append
    • append

      public void append(float f)
      Appends a float as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(float) method
      Parameters:
      f - Float to append
    • append

      public void append(int i)
      Appends an integer as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(int) method
      Parameters:
      i - Integer to append
    • append

      public void append(long l)
      Appends a long as-is to the existing command text, to ensure correct formatting when used as a constant consider using the appendLiteral(long) method
      Parameters:
      l - Long to append
    • append

      public void append(Object obj)
      Appends an object as-is to the existing command text, to ensure correct formatting when used as a constant consider converting into a more specific type and using the appropriate appendLiteral(), appendIri() or appendNode methods
      Parameters:
      obj - Object to append
    • appendNode

      public void appendNode(org.apache.jena.graph.Node n)
      Appends a Node to the command text as a constant using appropriate formatting
      Parameters:
      n - Node to append
    • appendNode

      public void appendNode(org.apache.jena.rdf.model.RDFNode n)
      Appends a Node to the command text as a constant using appropriate formatting
      Parameters:
      n - Node to append
    • appendIri

      public void appendIri(String uri)
      Appends a URI to the command text as a constant using appropriate formatting
      Parameters:
      uri - URI to append
    • appendIri

      public void appendIri(org.apache.jena.irix.IRIx iri)
      Appends an IRI to the command text as a constant using appropriate formatting
      Parameters:
      iri - IRI to append
    • appendLiteral

      public void appendLiteral(String value)
      Appends a simple literal as a constant using appropriate formatting
      Parameters:
      value - Lexical Value
    • appendLiteral

      public void appendLiteral(String value, String lang)
      Appends a literal with a lexical value and language to the command text as a constant using appropriate formatting
      Parameters:
      value - Lexical Value
      lang - Language
    • appendLiteral

      public void appendLiteral(String value, org.apache.jena.datatypes.RDFDatatype datatype)
      Appends a Typed Literal to the command text as a constant using appropriate formatting
      Parameters:
      value - Lexical Value
      datatype - Datatype
    • appendLiteral

      public void appendLiteral(boolean b)
      Appends a boolean to the command text as a constant using appropriate formatting
      Parameters:
      b - Boolean to append
    • appendLiteral

      public void appendLiteral(int i)
      Appends an integer to the command text as a constant using appropriate formatting
      Parameters:
      i - Integer to append
    • appendLiteral

      public void appendLiteral(long l)
      Appends a long to the command text as a constant using appropriate formatting
      Parameters:
      l - Long to append
    • appendLiteral

      public void appendLiteral(float f)
      Appends a float to the command text as a constant using appropriate formatting
      Parameters:
      f - Float to append
    • appendLiteral

      public void appendLiteral(double d)
      Appends a double to the command text as a constant using appropriate formatting
      Parameters:
      d - Double to append
    • appendLiteral

      public void appendLiteral(Calendar dt)
      Appends a date time to the command text as a constant using appropriate formatting
      Parameters:
      dt - Date Time to append
    • getCommandText

      public String getCommandText()
      Gets the basic Command Text

      Note: This will not reflect any injected parameters, to see the command with injected parameters invoke the toString() method

      Returns:
      Command Text
    • setBaseUri

      public void setBaseUri(String base)
      Sets the Base URI which will be prepended to the query/update
      Parameters:
      base - Base URI
    • getBaseUri

      public String getBaseUri()
      Gets the Base URI which will be prepended to a query
      Returns:
      Base URI
    • setParams

      public void setParams(QuerySolutionMap map)
      Sets the Parameters
      Parameters:
      map - Parameters
    • setParam

      public void setParam(int index, org.apache.jena.graph.Node n)
      Sets a Positional Parameter

      Setting a parameter to null is equivalent to calling clearParam(int) for the given variable

      Parameters:
      index - Positional Index
      n - Node
    • setParam

      public void setParam(String var, org.apache.jena.graph.Node n)
      Sets a variable parameter

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      n - Value
    • setParam

      public void setParam(int index, org.apache.jena.rdf.model.RDFNode n)
      Sets a positional parameter

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      index - Positional Index
      n - Node
    • setParam

      public void setParam(String var, org.apache.jena.rdf.model.RDFNode n)
      Sets a variable parameter

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      n - Value
    • setIri

      public void setIri(int index, String iri)
      Sets a positional parameter to an IRI

      Setting a parameter to null is equivalent to calling clearParam(int) for the given index

      Parameters:
      index - Positional Index
      iri - IRI
    • setIri

      public void setIri(String var, String iri)
      Sets a variable parameter to an IRI

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      iri - IRI
    • setIri

      public void setIri(int index, org.apache.jena.irix.IRIx iri)
      Sets a positional parameter to an IRI

      Setting a parameter to null is equivalent to calling clearParam(int) for the given index

      Parameters:
      index - Positional Index
      iri - IRI
    • setIri

      public void setIri(String var, org.apache.jena.irix.IRIx iri)
      Sets a variable parameter to an IRI

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      iri - IRI
    • setIri

      public void setIri(int index, URL url)
      Sets a positional parameter to an IRI

      Setting a parameter to null is equivalent to calling clearParam(int) for the given index

      Parameters:
      index - Positional Index
      url - URL
    • setIri

      public void setIri(String var, URL url)
      Sets a variable parameter to an IRI

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      url - URL used as IRI
    • setLiteral

      public void setLiteral(int index, org.apache.jena.rdf.model.Literal lit)
      Sets a positional parameter to a Literal

      Setting a parameter to null is equivalent to calling clearParam(int) for the given index

      Parameters:
      index - Positional Index
      lit - Value
    • setLiteral

      public void setLiteral(String var, org.apache.jena.rdf.model.Literal lit)
      Sets a variable parameter to a Literal

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      lit - Value
    • setLiteral

      public void setLiteral(int index, String value)
      Sets a positional parameter to a literal

      Setting a parameter to null is equivalent to calling clearParam(int) for the given index

      Parameters:
      index - Positional Index
      value - Lexical Value
    • setLiteral

      public void setLiteral(String var, String value)
      Sets a variable parameter to a literal

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      value - Lexical Value
    • setLiteral

      public void setLiteral(int index, String value, String lang)
      Sets a positional parameter to a literal with a language

      Setting a parameter to null is equivalent to calling clearParam(int) for the given index

      Parameters:
      index - Positional index
      value - Lexical Value
      lang - Language
    • setLiteral

      public void setLiteral(String var, String value, String lang)
      Sets a variable parameter to a literal with a language

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      value - Lexical Value
      lang - Language
    • setLiteral

      public void setLiteral(int index, String value, org.apache.jena.datatypes.RDFDatatype datatype)
      Sets a positional parameter to a typed literal

      Setting a parameter to null is equivalent to calling clearParam(int) for the given index

      Parameters:
      index - Positional Index
      value - Lexical Value
      datatype - Datatype
    • setLiteral

      public void setLiteral(String var, String value, org.apache.jena.datatypes.RDFDatatype datatype)
      Sets a variable parameter to a typed literal

      Setting a parameter to null is equivalent to calling clearParam(String) for the given variable

      Parameters:
      var - Variable
      value - Lexical Value
      datatype - Datatype
    • setLiteral

      public void setLiteral(int index, boolean value)
      Sets a positional parameter to a boolean literal
      Parameters:
      index - Positional Index
      value - boolean
    • setLiteral

      public void setLiteral(String var, boolean value)
      Sets a variable parameter to a boolean literal
      Parameters:
      var - Variable
      value - boolean
    • setLiteral

      public void setLiteral(int index, int i)
      Sets a positional parameter to an integer literal
      Parameters:
      index - Positional Index
      i - Integer Value
    • setLiteral

      public void setLiteral(String var, int i)
      Sets a variable parameter to an integer literal
      Parameters:
      var - Variable
      i - Integer Value
    • setLiteral

      public void setLiteral(int index, long l)
      Sets a positional parameter to an integer literal
      Parameters:
      index - Positional Index
      l - Integer Value
    • setLiteral

      public void setLiteral(String var, long l)
      Sets a variable parameter to an integer literal
      Parameters:
      var - Variable
      l - Integer Value
    • setLiteral

      public void setLiteral(int index, float f)
      Sets a positional parameter to a float literal
      Parameters:
      index - Positional Index
      f - Float value
    • setLiteral

      public void setLiteral(String var, float f)
      Sets a variable parameter to a float literal
      Parameters:
      var - Variable
      f - Float value
    • setLiteral

      public void setLiteral(int index, double d)
      Sets a positional parameter to a double literal
      Parameters:
      index - Positional Index
      d - Double value
    • setLiteral

      public void setLiteral(String var, double d)
      Sets a variable parameter to a double literal
      Parameters:
      var - Variable
      d - Double value
    • setLiteral

      public void setLiteral(int index, Calendar dt)
      Sets a positional parameter to a date time literal
      Parameters:
      index - Positional Index
      dt - Date Time value
    • setLiteral

      public void setLiteral(String var, Calendar dt)
      Sets a variable parameter to a date time literal
      Parameters:
      var - Variable
      dt - Date Time value
    • getParam

      public org.apache.jena.graph.Node getParam(String var)
      Gets the current value for a variable parameter
      Parameters:
      var - Variable
      Returns:
      Current value or null if not set
    • getParam

      public org.apache.jena.graph.Node getParam(int index)
      Gets the current value for a positional parameter
      Parameters:
      index - Positional Index
      Returns:
      Current value or null if not set
    • getVars

      public Iterator<String> getVars()
      Gets the variable names which are currently treated as variable parameters (i.e. have values set for them)
      Returns:
      Iterator of variable names
    • getVariableParameters

      public Map<String,org.apache.jena.graph.Node> getVariableParameters()
      Gets the map of currently set variable parameters, this will be an unmodifiable map
      Returns:
      Map of variable names and values
    • getPositionalParameters

      public Map<Integer,org.apache.jena.graph.Node> getPositionalParameters()
      Gets the map of currently set positional parameters, this will be an unmodifiable map
      Returns:
      Map of positional indexes and values
    • getEligiblePositionalParameters

      public Iterator<Integer> getEligiblePositionalParameters()
      Gets the eligible positional parameters i.e. detected positional parameters that may be set in the command string as it currently stands
      Returns:
      Iterator of eligible positional parameters
    • clearParam

      public void clearParam(String var)
      Clears the value for a variable or values parameter so the given variable will not * have a value injected
      Parameters:
      var - Variable
    • clearParam

      public void clearParam(int index)
      Clears the value for a positional parameter
      Parameters:
      index - Positional Index
    • clearParams

      public void clearParams()
      Clears all values for variable, values and positional parameters
    • toString

      public String toString()

      This method is where the actual work happens, the original command text is always preserved and we just generated a temporary command string by prepending the defined Base URI and namespace prefixes at the start of the command and injecting the set parameters into a copy of that base command string and return the resulting command.

      This class makes no guarantees about the validity of the returned string for use as a SPARQL Query or Update, for example if a variable parameter was injected which was mentioned in the SELECT variables list you'd have a syntax error when you try to parse the query. If you run into issues like this try using a mixture of variable and positional parameters.

      Overrides:
      toString in class Object
      Throws:
      ARQException - May be thrown if the code detects a SPARQL Injection vulnerability because of the interaction of the command string and the injected variables
    • asQuery

      public Query asQuery() throws QueryException
      Attempts to take the command text with parameters injected from the toString() method and parse it as a Query
      Returns:
      Query if the command text is a valid SPARQL query
      Throws:
      QueryException - Thrown if the command text does not parse
    • asQuery

      public Query asQuery(Syntax syntax)
      Attempts to take the command text with parameters injected from the toString() method and parse it as a Query using the given Syntax syntax
      Returns:
      Query if the command text is a valid SPARQL query
      Throws:
      QueryException - Thrown if the command text does not parse
    • asUpdate

      public UpdateRequest asUpdate()
      Attempts to take the command text with parameters injected from the toString() method and parse it as a UpdateRequest
      Returns:
      Update if the command text is a valid SPARQL Update request (one/more update commands)
    • asUpdate

      public UpdateRequest asUpdate(Syntax syntax)
      Attempts to take the command text with parameters injected from the toString() method and parse it as a UpdateRequest using the given Syntax
      Returns:
      Update if the command text is a valid SPARQL Update request (one/more update commands)
    • copy

      Makes a full copy of this parameterized string
      Returns:
      Copy of the string
    • copy

      public ParameterizedSparqlString copy(boolean copyParams)
      Makes a copy of the command text, base URI and prefix mapping and optionally copies parameter values
      Parameters:
      copyParams - Whether to copy parameters
      Returns:
      Copy of the string
    • copy

      public ParameterizedSparqlString copy(boolean copyParams, boolean copyBase, boolean copyPrefixes)
      Makes a copy of the command text and optionally copies other aspects
      Parameters:
      copyParams - Whether to copy parameters
      copyBase - Whether to copy the Base URI
      copyPrefixes - Whether to copy the prefix mappings
      Returns:
      Copy of the string
    • setNsPrefix

      public org.apache.jena.shared.PrefixMapping setNsPrefix(String prefix, String uri)
      Specified by:
      setNsPrefix in interface org.apache.jena.shared.PrefixMapping
    • removeNsPrefix

      public org.apache.jena.shared.PrefixMapping removeNsPrefix(String prefix)
      Specified by:
      removeNsPrefix in interface org.apache.jena.shared.PrefixMapping
    • clearNsPrefixMap

      public org.apache.jena.shared.PrefixMapping clearNsPrefixMap()
      Specified by:
      clearNsPrefixMap in interface org.apache.jena.shared.PrefixMapping
    • setNsPrefixes

      public org.apache.jena.shared.PrefixMapping setNsPrefixes(org.apache.jena.shared.PrefixMapping other)
      Specified by:
      setNsPrefixes in interface org.apache.jena.shared.PrefixMapping
    • setNsPrefixes

      public org.apache.jena.shared.PrefixMapping setNsPrefixes(Map<String,String> map)
      Specified by:
      setNsPrefixes in interface org.apache.jena.shared.PrefixMapping
    • withDefaultMappings

      public org.apache.jena.shared.PrefixMapping withDefaultMappings(org.apache.jena.shared.PrefixMapping map)
      Specified by:
      withDefaultMappings in interface org.apache.jena.shared.PrefixMapping
    • getNsPrefixURI

      public String getNsPrefixURI(String prefix)
      Specified by:
      getNsPrefixURI in interface org.apache.jena.shared.PrefixMapping
    • getNsURIPrefix

      public String getNsURIPrefix(String uri)
      Specified by:
      getNsURIPrefix in interface org.apache.jena.shared.PrefixMapping
    • getNsPrefixMap

      public Map<String,String> getNsPrefixMap()
      Specified by:
      getNsPrefixMap in interface org.apache.jena.shared.PrefixMapping
    • expandPrefix

      public String expandPrefix(String prefixed)
      Specified by:
      expandPrefix in interface org.apache.jena.shared.PrefixMapping
    • shortForm

      public String shortForm(String uri)
      Specified by:
      shortForm in interface org.apache.jena.shared.PrefixMapping
    • qnameFor

      public String qnameFor(String uri)
      Specified by:
      qnameFor in interface org.apache.jena.shared.PrefixMapping
    • hasNoMappings

      public boolean hasNoMappings()
      Specified by:
      hasNoMappings in interface org.apache.jena.shared.PrefixMapping
    • numPrefixes

      public int numPrefixes()
      Specified by:
      numPrefixes in interface org.apache.jena.shared.PrefixMapping
    • lock

      public org.apache.jena.shared.PrefixMapping lock()
      Specified by:
      lock in interface org.apache.jena.shared.PrefixMapping
    • samePrefixMappingAs

      public boolean samePrefixMappingAs(org.apache.jena.shared.PrefixMapping other)
      Specified by:
      samePrefixMappingAs in interface org.apache.jena.shared.PrefixMapping
    • setValues

      public void setValues(String valueName, Collection<? extends org.apache.jena.rdf.model.RDFNode> items)
      Assign a VALUES valueName with a multiple items.
      Can be used to assign multiple values to a single variable or single value to multiple variables (if using a List) in the SPARQL query.
      See setRowValues to assign multiple values to multiple variables.
      Using "valueName" with list(prop_A, obj_A) on query "VALUES (?p ?o) {?valueName}" * would produce "VALUES (?p ?o) {(prop_A obj_A)}".
      Parameters:
      valueName -
      items -
    • setValues

      public void setValues(String valueName, org.apache.jena.rdf.model.RDFNode item)
      Assign a VALUES valueName with a single item.
      Using "valueName" with Literal obj_A on query "VALUES ?o {?valueName}" would produce * "VALUES ?o {obj_A}".
      Parameters:
      valueName -
      item -
    • setValues

      public void setValues(Map<String,Collection<? extends org.apache.jena.rdf.model.RDFNode>> itemsMap)
      ** Sets a map of VALUES valueNames and their items.
      Can be used to assign multiple values to a single variable or single value to multiple variables (if using a List) in the SPARQL query.
      See setRowValues to assign multiple values to multiple variables.
      Parameters:
      itemsMap -
    • setRowValues

      public void setRowValues(String valueName, Collection<List<? extends org.apache.jena.rdf.model.RDFNode>> rowItems)
      Allocate multiple lists of variables to a single VALUES valueName.
      Using "valuesName" with list(list(prop_A, obj_A), list(prop_B, obj_B)) on query "VALUES (?p ?o) {?valuesName}" would produce "VALUES (?p ?o) {(prop_A obj_A) * (prop_B obj_B)}".
      Parameters:
      valueName -
      rowItems -