public class TokenParserImpl extends Object implements TokenParser, ConfigurationChangeListener
T-Plan Robot Enterprise, (C) 2009-2022 T-Plan Limited. All rights reserved.
Modifier and Type | Field and Description |
---|---|
static String |
CFG_USE_LEGACY_EVALUATOR |
static Character |
PARAM_VALUE_SEPARATOR |
FILE_PATH_SEPARATOR
Modifier and Type | Method and Description |
---|---|
String |
buttonToString(int button)
Convert a
MouseEvent button constant to a TPR string one. |
String |
colorArrayToString(Color[] c)
Convert an array of colors to a semicolon separated list of HTML-style
color representation.
|
String |
colorToString(Color c)
Convert color to HTML-style RGB string.
|
void |
configurationChanged(ConfigurationChangeEvent evt)
This method gets called when a configuration parameter is changed.
|
String |
dimensionToString(Dimension d)
Convert dimensions to a string.
|
Object |
evaluateNumericExpression(String expression,
Class resultType)
Evaluate a numeric or boolean expression.
|
Object |
evaluateNumericExpression(String expression,
Class resultType,
ScriptingContext context)
Evaluate a numeric or boolean expression.
|
String |
fileListToString(File[] files)
Convert a list of files to the semicolon separated string format
understood by TPR scripts.
|
String |
fileListToString(File[] files,
File defaultPath)
Convert a list of files to the semicolon separated string format
understood by TPR scripts.
|
String |
fileListToString(File[] files,
File defaultPath,
boolean forceRelative)
Convert a list of files to the semicolon separated string format
understood by TPR scripts.
|
ExpressionEvaluator |
getEvaluator()
Get the numeric and boolean expression evaluator.
|
static TokenParser |
getParser() |
String |
modifiersToString(int modifiers)
Convert extended key event modifiers to the TPR format.
|
Map |
parse(String params)
Parse the argument string into a map of [param, value] pairs.
|
Map<String,String> |
parse(String params,
List v)
Parse the argument string into a map of [param, value] pairs.
|
Map<String,String> |
parse(String params,
List<String> v,
List<int[]> l) |
Map<String,String> |
parse(String params,
List<String> v,
List<int[]> l,
boolean throwExceptionOnErrors,
int[] errorOffset) |
Map<String,String> |
parse(String params,
List<String> v,
List<int[]> l,
boolean throwExceptionOnErrors,
int[] errorOffset,
boolean returnOriginalValues) |
Map<String,String> |
parse(String params,
List v,
boolean throwExceptionOnErrors,
int[] errorOffset)
Parse the argument string for parameters and parameter-value pairs.
|
Map<String,String> |
parse(String params,
List v,
boolean throwExceptionOnErrors,
int[] errorOffset,
boolean returnOriginalValues)
Parse the argument string for parameters and parameter-value pairs.
|
LinkedHashMap<String,Object> |
parse2map(String command) |
Boolean |
parseBoolean(Object obj,
String paramName)
Parse a boolean number.
|
Color |
parseColor(String s)
Parse an HTML-style RGB color.
|
Color[] |
parseColorArray(String s)
Parse a list of semicolon separated colors in the HTML format.
|
String |
parseCommandName(String command)
Get the command name (the first word) from the argument command line.
|
Dimension |
parseDimension(Object obj,
Dimension defaults,
String paramName,
ScriptingContext ctx)
Parse dimension (the [width, height] value pair) from the argument
object.
|
File[] |
parseFileList(String fileList)
Parse file list from the TPR semicolon separated format.
|
File[] |
parseFileList(String fileList,
File defaultPath)
Parse file list from the TPR semicolon separated format.
|
List<String> |
parseForValues(String values)
Parse a white space separated list of values (tokens).
|
Number |
parseInteger(Object obj,
String paramName)
Parse an integer number.
|
Number |
parseInteger(Object obj,
String paramName,
ScriptingContext ctx)
Parse an integer number.
|
int |
parseKeyLocation(String location)
Convert TPR specified key location to the
KeyEvent one. |
int |
parseModifiers(String modifiers)
Parse the key modifiers.
|
int |
parseMouseButton(String button)
Parse the mouse button.
|
Number |
parseNumber(Object obj,
String paramName)
Parse a number.
|
Number |
parseNumber(Object obj,
String paramName,
ScriptingContext ctx)
Parse a number.
|
Map<String,String> |
parseParameters(String command)
Parse parameters from a command line.
|
Map<String,String> |
parseParameters(String command,
List v)
Parse parameters from a command line.
|
Map<String,String> |
parseParameters(String command,
List v,
boolean returnOriginalValues)
Parse parameters from a command line.
|
String |
parseParameterString(String command)
Get the parameter substring from a command line.
|
Number |
parsePercentage(Object obj,
String paramName)
Parse a percentage such as "23.5%".
|
Number |
parsePercentage(Object obj,
String paramName,
ScriptingContext ctx)
Parse a number which may be terminated with a percent sign '%'.
|
RelativePoint |
parsePoint(Object obj,
String paramName)
Parse a point from the argument object.
|
RelativePoint |
parsePoint(Object obj,
String paramName,
ScriptingContext ctx)
Parse a point from the argument object.
|
Map<String,String> |
parsePointParams(String s,
String paramName)
Parse point parameters from the argument object.
|
Rectangle |
parseRectangle(Object obj,
Rectangle defaults,
String paramName)
Parse a rectangle from the argument object.
|
Rectangle |
parseRectangle(Object obj,
Rectangle defaults,
String paramName,
ScriptingContext ctx)
Parse a rectangle from the argument object.
|
Rectangle |
parseRectangle(Object obj,
String paramName)
Parse a rectangle from the argument object.
|
Map<String,String> |
parseRectangleParams(String s,
String paramName)
Parse rectangle parameters from the argument object.
|
Number |
parseTime(Object obj,
String paramName)
Parse time from a string.The method always returns the time value in
milliseconds.
|
Number |
parseTime(Object obj,
String paramName,
Object[] components)
Parse time from a string.The method always returns the time value in
milliseconds.
|
Number |
parseTime(Object obj,
String paramName,
Object[] components,
ScriptingContext ctx)
Parse a time value.
|
String |
pointToString(Point p)
Convert the argument point to a string.
|
String |
readToken(char[] ca,
int beg)
This will read a string in quotes or just a single string.Reading stops
when the final quote is reached (quoted string) or when a space or equal
sign is reached (unquoted string).
|
String |
rectToString(Rectangle r)
Convert the argument rectangle to a string.
|
public static final Character PARAM_VALUE_SEPARATOR
public static final String CFG_USE_LEGACY_EVALUATOR
public static TokenParser getParser()
public Map parse(String params)
TokenParser
parse
in interface TokenParser
params
- a string containing parameters.public String readToken(char[] ca, int beg)
This will read a string in quotes or just a single string.Reading stops when the final quote is reached (quoted string) or when a space or equal sign is reached (unquoted string).
Examples:
readToken
in interface TokenParser
ca
- the input string characters.beg
- the start offset.public Map<String,String> parse(String params, List v)
TokenParser
parse
in interface TokenParser
params
- a string containing parameters.v
- optional list to store the parameter names to. The parameters
will be stored in the same order as they are parsed.public Map<String,String> parse(String params, List v, boolean throwExceptionOnErrors, int[] errorOffset)
TokenParser
parse
in interface TokenParser
params
- the string to parse.v
- a list to store the parameter names to.throwExceptionOnErrors
- whether to throw an error on syntax error,
for example on an unescaped or a missing enclosing double quote.errorOffset
- optional output array of length=1 to store the index
of the error to. It is used only when throwExceptionOnErrors
is true.public Map<String,String> parse(String params, List v, boolean throwExceptionOnErrors, int[] errorOffset, boolean returnOriginalValues)
TokenParser
parse
in interface TokenParser
params
- the string to parse.v
- a list to store the parameter names to.throwExceptionOnErrors
- whether to throw an error on syntax error,
for example on an unescaped or a missing enclosing double quote.errorOffset
- optional output array of length=1 to store the index
of the error to. It is used only when throwExceptionOnErrors
is true.returnOriginalValues
- a flag indicating whether to fix obvious errors in
the values such as unescaped quotes.public LinkedHashMap<String,Object> parse2map(String command)
parse2map
in interface TokenParser
public Map<String,String> parse(String params, List<String> v, List<int[]> l)
parse
in interface TokenParser
public Map<String,String> parse(String params, List<String> v, List<int[]> l, boolean throwExceptionOnErrors, int[] errorOffset)
public Map<String,String> parse(String params, List<String> v, List<int[]> l, boolean throwExceptionOnErrors, int[] errorOffset, boolean returnOriginalValues)
public Number parseNumber(Object obj, String paramName) throws SyntaxErrorException
TokenParser
parseNumber
in interface TokenParser
obj
- the value to parse.paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not a number.public Number parseNumber(Object obj, String paramName, ScriptingContext ctx) throws SyntaxErrorException
TokenParser
parseNumber
in interface TokenParser
obj
- the value to parse.paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".ctx
- scripting context (optional). This parameter is available
since the 2.3.3 release. If the context is not null and it is a
compilation one, the method reports no error when the value contains one
or more calls of variables which are not known.SyntaxErrorException
- when the value is not a number or the
context is an execution one and the value contains an unresolved variable
call.public Number parseInteger(Object obj, String paramName) throws SyntaxErrorException
TokenParser
parseInteger
in interface TokenParser
obj
- the value to parse.paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not an integer.public Number parseInteger(Object obj, String paramName, ScriptingContext ctx) throws SyntaxErrorException
TokenParser
parseInteger
in interface TokenParser
obj
- the value to parse.paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".ctx
- scripting context (optional). This parameter is available
since the 2.3.3 release. If the context is not null and it is a
compilation one, the method reports no error when the value contains one
or more calls of variables which are not known.SyntaxErrorException
- when the value is not an integer or the
context is an execution one and the value contains an unresolved variable
call.public Boolean parseBoolean(Object obj, String paramName) throws SyntaxErrorException
TokenParser
parseBoolean
in interface TokenParser
obj
- the value to parse.paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not a boolean.public Number parsePercentage(Object obj, String paramName) throws SyntaxErrorException
TokenParser
parsePercentage
in interface TokenParser
obj
- the value to parse.paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not a valid percentage.public Number parsePercentage(Object obj, String paramName, ScriptingContext ctx) throws SyntaxErrorException
TokenParser
parsePercentage
in interface TokenParser
obj
- the value to parse.paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".ctx
- scripting context (optional). This parameter is available
since the 2.3.3 release. If the context is not null and it is a
compilation one, the method reports no error when the value contains one
or more calls of variables which are not known.SyntaxErrorException
- when the value is not a number or the
context is an execution one and the value contains an unresolved variable
call.public Number parseTime(Object obj, String paramName) throws SyntaxErrorException
Parse time from a string.The method always returns the time value in milliseconds.
Supported formats are: - '5' = 5 ms - '5s' = 5 seconds = 5000 ms - '5m' = 5 minutes = 5*60*1000 ms - '5h' = 5 hours = 5*60*60*1000 ms - '5d' = 5 days = 5*24*60*60*1000 ms
The formats are case sensitive.
parseTime
in interface TokenParser
obj
- a String or a Number containing timeparamName
- parameter name (to be mentioned in the eventually thrown
exception)SyntaxErrorException
- on an argument syntax error.NumberFormatException
- when the string doesn't comply with the
format.public Number parseTime(Object obj, String paramName, Object[] components) throws SyntaxErrorException
TokenParser
parseTime
in interface TokenParser
obj
- an object containing time (it may be a String or Number). See
the
TokenParser.parseTime(java.lang.Object, java.lang.String, java.lang.Object[], com.tplan.robot.scripting.ScriptingContext)
method for details on the supported time formats.paramName
- parameter name (to be mentioned in the eventually thrown
exception)components
- optional input-output array (length=3 or higher) to
store the parsed number (components[0]), time unit (components[1]) and
the unit factor used to convert the number to milliseconds
(components[2]).SyntaxErrorException
- on a syntax error.public Number parseTime(Object obj, String paramName, Object[] components, ScriptingContext ctx) throws SyntaxErrorException
TokenParser
parseTime
in interface TokenParser
obj
- the value to parse.paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".components
- optional input-output array (length=3 or higher) to
store the parsed number (components[0]), time unit (components[1]) and
the unit factor used to convert the number to milliseconds
(components[2]).ctx
- scripting context (optional). This parameter is available
since the 2.3.3 release. If the context is not null and it is a
compilation one, the method reports no error when the value contains one
or more calls of variables which are not known.SyntaxErrorException
- when the value is not a valid time value or
the context is an execution one and the value contains an unresolved
variable call.public RelativePoint parsePoint(Object obj, String paramName) throws SyntaxErrorException
TokenParser
parsePoint
in interface TokenParser
obj
- a value to parse from, for example "x:0,y:36".paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not a valid point.public RelativePoint parsePoint(Object obj, String paramName, ScriptingContext ctx) throws SyntaxErrorException
TokenParser
parsePoint
in interface TokenParser
obj
- a value to parse from, for example "x:0,y:36".paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".ctx
- a script context.SyntaxErrorException
- when the value is not a valid point.public Map<String,String> parsePointParams(String s, String paramName) throws SyntaxErrorException
TokenParser
parsePointParams
in interface TokenParser
s
- a value to parse from, for example "x:0,y:36".paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not a valid point.public Map<String,String> parseRectangleParams(String s, String paramName) throws SyntaxErrorException
TokenParser
parseRectangleParams
in interface TokenParser
s
- a value to parse from, for example "x:0,y:0,w:100,h:100".paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not a valid rectangle.public Rectangle parseRectangle(Object obj, String paramName) throws SyntaxErrorException
TokenParser
parseRectangle
in interface TokenParser
obj
- a value to parse from, for example "x:0,y:0,w:100,h:100".paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not a valid rectangle.
variable call.public Rectangle parseRectangle(Object obj, Rectangle defaults, String paramName) throws SyntaxErrorException
TokenParser
parseRectangle
in interface TokenParser
obj
- a value to parse from, for example "x:0,y:0,w:100,h:100".defaults
- optional screen dimensions. They are used to evaluate
percentage values. For example, if "defaults=x:0,y:0,w:600,h:800" and the
argument rectangle is "x:10%,y:0,w:50%,h:800" it will be resolved as
"x:60,y:0,w:300,h:800".paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".SyntaxErrorException
- when the value is not a valid rectangle.
variable call.public Rectangle parseRectangle(Object obj, Rectangle defaults, String paramName, ScriptingContext ctx) throws SyntaxErrorException
TokenParser
parseRectangle
in interface TokenParser
obj
- a value to parse from, for example "x:0,y:0,w:100,h:100".defaults
- optional screen dimensions. They are used to evaluate
percentage values. For example, if "defaults=x:0,y:0,w:600,h:800" and the
argument rectangle is "x:10%,y:0,w:50%,h:800" it will be resolved as
"x:60,y:0,w:300,h:800".paramName
- parameter name (optional). If it is specified and
parsing fails the method will throw an exception saying something like
"Parameter 'paramname' parsing failed".ctx
- scripting context (optional). This parameter is available
since the 2.3.3 release. If the context is not null and it is a
compilation one, the method reports no error when the value contains one
or more calls of variables which are not known.SyntaxErrorException
- when the value is not a valid rectangle or
the context is an execution one and the value contains an unresolved
variable call.public String rectToString(Rectangle r)
TokenParser
rectToString
in interface TokenParser
r
- a rectangle.public String pointToString(Point p)
TokenParser
pointToString
in interface TokenParser
p
- the point to convert to string.public int parseMouseButton(String button) throws SyntaxErrorException
TokenParser
parseMouseButton
in interface TokenParser
button
- the button to parse. The TPR language supports three button
types which will be parsed as follows:
MouseEvent.BUTTON1
,MouseEvent.BUTTON2
,MouseEvent.BUTTON3
.MouseEvent
compliant mouse button code.SyntaxErrorException
- when the argument is not a valid button
type.public int parseModifiers(String modifiers) throws SyntaxErrorException
TokenParser
parseModifiers
in interface TokenParser
modifiers
- the value to parse. The TPR language supports three
modifiers which will be parsed as follows:
InputEvent.ALT_MASK
,InputEvent.SHIFT_MASK
,InputEvent.CTRL_MASK
.InputEvent.ALT_MASK
| InputEvent.SHIFT_MASK
.InputEvent
compatible modifier mask.SyntaxErrorException
- when the argument is not a valid modifier or
a combination of modifiers.public int parseKeyLocation(String location) throws SyntaxErrorException
TokenParser
KeyEvent
one.parseKeyLocation
in interface TokenParser
location
- a TPR location constant, one of "location, standard,
numpad, left".KeyEvent
location constant.SyntaxErrorException
- on an unknown argument.public String fileListToString(File[] files)
TokenParser
fileListToString
in interface TokenParser
files
- one or more file paths.public String fileListToString(File[] files, File defaultPath)
TokenParser
fileListToString
in interface TokenParser
files
- one or more file paths.defaultPath
- if populated the method converts files contained
within that path to relative paths.public String fileListToString(File[] files, File defaultPath, boolean forceRelative)
TokenParser
fileListToString
in interface TokenParser
files
- one or more file paths.defaultPath
- if populated the method converts files to relative
paths.forceRelative
- if true and the default path is provided the method
will convert all file paths to relative ones. If false and the path is
provided it will convert to relative only files contained within that
path.public String parseCommandName(String command)
TokenParser
parseCommandName
in interface TokenParser
command
- a command line (command plus arguments and parameters).public String parseParameterString(String command)
TokenParser
parseParameterString
in interface TokenParser
command
- a command line (command plus arguments and parameters).public Map<String,String> parseParameters(String command)
TokenParser
parseParameters
in interface TokenParser
command
- a command line (command plus arguments and parameters).public Map<String,String> parseParameters(String command, List v)
TokenParser
parseParameters
in interface TokenParser
command
- a command line (command plus arguments and parameters).v
- if not null it will be populated with the parameter names in the
order they are parsed.public Map<String,String> parseParameters(String command, List v, boolean returnOriginalValues)
TokenParser
parseParameters
in interface TokenParser
command
- a command line (command plus arguments and parameters).v
- if not null it will be populated with the parameter names in the
order they are parsed.returnOriginalValues
- a flag indicating whether to fix obvious
errors in the values such as unescaped quotes.public String modifiersToString(int modifiers)
TokenParser
modifiersToString
in interface TokenParser
modifiers
- modifiers returned by
InputEvent.getModifiersEx()
.public String buttonToString(int button)
TokenParser
MouseEvent
button constant to a TPR string one.buttonToString
in interface TokenParser
button
- a button returned by MouseEvent.getButton()
.public File[] parseFileList(String fileList)
TokenParser
parseFileList
in interface TokenParser
fileList
- a TPR file list.public File[] parseFileList(String fileList, File defaultPath)
TokenParser
parseFileList
in interface TokenParser
fileList
- a TPR file list.defaultPath
- if provided then any relative paths in the argument
list will be resolved against it.public Color parseColor(String s)
parseColor
in interface TokenParser
s
- HTM-style color argument. Each of the R, G, B components must be
specified as 2-character hexadecimal number in this specific order. The
string may be optionally prefixed with the hash character '#'. Examples
of valid colors are "ffffff" or "#ffffff" (white), "ff0000" or "#ff0000"
(red), "000000" or "#000000" (black) etc.The argument may be also in a form of three semicolon separated numbers representing the R, G and B components, for example "255;0;0" is equivalent to "ff0000" and represents red color.
If the argument doesn't meet the required format, the method throws an IllegalArgumentException.
public String colorToString(Color c)
colorToString
in interface TokenParser
c
- a color.public String colorArrayToString(Color[] c)
TokenParser
colorArrayToString
in interface TokenParser
c
- a color array.TokenParser.colorToString(java.awt.Color)
method for details on the color
format.public Color[] parseColorArray(String s)
TokenParser
parseColorArray
in interface TokenParser
s
- semicolon separated list of colors. See the
TokenParser.parseColor(java.lang.String)
method for details on the color
format.public List<String> parseForValues(String values)
TokenParser
Parse a white space separated list of values (tokens). A token containing spaces must be enclosed in escaped double quotes. Any double escaped double quote will be converted to a double quote. For example, an argument like "USA \"South Africa\" \"\" \"United Kingdom (\\\"UK\\\")\"" will produce [ "USA", "South Africa", "", "United Kingdom (\"UK\")" ].
This method was introduced in 3.0.1 to support conversion of the TPR 'for' statement iterating over a set of values to Java.
parseForValues
in interface TokenParser
values
- a list of values.public Dimension parseDimension(Object obj, Dimension defaults, String paramName, ScriptingContext ctx) throws SyntaxErrorException
TokenParser
parseDimension
in interface TokenParser
obj
- an object to parse from.defaults
- the default dimensions to use if the argument object uses
percentages.paramName
- name of the parameter (will be used in the text of
eventually thrown exception).ctx
- a context (optional).SyntaxErrorException
- when the argument object does not represent
valid dimensions.public String dimensionToString(Dimension d)
TokenParser
dimensionToString
in interface TokenParser
d
- dimensions.public ExpressionEvaluator getEvaluator()
TokenParser
getEvaluator
in interface TokenParser
public Object evaluateNumericExpression(String expression, Class resultType, ScriptingContext context)
TokenParser
evaluateNumericExpression
in interface TokenParser
expression
- a numeric or a boolean expression.resultType
- the desired result type such as Boolean.class,
Double.class or Integer.class.context
- a script context.public Object evaluateNumericExpression(String expression, Class resultType)
TokenParser
evaluateNumericExpression
in interface TokenParser
expression
- a numeric or a boolean expression.resultType
- the desired result type such as Boolean.class,
Double.class or Integer.class.public void configurationChanged(ConfigurationChangeEvent evt)
ConfigurationChangeListener
configurationChanged
in interface ConfigurationChangeListener
evt
- A ConfigurationChangeEvent object describing the event source
and the parameter that has changed.