Class ParameterizedSparqlString
- java.lang.Object
-
- org.apache.jena.query.ParameterizedSparqlString
-
- All Implemented Interfaces:
PrefixMapping
public class ParameterizedSparqlString extends java.lang.Object implements 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
StringBufferreplacement 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
QuerySolutionMapas 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
QueryorUpdateRequestcan be created using theasQuery()andasUpdate()methods assuming the command an instance represents is actually valid as a query/update.Warnings
- 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
QueryorUpdateRequest. - 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
PrefixMapping.Factory, PrefixMapping.IllegalPrefixException, PrefixMapping.JenaLockedException
-
-
Field Summary
-
Fields inherited from interface org.apache.jena.shared.PrefixMapping
Extended, Standard
-
-
Constructor Summary
Constructors Constructor Description ParameterizedSparqlString()Creates a new parameterized string with an empty command textParameterizedSparqlString(java.lang.String command)Creates a new parameterized stringParameterizedSparqlString(java.lang.String command, java.lang.String base)Creates a new parameterized stringParameterizedSparqlString(java.lang.String command, java.lang.String base, PrefixMapping prefixes)Creates a new parameterized stringParameterizedSparqlString(java.lang.String command, QuerySolutionMap map)Creates a new parameterized stringParameterizedSparqlString(java.lang.String command, QuerySolutionMap map, java.lang.String base)Creates a new parameterized stringParameterizedSparqlString(java.lang.String command, QuerySolutionMap map, java.lang.String base, PrefixMapping prefixes)Creates a new parameterized stringParameterizedSparqlString(java.lang.String command, QuerySolutionMap map, PrefixMapping prefixes)Creates a new parameterized stringParameterizedSparqlString(java.lang.String command, PrefixMapping prefixes)Creates a new parameterized stringParameterizedSparqlString(QuerySolutionMap map)Creates a new parameterized stringParameterizedSparqlString(QuerySolutionMap map, PrefixMapping prefixes)Creates a new parameterized stringParameterizedSparqlString(PrefixMapping prefixes)Creates a new parameterized string
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidappend(boolean b)Appends a boolean as-is to the existing command text, to ensure correct formatting when used as a constant consider using theappendLiteral(boolean)methodvoidappend(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 theappendLiteral()methodsvoidappend(double d)Appends a double as-is to the existing command text, to ensure correct formatting when used as a constant consider using theappendLiteral(double)methodvoidappend(float f)Appends a float as-is to the existing command text, to ensure correct formatting when used as a constant consider using theappendLiteral(float)methodvoidappend(int i)Appends an integer as-is to the existing command text, to ensure correct formatting when used as a constant consider using theappendLiteral(int)methodvoidappend(long l)Appends a long as-is to the existing command text, to ensure correct formatting when used as a constant consider using theappendLiteral(long)methodvoidappend(java.lang.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 appropriateappendLiteral(),appendIri()orappendNodemethodsvoidappend(java.lang.String text)Appends some text as-is to the existing command text, to ensure correct formatting when used as a constant consider using theappendLiteral(String)orappendIri(String)method as appropriatevoidappendIri(java.lang.String uri)Appends a URI to the command text as a constant using appropriate formattingvoidappendIri(org.apache.jena.iri.IRI iri)Deprecated.UseappendIri(IRIx)voidappendIri(IRIx iri)Appends an IRI to the command text as a constant using appropriate formattingvoidappendLiteral(boolean b)Appends a boolean to the command text as a constant using appropriate formattingvoidappendLiteral(double d)Appends a double to the command text as a constant using appropriate formattingvoidappendLiteral(float f)Appends a float to the command text as a constant using appropriate formattingvoidappendLiteral(int i)Appends an integer to the command text as a constant using appropriate formattingvoidappendLiteral(long l)Appends a long to the command text as a constant using appropriate formattingvoidappendLiteral(java.lang.String value)Appends a simple literal as a constant using appropriate formattingvoidappendLiteral(java.lang.String value, java.lang.String lang)Appends a literal with a lexical value and language to the command text as a constant using appropriate formattingvoidappendLiteral(java.lang.String value, RDFDatatype datatype)Appends a Typed Literal to the command text as a constant using appropriate formattingvoidappendLiteral(java.util.Calendar dt)Appends a date time to the command text as a constant using appropriate formattingvoidappendNode(Node n)Appends a Node to the command text as a constant using appropriate formattingvoidappendNode(RDFNode n)Appends a Node to the command text as a constant using appropriate formattingQueryasQuery()Attempts to take the command text with parameters injected from thetoString()method and parse it as aQueryQueryasQuery(Syntax syntax)Attempts to take the command text with parameters injected from thetoString()method and parse it as aQueryusing the givenSyntaxsyntaxUpdateRequestasUpdate()Attempts to take the command text with parameters injected from thetoString()method and parse it as aUpdateRequestUpdateRequestasUpdate(Syntax syntax)Attempts to take the command text with parameters injected from thetoString()method and parse it as aUpdateRequestusing the givenSyntaxPrefixMappingclearNsPrefixMap()voidclearParam(int index)Clears the value for a positional parametervoidclearParam(java.lang.String var)Clears the value for a variable or values parameter so the given variable will not * have a value injectedvoidclearParams()Clears all values for variable, values and positional parametersParameterizedSparqlStringcopy()Makes a full copy of this parameterized stringParameterizedSparqlStringcopy(boolean copyParams)Makes a copy of the command text, base URI and prefix mapping and optionally copies parameter valuesParameterizedSparqlStringcopy(boolean copyParams, boolean copyBase, boolean copyPrefixes)Makes a copy of the command text and optionally copies other aspectsjava.lang.StringexpandPrefix(java.lang.String prefixed)java.lang.StringgetBaseUri()Gets the Base URI which will be prepended to a queryjava.lang.StringgetCommandText()Gets the basic Command Textjava.util.Iterator<java.lang.Integer>getEligiblePositionalParameters()Gets the eligible positional parameters i.e.java.util.Map<java.lang.String,java.lang.String>getNsPrefixMap()java.lang.StringgetNsPrefixURI(java.lang.String prefix)java.lang.StringgetNsURIPrefix(java.lang.String uri)NodegetParam(int index)Gets the current value for a positional parameterNodegetParam(java.lang.String var)Gets the current value for a variable parameterjava.util.Map<java.lang.Integer,Node>getPositionalParameters()Gets the map of currently set positional parameters, this will be an unmodifiable mapSyntaxgetSyntax()Gets the syntax used for parsing when callingasQuery()orasUpdate()java.util.Map<java.lang.String,Node>getVariableParameters()Gets the map of currently set variable parameters, this will be an unmodifiable mapjava.util.Iterator<java.lang.String>getVars()Deprecated.booleanhasNoMappings()PrefixMappinglock()intnumPrefixes()java.lang.StringqnameFor(java.lang.String uri)PrefixMappingremoveNsPrefix(java.lang.String prefix)booleansamePrefixMappingAs(PrefixMapping other)voidsetBaseUri(java.lang.String base)Sets the Base URI which will be prepended to the query/updatevoidsetCommandText(java.lang.String command)Sets the command text, overwriting any existing command text.voidsetIri(int index, java.lang.String iri)Sets a positional parameter to an IRIvoidsetIri(int index, java.net.URL url)Sets a positional parameter to an IRIvoidsetIri(int index, org.apache.jena.iri.IRI iri)Deprecated.voidsetIri(int index, IRIx iri)Sets a positional parameter to an IRIvoidsetIri(java.lang.String var, java.lang.String iri)Sets a variable parameter to an IRIvoidsetIri(java.lang.String var, java.net.URL url)Sets a variable parameter to an IRIvoidsetIri(java.lang.String var, org.apache.jena.iri.IRI iri)Deprecated.voidsetIri(java.lang.String var, IRIx iri)Sets a variable parameter to an IRIvoidsetLiteral(int index, boolean value)Sets a positional parameter to a boolean literalvoidsetLiteral(int index, double d)Sets a positional parameter to a double literalvoidsetLiteral(int index, float f)Sets a positional parameter to a float literalvoidsetLiteral(int index, int i)Sets a positional parameter to an integer literalvoidsetLiteral(int index, long l)Sets a positional parameter to an integer literalvoidsetLiteral(int index, java.lang.String value)Sets a positional parameter to a literalvoidsetLiteral(int index, java.lang.String value, java.lang.String lang)Sets a positional parameter to a literal with a languagevoidsetLiteral(int index, java.lang.String value, RDFDatatype datatype)Sets a positional parameter to a typed literalvoidsetLiteral(int index, java.util.Calendar dt)Sets a positional parameter to a date time literalvoidsetLiteral(int index, Literal lit)Sets a positional parameter to a LiteralvoidsetLiteral(java.lang.String var, boolean value)Sets a variable parameter to a boolean literalvoidsetLiteral(java.lang.String var, double d)Sets a variable parameter to a double literalvoidsetLiteral(java.lang.String var, float f)Sets a variable parameter to a float literalvoidsetLiteral(java.lang.String var, int i)Sets a variable parameter to an integer literalvoidsetLiteral(java.lang.String var, long l)Sets a variable parameter to an integer literalvoidsetLiteral(java.lang.String var, java.lang.String value)Sets a variable parameter to a literalvoidsetLiteral(java.lang.String var, java.lang.String value, java.lang.String lang)Sets a variable parameter to a literal with a languagevoidsetLiteral(java.lang.String var, java.lang.String value, RDFDatatype datatype)Sets a variable parameter to a typed literalvoidsetLiteral(java.lang.String var, java.util.Calendar dt)Sets a variable parameter to a date time literalvoidsetLiteral(java.lang.String var, Literal lit)Sets a variable parameter to a LiteralPrefixMappingsetNsPrefix(java.lang.String prefix, java.lang.String uri)PrefixMappingsetNsPrefixes(java.util.Map<java.lang.String,java.lang.String> map)PrefixMappingsetNsPrefixes(PrefixMapping other)voidsetParam(int index, Node n)Sets a Positional ParametervoidsetParam(int index, RDFNode n)Sets a positional parametervoidsetParam(java.lang.String var, Node n)Sets a variable parametervoidsetParam(java.lang.String var, RDFNode n)Sets a variable parametervoidsetParams(QuerySolutionMap map)Sets the ParametersvoidsetRowValues(java.lang.String valueName, java.util.Collection<java.util.List<? extends 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)}".voidsetSyntax(Syntax syntax)Sets the syntax used for parsing when callingasQuery()orasUpdate()voidsetValues(java.lang.String valueName, java.util.Collection<? extends 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)}".voidsetValues(java.lang.String valueName, 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}".voidsetValues(java.util.Map<java.lang.String,java.util.Collection<? extends 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.java.lang.StringshortForm(java.lang.String uri)java.lang.StringtoString()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.PrefixMappingwithDefaultMappings(PrefixMapping map)
-
-
-
Constructor Detail
-
ParameterizedSparqlString
public ParameterizedSparqlString(java.lang.String command, QuerySolutionMap map, java.lang.String base, PrefixMapping prefixes)Creates a new parameterized string- Parameters:
command- Raw Command Textmap- Initial Parameters to injectbase- Base URIprefixes- Prefix Mapping
-
ParameterizedSparqlString
public ParameterizedSparqlString(java.lang.String command, QuerySolutionMap map, java.lang.String base)Creates a new parameterized string- Parameters:
command- Raw Command Textmap- Initial Parameters to injectbase- Base URI
-
ParameterizedSparqlString
public ParameterizedSparqlString(java.lang.String command, QuerySolutionMap map, PrefixMapping prefixes)Creates a new parameterized string- Parameters:
command- Raw Command Textmap- Initial Parameters to injectprefixes- Prefix Mapping
-
ParameterizedSparqlString
public ParameterizedSparqlString(java.lang.String command, QuerySolutionMap map)Creates a new parameterized string- Parameters:
command- Raw Command Textmap- Initial Parameters to inject
-
ParameterizedSparqlString
public ParameterizedSparqlString(java.lang.String command, java.lang.String base, PrefixMapping prefixes)Creates a new parameterized string- Parameters:
command- Raw Command Textbase- Base URIprefixes- Prefix Mapping
-
ParameterizedSparqlString
public ParameterizedSparqlString(java.lang.String command, PrefixMapping prefixes)Creates a new parameterized string- Parameters:
command- Raw Command Textprefixes- Prefix Mapping
-
ParameterizedSparqlString
public ParameterizedSparqlString(java.lang.String command, java.lang.String base)Creates a new parameterized string- Parameters:
command- Raw Command Textbase- Base URI
-
ParameterizedSparqlString
public ParameterizedSparqlString(java.lang.String command)
Creates a new parameterized string- Parameters:
command- Raw Command Text
-
ParameterizedSparqlString
public ParameterizedSparqlString(QuerySolutionMap map, PrefixMapping prefixes)
Creates a new parameterized string- Parameters:
map- Initial Parameters to injectprefixes- Prefix Mapping
-
ParameterizedSparqlString
public ParameterizedSparqlString(QuerySolutionMap map)
Creates a new parameterized string- Parameters:
map- Initial Parameters to inject
-
ParameterizedSparqlString
public ParameterizedSparqlString(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 Detail
-
getSyntax
public Syntax getSyntax()
Gets the syntax used for parsing when callingasQuery()orasUpdate()- Returns:
- Syntax
-
setSyntax
public void setSyntax(Syntax syntax)
Sets the syntax used for parsing when callingasQuery()orasUpdate()- Parameters:
syntax- Syntax
-
setCommandText
public void setCommandText(java.lang.String command)
Sets the command text, overwriting any existing command text. If you want to append to the command text use one of theappend(String),appendIri(String),appendLiteral(String)orappendNode(Node)methods instead- Parameters:
command- Command Text
-
append
public void append(java.lang.String text)
Appends some text as-is to the existing command text, to ensure correct formatting when used as a constant consider using theappendLiteral(String)orappendIri(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 theappendLiteral()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 theappendLiteral(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 theappendLiteral(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 theappendLiteral(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 theappendLiteral(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 theappendLiteral(long)method- Parameters:
l- Long to append
-
append
public void append(java.lang.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 appropriateappendLiteral(),appendIri()orappendNodemethods- Parameters:
obj- Object to append
-
appendNode
public void appendNode(Node n)
Appends a Node to the command text as a constant using appropriate formatting- Parameters:
n- Node to append
-
appendNode
public void appendNode(RDFNode n)
Appends a Node to the command text as a constant using appropriate formatting- Parameters:
n- Node to append
-
appendIri
public void appendIri(java.lang.String uri)
Appends a URI to the command text as a constant using appropriate formatting- Parameters:
uri- URI to append
-
appendIri
public void appendIri(IRIx iri)
Appends an IRI to the command text as a constant using appropriate formatting- Parameters:
iri- IRI to append
-
appendIri
@Deprecated public void appendIri(org.apache.jena.iri.IRI iri)
Deprecated.UseappendIri(IRIx)Appends an IRI to the command text as a constant using appropriate formatting- Parameters:
iri- IRI to append
-
appendLiteral
public void appendLiteral(java.lang.String value)
Appends a simple literal as a constant using appropriate formatting- Parameters:
value- Lexical Value
-
appendLiteral
public void appendLiteral(java.lang.String value, java.lang.String lang)Appends a literal with a lexical value and language to the command text as a constant using appropriate formatting- Parameters:
value- Lexical Valuelang- Language
-
appendLiteral
public void appendLiteral(java.lang.String value, RDFDatatype datatype)Appends a Typed Literal to the command text as a constant using appropriate formatting- Parameters:
value- Lexical Valuedatatype- 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(java.util.Calendar dt)
Appends a date time to the command text as a constant using appropriate formatting- Parameters:
dt- Date Time to append
-
getCommandText
public java.lang.String getCommandText()
Gets the basic Command TextNote: 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(java.lang.String base)
Sets the Base URI which will be prepended to the query/update- Parameters:
base- Base URI
-
getBaseUri
public java.lang.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, Node n)Sets a Positional ParameterSetting a parameter to null is equivalent to calling
clearParam(int)for the given variable- Parameters:
index- Positional Indexn- Node
-
setParam
public void setParam(java.lang.String var, Node n)Sets a variable parameterSetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variablen- Value
-
setParam
public void setParam(int index, RDFNode n)Sets a positional parameterSetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
index- Positional Indexn- Node
-
setParam
public void setParam(java.lang.String var, RDFNode n)Sets a variable parameterSetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variablen- Value
-
setIri
public void setIri(int index, java.lang.String iri)Sets a positional parameter to an IRISetting a parameter to null is equivalent to calling
clearParam(int)for the given index- Parameters:
index- Positional Indexiri- IRI
-
setIri
public void setIri(java.lang.String var, java.lang.String iri)Sets a variable parameter to an IRISetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variableiri- IRI
-
setIri
@Deprecated public void setIri(int index, org.apache.jena.iri.IRI iri)Deprecated.Sets a positional parameter to an IRISetting a parameter to null is equivalent to calling
clearParam(int)for the given index- Parameters:
index- Positional Indexiri- IRI
-
setIri
public void setIri(int index, IRIx iri)Sets a positional parameter to an IRISetting a parameter to null is equivalent to calling
clearParam(int)for the given index- Parameters:
index- Positional Indexiri- IRI
-
setIri
public void setIri(java.lang.String var, IRIx iri)Sets a variable parameter to an IRISetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variableiri- IRI
-
setIri
@Deprecated public void setIri(java.lang.String var, org.apache.jena.iri.IRI iri)Deprecated.Sets a variable parameter to an IRISetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variableiri- IRI
-
setIri
public void setIri(int index, java.net.URL url)Sets a positional parameter to an IRISetting a parameter to null is equivalent to calling
clearParam(int)for the given index- Parameters:
index- Positional Indexurl- URL
-
setIri
public void setIri(java.lang.String var, java.net.URL url)Sets a variable parameter to an IRISetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variableurl- URL used as IRI
-
setLiteral
public void setLiteral(int index, Literal lit)Sets a positional parameter to a LiteralSetting a parameter to null is equivalent to calling
clearParam(int)for the given index- Parameters:
index- Positional Indexlit- Value
-
setLiteral
public void setLiteral(java.lang.String var, Literal lit)Sets a variable parameter to a LiteralSetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variablelit- Value
-
setLiteral
public void setLiteral(int index, java.lang.String value)Sets a positional parameter to a literalSetting a parameter to null is equivalent to calling
clearParam(int)for the given index- Parameters:
index- Positional Indexvalue- Lexical Value
-
setLiteral
public void setLiteral(java.lang.String var, java.lang.String value)Sets a variable parameter to a literalSetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variablevalue- Lexical Value
-
setLiteral
public void setLiteral(int index, java.lang.String value, java.lang.String lang)Sets a positional parameter to a literal with a languageSetting a parameter to null is equivalent to calling
clearParam(int)for the given index- Parameters:
index- Positional indexvalue- Lexical Valuelang- Language
-
setLiteral
public void setLiteral(java.lang.String var, java.lang.String value, java.lang.String lang)Sets a variable parameter to a literal with a languageSetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variablevalue- Lexical Valuelang- Language
-
setLiteral
public void setLiteral(int index, java.lang.String value, RDFDatatype datatype)Sets a positional parameter to a typed literalSetting a parameter to null is equivalent to calling
clearParam(int)for the given index- Parameters:
index- Positional Indexvalue- Lexical Valuedatatype- Datatype
-
setLiteral
public void setLiteral(java.lang.String var, java.lang.String value, RDFDatatype datatype)Sets a variable parameter to a typed literalSetting a parameter to null is equivalent to calling
clearParam(String)for the given variable- Parameters:
var- Variablevalue- Lexical Valuedatatype- Datatype
-
setLiteral
public void setLiteral(int index, boolean value)Sets a positional parameter to a boolean literal- Parameters:
index- Positional Indexvalue- boolean
-
setLiteral
public void setLiteral(java.lang.String var, boolean value)Sets a variable parameter to a boolean literal- Parameters:
var- Variablevalue- boolean
-
setLiteral
public void setLiteral(int index, int i)Sets a positional parameter to an integer literal- Parameters:
index- Positional Indexi- Integer Value
-
setLiteral
public void setLiteral(java.lang.String var, int i)Sets a variable parameter to an integer literal- Parameters:
var- Variablei- Integer Value
-
setLiteral
public void setLiteral(int index, long l)Sets a positional parameter to an integer literal- Parameters:
index- Positional Indexl- Integer Value
-
setLiteral
public void setLiteral(java.lang.String var, long l)Sets a variable parameter to an integer literal- Parameters:
var- Variablel- Integer Value
-
setLiteral
public void setLiteral(int index, float f)Sets a positional parameter to a float literal- Parameters:
index- Positional Indexf- Float value
-
setLiteral
public void setLiteral(java.lang.String var, float f)Sets a variable parameter to a float literal- Parameters:
var- Variablef- Float value
-
setLiteral
public void setLiteral(int index, double d)Sets a positional parameter to a double literal- Parameters:
index- Positional Indexd- Double value
-
setLiteral
public void setLiteral(java.lang.String var, double d)Sets a variable parameter to a double literal- Parameters:
var- Variabled- Double value
-
setLiteral
public void setLiteral(int index, java.util.Calendar dt)Sets a positional parameter to a date time literal- Parameters:
index- Positional Indexdt- Date Time value
-
setLiteral
public void setLiteral(java.lang.String var, java.util.Calendar dt)Sets a variable parameter to a date time literal- Parameters:
var- Variabledt- Date Time value
-
getParam
public Node getParam(java.lang.String var)
Gets the current value for a variable parameter- Parameters:
var- Variable- Returns:
- Current value or null if not set
-
getParam
public 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
@Deprecated public java.util.Iterator<java.lang.String> getVars()
Deprecated.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 java.util.Map<java.lang.String,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 java.util.Map<java.lang.Integer,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 java.util.Iterator<java.lang.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(java.lang.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 java.lang.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:
toStringin classjava.lang.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 thetoString()method and parse it as aQuery- 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 thetoString()method and parse it as aQueryusing the givenSyntaxsyntax- 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 thetoString()method and parse it as aUpdateRequest- 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 thetoString()method and parse it as aUpdateRequestusing the givenSyntax- Returns:
- Update if the command text is a valid SPARQL Update request (one/more update commands)
-
copy
public ParameterizedSparqlString 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 parameterscopyBase- Whether to copy the Base URIcopyPrefixes- Whether to copy the prefix mappings- Returns:
- Copy of the string
-
setNsPrefix
public PrefixMapping setNsPrefix(java.lang.String prefix, java.lang.String uri)
- Specified by:
setNsPrefixin interfacePrefixMapping
-
removeNsPrefix
public PrefixMapping removeNsPrefix(java.lang.String prefix)
- Specified by:
removeNsPrefixin interfacePrefixMapping
-
clearNsPrefixMap
public PrefixMapping clearNsPrefixMap()
- Specified by:
clearNsPrefixMapin interfacePrefixMapping
-
setNsPrefixes
public PrefixMapping setNsPrefixes(PrefixMapping other)
- Specified by:
setNsPrefixesin interfacePrefixMapping
-
setNsPrefixes
public PrefixMapping setNsPrefixes(java.util.Map<java.lang.String,java.lang.String> map)
- Specified by:
setNsPrefixesin interfacePrefixMapping
-
withDefaultMappings
public PrefixMapping withDefaultMappings(PrefixMapping map)
- Specified by:
withDefaultMappingsin interfacePrefixMapping
-
getNsPrefixURI
public java.lang.String getNsPrefixURI(java.lang.String prefix)
- Specified by:
getNsPrefixURIin interfacePrefixMapping
-
getNsURIPrefix
public java.lang.String getNsURIPrefix(java.lang.String uri)
- Specified by:
getNsURIPrefixin interfacePrefixMapping
-
getNsPrefixMap
public java.util.Map<java.lang.String,java.lang.String> getNsPrefixMap()
- Specified by:
getNsPrefixMapin interfacePrefixMapping
-
expandPrefix
public java.lang.String expandPrefix(java.lang.String prefixed)
- Specified by:
expandPrefixin interfacePrefixMapping
-
shortForm
public java.lang.String shortForm(java.lang.String uri)
- Specified by:
shortFormin interfacePrefixMapping
-
qnameFor
public java.lang.String qnameFor(java.lang.String uri)
- Specified by:
qnameForin interfacePrefixMapping
-
hasNoMappings
public boolean hasNoMappings()
- Specified by:
hasNoMappingsin interfacePrefixMapping
-
numPrefixes
public int numPrefixes()
- Specified by:
numPrefixesin interfacePrefixMapping
-
lock
public PrefixMapping lock()
- Specified by:
lockin interfacePrefixMapping
-
samePrefixMappingAs
public boolean samePrefixMappingAs(PrefixMapping other)
- Specified by:
samePrefixMappingAsin interfacePrefixMapping
-
setValues
public void setValues(java.lang.String valueName, java.util.Collection<? extends 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(java.lang.String valueName, 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(java.util.Map<java.lang.String,java.util.Collection<? extends 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(java.lang.String valueName, java.util.Collection<java.util.List<? extends 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-
-
-