Package io.virtdata.templates
Class ParsedTemplate
- java.lang.Object
-
- io.virtdata.templates.ParsedTemplate
-
public class ParsedTemplate extends java.lang.ObjectA parsed template is a form of a raw template which has been parsed for its named anchors and sanity checked against a set of provided bindings. Once the parsed template is constructed, the methodorError()should always called before it is used.Validity Checks
A parsed template is considered to be valid if and only if the raw template contained only named anchors which were defined in the provided bindings. Extra bindings are not presumed to make a template invalid, but this interpretation is left to the caller for extra checks if needed.Parsed Details
After parsing, the following details are available:Parsed Spans
This is an alternating list of the literal sections of the raw template interspersed with the anchor names. This list always starts and ends with a literal section, so will always contain an odd number of elements. (some span sections may be empty if necessary, but not null)Specific Bindings
These are the binding names and definitions which were used in a named anchor and also found in the provided bindings. If an anchor references a binding which is not provided, then it will not be in this map.Missing Bindings
This is a list of binding names which were found in the raw template but which were not found in the provided bindings by name.Extra Bindings
This is a list of binding names which were provided by the user, but which were not used in the raw template by name.
-
-
Constructor Summary
Constructors Constructor Description ParsedTemplate(java.lang.String rawtemplate, java.util.Map<java.lang.String,java.lang.String> providedBindings)Construct a new ParsedTemplate from the provided statement template.ParsedTemplate(java.lang.String rawtemplate, java.util.Map<java.lang.String,java.lang.String> providedBindings, java.util.regex.Pattern... providedPatterns)Parse the given raw template, check the bind points against the provide bindings, and provide detailed template checks for validity.
-
Method Summary
Modifier and Type Method Description java.util.List<java.lang.String>getAnchors()Return the list of anchors as found in the raw template.java.util.List<BindPoint>getBindPoints()Get the named anchors and their associated binding specifiers as found in the raw template.java.util.Set<java.lang.String>getExtraBindings()The list of binding names returned by this method does not constitute an error.java.util.Set<java.lang.String>getMissingBindings()Returns a list of binding names which were referenced in eitherjava.lang.StringgetPositionalStatement(java.util.function.Function<java.lang.String,java.lang.String> tokenFormatter)Return the statement that can be used as-is by any driver specific version.java.lang.String[]getSpans()Return the parsed template in (literal, variable, ..., ..., literal) form.java.util.Map<java.lang.String,java.lang.String>getSpecificBindings()Return a map of bindings which were referenced in the statement.booleanhasError()ParsedTemplateorError()java.lang.StringtoString()
-
-
-
Constructor Detail
-
ParsedTemplate
public ParsedTemplate(java.lang.String rawtemplate, java.util.Map<java.lang.String,java.lang.String> providedBindings)Construct a new ParsedTemplate from the provided statement template.- Parameters:
rawtemplate- The string that contains literal sections and anchor sections interspersed
-
ParsedTemplate
public ParsedTemplate(java.lang.String rawtemplate, java.util.Map<java.lang.String,java.lang.String> providedBindings, java.util.regex.Pattern... providedPatterns)Parse the given raw template, check the bind points against the provide bindings, and provide detailed template checks for validity.Overriding Patterns
If patterns are not provided then
DEFAULT_PATTERNSare used, which includes the ability to match {var1} and ?var1 style anchors. If patterns are provided, then they must be compatible with theMatcher.find()method, and must also have a named group with the name 'anchor', as in (?<anchor>...)- Parameters:
rawtemplate- A string template which contains optionally embedded named anchorsprovidedBindings- The bindings which are provided by the user to fulfill the named anchors in this raw templateprovidedPatterns- The patterns which match the named anchor format and extract anchor names from the raw template
-
-
Method Detail
-
orError
public ParsedTemplate orError()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
hasError
public boolean hasError()
- Returns:
- true if the parsed statement is not usable.
-
getExtraBindings
public java.util.Set<java.lang.String> getExtraBindings()
The list of binding names returned by this method does not constitute an error. They may be used for for informational purposes in error handlers, for example.- Returns:
- a set of bindings names which were provided to
this parsed statement, but which were not referenced
in either
{anchor}or?anchor
form.
-
getMissingBindings
public java.util.Set<java.lang.String> getMissingBindings()
Returns a list of binding names which were referenced in either{anchor}or?anchor
form, but which were not present in the provided bindings map. If any binding names are present in the returned set, then this binding will not be usable.- Returns:
- A list of binding names which were referenced but not defined*
-
getSpecificBindings
public java.util.Map<java.lang.String,java.lang.String> getSpecificBindings()
Return a map of bindings which were referenced in the statement. This is an easy way to get the list of effective bindings for a statement for diagnostic purposes without including a potentially long list of library bindings. This method does not represent all of the binding points, as when anchor names are used more than once.- Returns:
- a bindings map of referenced bindings in the statement
-
getAnchors
public java.util.List<java.lang.String> getAnchors()
Return the list of anchors as found in the raw template.- Returns:
-
getBindPoints
public java.util.List<BindPoint> getBindPoints()
Get the named anchors and their associated binding specifiers as found in the raw template.- Returns:
- A list of bind points
- Throws:
java.security.InvalidParameterException- if the template has an error, such as an anchor which has no provided binding.
-
getPositionalStatement
public java.lang.String getPositionalStatement(java.util.function.Function<java.lang.String,java.lang.String> tokenFormatter)
Return the statement that can be used as-is by any driver specific version. This uses the anchor token as provided to yield a version of the statement which contains positional anchors, but no named bindings.- Parameters:
tokenFormatter- The mapping from a token name to a place holder- Returns:
- A driver or usage-specific format of the statement, with anchors
-
getSpans
public java.lang.String[] getSpans()
Return the parsed template in (literal, variable, ..., ..., literal) form.- Returns:
-
-