public class DefaultMatchRuleParser extends BaseMatchRuleParser
It is recommended to use the static create() methods rather than the constructors to get a new parser instance.
firstName=John & lastName=M*
It means that attribute 'firstName' must have the value "John" and that the attribute 'lastName' must match "M*", that is all values starting with a capital 'M' will evaluate to true.
A slightly different syntax for the above rule is:
firstName{John} & lastName{M*}
Using the following dictionary will evaluate the rules from above to false:
Map map = new HashMap() ; map.put( "firstName", "Conner" ) ; map.put( "lastName", "McLeod" ) ; MatchRule mr = DefaultMatchRuleParser.parseRule( "firstName{John} & lastName{M*}" ) ; boolean found = mr.matches(map) ; // returns false
The next dictionary will evaluate the rule to true:
Map map = new HashMap() ; map.put( "firstName", "John" ) ; map.put( "lastName", "Miles" ) ; MatchRule mr = DefaultMatchRuleParser.parseRule( "firstName{John} & lastName{M*}" ) ; boolean found = mr.matches(map) ; // returns true
The parser generally supports the following comparisons in rules:
Comparison | Description | Example(s) |
equals | Compares if the value of the attribute is equal to the specified value | name=Roger |
equals any | Compares if the value of the attribute is equal to any of a list of specified values | name{Fred,Peter,John,Mike} |
matches | Compares if the value of the attribute matches the specified pattern | name=J*y |
matches any | Compares if the value of the attribute matches any of a list of specified values | name{Fred,A*,B?n,R*so?,Carl} |
less | Compares if the value of the attribute is less than the specified value | name<Henderson age<20 |
greater | Compares if the value of the attribute is greater than the specified value | name>Franklin age>15 |
less or equal | Compares if the value of the attribute is less or equal to the specified value | name<=Anderson age<=50 |
greater or equal | Compares if the value of the attribute is greater or equal to the specified value | name>=Franklin age>=3 |
There are some characters with special purpose in a rule. The table below describes each of them and lists the method that can be used to change the character.
char | Description | Method to change in MatchRuleChars |
& | AND operator | setAndChar() |
| | OR operator | setOrChar( ) |
! | NOT operator | setNotChar() |
{ | Starts a list of values | setValueStartChar() |
, | Separator of values in a value list | setValueSeparatorChar() |
} | Ends a list of values | setValueEndChar() |
( | Starts a group of attribute rules | setGroupStartChar() |
) | Ends a group of attribute rules | setGroupEndChar() |
= | Compares equality of the attribute's value | setEqualsChar() |
< | Compares the attribute's value to be less than the specified value | setLessChar() |
> | Compares the attribute's value to be greater zhan the specified value | setGreaterChar() |
? | Wildcard for a single character in a value | --- |
* | Wildcard for any count of characters in a value | --- |
Any rule must comply to the following restrictions:
A more complex rule could look like this:
( city{P*,Ch*} & ! city{Paris,Pretoria} ) | ( language{en,de,fr,it,es} & currency{??D} )
The dictonary below will evaluate to true if checked against the above rule:
DefaultMatchRuleParser parser = new DefaultMatchRuleParser() ; MatchRule rule = parser.parse("( city{P*,Ch*} & ! city{Paris,Pretoria} ) | " + ( language{en,de,fr,it,es} & currency{??D} )" ) ; Map map = new HashMap() ; map.put( "city", "Pittsburg" ) ; map.put( "language", "en" ) ; map.put( "currency", "USD" ) ; boolean ok = rule.matches( map ) ;
Whereas the following values produce a false:
MatchRuleChars chars = new MatchRuleChars() ; chars.setValueSeparatorChar( ';' ) ; DefaultMatchRuleParser parser = new DefaultMatchRuleParser(chars) ; MatchRule rule = parser.parse( "( city{P*;Ch*} & ! city{Paris;Pretoria} ) | " + ( language{en;de;fr;it;es} & currency{??D} )" ) ; Map map = new HashMap() ; map.put( "city", "Pretoria" ) ; map.put( "language", "de" ) ; map.put( "currency", "USD" ) ; boolean ok = rule.matches( map ) ;
Constructor and Description |
---|
DefaultMatchRuleParser()
Initialize the new instance with default values.
|
DefaultMatchRuleParser(MatchRuleChars ruleCharacters)
Initialize the new instance with a set of rule characters.
|
Modifier and Type | Method and Description |
---|---|
static DefaultMatchRuleParser |
create()
Returns a new parser that generates rules which treat the multi-char
wildcard (i.e.
|
static DefaultMatchRuleParser |
create(MatchRuleChars chars)
Returns a new parser that generates rules which treat the multi-char
wildcard (i.e.
|
protected MatchRule |
createMatchRuleOn(MatchGroup group) |
char |
getAndChar()
Returns the character for AND operations ( DEFAULT = '&' )
|
char |
getEqualsChar()
Returns the character for equals comparisons ( DEFAULT = '=' )
|
char |
getGreaterChar()
Returns the character for greater than comparisons ( DEFAULT = '>' )
|
char |
getGroupEndChar()
Returns the character that ends a logical group ( DEFAULT = ')' )
|
char |
getGroupStartChar()
Returns the character that starts a logical group ( DEFAULT = '(' )
|
boolean |
getIgnoreCaseInNames()
Returns true, if the parser produces MatchRules that treat
attribute names case-insensitive.
|
boolean |
getIgnoreCaseInValues()
Returns true, if the parser produces MatchRules that are case-insensitive
when comparing values.
|
char |
getLessChar()
Returns the character for less than comparisons ( DEFAULT = '<' )
|
boolean |
getMultiCharWildcardMatchesEmptyString()
Returns true, if this parser creates match rules that allow empty strings
at the position of the multi character wildcard ('*').
|
char |
getNotChar()
Returns the character for NOT operations ( DEFAULT = '!' )
|
char |
getOrChar()
Returns the character for OR operations ( DEFAULT = '|' )
|
protected MatchRuleChars |
getRuleChars() |
protected java.lang.String |
getSpecialNameCharacters()
Returns the special character allowed in attribute names
|
char |
getValueDelimiterChar()
Returns the character that is used to enclose a value ( DEFAULT = '\'' )
|
char |
getValueEndChar()
Returns the character ends a list of values ( DEFAULT = '}' )
|
char |
getValueSeparatorChar()
Returns the character for separation of values ( DEFAULT = ',' )
|
char |
getValueStartChar()
Returns the character that starts a list of values ( DEFAULT = '{' )
|
protected boolean |
isGroupEnd(char ch) |
protected boolean |
isOperator(char ch) |
protected boolean |
isPartOfValue(char ch)
Returns true if the given character can be part of a value.
|
protected boolean |
isValidCompareOperator(char ch) |
protected boolean |
isValidNameCharacter(char ch) |
protected boolean |
nextIsGroupElement() |
MatchRule |
parse(java.lang.String rule)
Parse the given rule string to a MatchRule object that can
be used to check attributes in a Map, if they match the rule.
|
protected MatchAttribute |
parseAttribute() |
protected MatchGroup |
parseGroup() |
static MatchRule |
parseRule(java.lang.String rule)
Parse the given rule string to a MatchRule object that can
be used to check attributes in a Map, if they match the rule.
|
protected MatchGroup |
parseToGroup(java.lang.String rule)
Parse the given rule string to a MatchGroup which can be used to
create a MatchRule.
|
MatchRule |
parseTypedRule(java.lang.String rule,
java.util.Map<java.lang.String,java.lang.Class<?>> datatypes)
Parse the given rule string to a MatchRule and apply the given datatypes
to it.
|
protected void |
readAttributeName(MatchAttribute attribute) |
protected char |
readCompareOperator(MatchAttribute attribute) |
protected java.lang.String |
readDelimitedMatchValue() |
protected MatchElement |
readElement() |
protected void |
readElements(MatchGroup group) |
protected StringPattern |
readMatchValue() |
protected void |
readMatchValue(MatchAttribute attribute) |
protected void |
readMatchValues(MatchAttribute attribute) |
protected void |
readOperators(MatchElement element)
Return two operator flags depending on the next characters in the
scanner.
|
protected java.lang.String |
readUndelimitedMatchValue(char character) |
protected java.lang.String |
readUpTo(char exitChar) |
void |
setAndChar(char newValue)
Sets the character for AND operations
|
void |
setEqualsChar(char newValue)
Sets the character that is used to compare if two values are equal
|
void |
setGreaterChar(char newValue)
Sets the character that is used to compare if a value is greater than another
|
void |
setGroupEndChar(char newValue)
Sets the character that ends a group
|
void |
setGroupStartChar(char newValue)
Sets the character that starts a group
|
void |
setIgnoreCaseInNames(boolean newValue)
Sets whether or not the parser produces MatchRules that treat
attribute names case-insensitive.
|
void |
setIgnoreCaseInValues(boolean newValue)
Sets whether or not the parser produces MatchRules that are
case-insensitive when comparing values.
|
void |
setLessChar(char newValue)
Sets the character that is used to compare if a value is less than another
|
void |
setMultiCharWildcardMatchesEmptyString(boolean newValue)
Sets whether or not this parser creates match rules that allow empty
strings at the position of the multi character wildcard ('*').
|
void |
setNotChar(char newValue)
Sets the character for NOT operations
|
void |
setOrChar(char newValue)
Sets the character for OR operations
|
protected void |
setRuleChars(MatchRuleChars newValue) |
void |
setValueEndChar(char newValue)
Sets the character that ends a value list
|
void |
setValueSeparatorChar(char newValue)
Sets the character that separates values in a value list
|
void |
setValueStartChar(char newValue)
Sets the character that starts a value list
|
atEnd, checkExpectedEnd, checkUnexpectedEnd, scanner, scanner, str, throwException
public DefaultMatchRuleParser()
public DefaultMatchRuleParser(MatchRuleChars ruleCharacters)
public static MatchRule parseRule(java.lang.String rule) throws MatchRuleParseException
rule
- The rule in a string compliant to the MatchRule syntaxMatchRuleParseException
- Each syntax error in the given rule causes
this exception with a short description
of what is wrongpublic static DefaultMatchRuleParser create()
public static DefaultMatchRuleParser create(MatchRuleChars chars)
chars
- The charscter set that is used for the rules operators etc.public boolean getIgnoreCaseInNames()
public void setIgnoreCaseInNames(boolean newValue)
public boolean getIgnoreCaseInValues()
public void setIgnoreCaseInValues(boolean newValue)
public boolean getMultiCharWildcardMatchesEmptyString()
The default value is false.
public void setMultiCharWildcardMatchesEmptyString(boolean newValue)
The default value is false.
public MatchRule parse(java.lang.String rule) throws MatchRuleParseException
rule
- The rule in a string compliant to the MatchRule syntaxMatchRuleParseException
- Each syntax error in the given rule causes
this exception with a short description
of what is wrongpublic MatchRule parseTypedRule(java.lang.String rule, java.util.Map<java.lang.String,java.lang.Class<?>> datatypes) throws MatchRuleException
rule
- The rule in a string compliant to the MatchRule syntaxdatatypes
- The attributes and their associated datatypesMatchRuleParseException
- Each syntax error in the given rule causes
this exception with a short description
of what is wrongMatchRuleException
MatchRule.setDatatypes(Map)
public char getAndChar()
public void setAndChar(char newValue)
public char getOrChar()
public void setOrChar(char newValue)
public char getNotChar()
public void setNotChar(char newValue)
public char getValueSeparatorChar()
public void setValueSeparatorChar(char newValue)
public char getValueDelimiterChar()
public char getValueStartChar()
public void setValueStartChar(char newValue)
public char getValueEndChar()
public void setValueEndChar(char newValue)
public char getGroupStartChar()
public void setGroupStartChar(char newValue)
public char getGroupEndChar()
public void setGroupEndChar(char newValue)
public char getGreaterChar()
public void setGreaterChar(char newValue)
public char getLessChar()
public void setLessChar(char newValue)
public char getEqualsChar()
public void setEqualsChar(char newValue)
protected MatchGroup parseToGroup(java.lang.String rule) throws MatchRuleParseException
rule
- The rule in a string compliant to the MatchRule syntaxMatchRuleParseException
- Each syntax error in the given rule causes
this exception with a short description
of what is wrongprotected MatchGroup parseGroup() throws MatchRuleParseException
MatchRuleParseException
protected void readElements(MatchGroup group) throws MatchRuleParseException
MatchRuleParseException
protected MatchElement readElement() throws MatchRuleParseException
MatchRuleParseException
protected boolean nextIsGroupElement()
protected MatchAttribute parseAttribute() throws MatchRuleParseException
MatchRuleParseException
protected void readAttributeName(MatchAttribute attribute) throws MatchRuleParseException
MatchRuleParseException
protected boolean isValidCompareOperator(char ch)
protected char readCompareOperator(MatchAttribute attribute) throws MatchRuleParseException
MatchRuleParseException
protected void readMatchValues(MatchAttribute attribute) throws MatchRuleParseException
MatchRuleParseException
protected void readMatchValue(MatchAttribute attribute) throws MatchRuleParseException
MatchRuleParseException
protected StringPattern readMatchValue() throws MatchRuleParseException
MatchRuleParseException
protected java.lang.String readDelimitedMatchValue() throws MatchRuleParseException
MatchRuleParseException
protected java.lang.String readUndelimitedMatchValue(char character)
protected boolean isPartOfValue(char ch)
protected void readOperators(MatchElement element)
protected java.lang.String readUpTo(char exitChar) throws MatchRuleParseException
MatchRuleParseException
protected boolean isOperator(char ch)
protected boolean isValidNameCharacter(char ch)
protected boolean isGroupEnd(char ch)
protected java.lang.String getSpecialNameCharacters()
protected MatchRule createMatchRuleOn(MatchGroup group)
protected MatchRuleChars getRuleChars()
protected void setRuleChars(MatchRuleChars newValue)