Class ParamsParser
Synopsis
This is a multi-purpose special parser for parameters in a command or other map. It is called special because it does take a few liberties which are not commonly found in regular languages, but which may be desirable for casual use. This is done in a very specific way such that interpretation is unambiguous.
Basic Format
The input line consists of a sequence of names and values, called assignments.
These occur as name=value, and may be delimited by spaces or semicolons, like
name1=value1 name2=value2 or name1=value1;name2=value2.
Values can even contain spaces, even though space is a possible delimiter.
Thus, name1= value foo bar
name2=baz works, with the values value foo bar and baz.
Names
The name of a parameter must appear in raw literal form, with no escaping. A name may not contain spaces, semicolons or equals signs, and may not start with quotes of any kind. Otherwise, there are no restrictions placed on the characters that may appear in a name. Even escaped characters are allowed.Values
Values can appear as single quoted values, double quoted values, or literal values. The way the value is parsed is determined by the leading character, respectively.Single Quoted
If the leading character is a single quote, then all following characters up to the next single quote are taken as the value, except for escape characters which work as expected. Double quotes, spaces, equals or any other characters have no special meaning within a single quoted value.
Double Quoted
If the leading character is a double quote, then all following characters up to the next double quote are taken as the value, except for escape characters which work as expected. Single quotes, spaces, equals or other characters have no special meaning within double quotes.
Literal
Otherwise, the value is taken as every single character, including spaces, single quotes, and double quotes, up to the end of the command line or the next parameter assignment. Any occurrence of semicolons which is not escaped will be treated as a delimiter. The next occurrence of a name and equals pattern after a space will be taken as the next named parameter. Otherwise, all spaces and partial word are included in the last value assigment found. Leading spaces on literal values are skipped unless escaped.
Detection
When a caller wants to parse this format optionally when the format is recognizable as having parameters, thehasValues(String) method can be called. To be recognized as having parameters, a more strict
definition is used: The patter must start with a simple assignment having a varname which starts with an
alphabetic character or an underscore, followed by any alpha-numeric, dot ., dash -, or
underscore _ with an assignment character = following. The following regex can be used
as an example for documentation purposes when explaining op mapping conventions to users:
[_a-zA-Z][-_.\\w]*
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanstatic booleanParse a string input as a loose-form param=value list, and be reasonable about formatting conventions that most users would follow in the absence of detailed rules.parseToMap(Object src, String mainField) If the source object is a Map, return it as-is.
-
Field Details
-
ASSIGN_CHARS
- See Also:
-
-
Constructor Details
-
ParamsParser
public ParamsParser()
-
-
Method Details
-
parse
-
hasValues
-
hasValues
-
parse
public static Map<String,String> parse(String input, String assignmentCharacters, boolean canonicalize) Parse a string input as a loose-form param=value list, and be reasonable about formatting conventions that most users would follow in the absence of detailed rules. SeeParamsParserfor more details on how this works.- Parameters:
input- The string form containing the parameter names and values to be extracted.canonicalize- Whether or not to replace synonyms with modern forms and to warn when old forms are used- Returns:
- A map of extracted keys and values
-
parseToMap
If the source object is a Map, return it as-is. If it is a parseable character sequence with assignments, parse it into a map and return it. Otherwise, if it is a character sequence with no parsable key=value assignments, assume it to be a short-form declaration of a type of selector field like 'name' or 'type', and compose it into a map with the provided main field name.- Parameters:
src- The source Map or CharSequencemainField- The name of the main field in 'value only' mode.- Returns:
- A map
- Throws:
RuntimeException- if the src is neither CharSequence nor Map
-