|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.pf.text.BaseMatchRuleParser
org.pf.text.DefaultMatchRuleParser
public class DefaultMatchRuleParser
This parser translates the match-rule syntax to a MatchRule object.
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 Summary | |
|---|---|
DefaultMatchRuleParser()
Initialize the new instance with default values. |
|
DefaultMatchRuleParser(MatchRuleChars ruleCharacters)
Initialize the new instance with a set of rule characters. |
|
| Method Summary | |
|---|---|
static DefaultMatchRuleParser |
create()
Returns a new parser that generates rules which treat the multi-char wildcard (i.e. '*') in a way that it matches empty strings. |
static DefaultMatchRuleParser |
create(MatchRuleChars chars)
Returns a new parser that generates rules which treat the multi-char wildcard (i.e. '*') in a way that it matches empty strings. |
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 = '|' ) |
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 = '{' ) |
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. |
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. |
MatchRule |
parseTypedRule(java.lang.String rule,
java.util.Map datatypes)
Parse the given rule string to a MatchRule and apply the given datatypes to it. |
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 |
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 |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public DefaultMatchRuleParser()
public DefaultMatchRuleParser(MatchRuleChars ruleCharacters)
| Method Detail |
|---|
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 static MatchRule parseRule(java.lang.String rule)
throws MatchRuleParseException
rule - The rule in a string compliant to the MatchRule syntax
MatchRuleParseException - 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 MatchRule parse(java.lang.String rule)
throws MatchRuleParseException
rule - The rule in a string compliant to the MatchRule syntax
MatchRuleParseException - Each syntax error in the given rule causes
this exception with a short description
of what is wrong
public MatchRule parseTypedRule(java.lang.String rule,
java.util.Map datatypes)
throws MatchRuleException
rule - The rule in a string compliant to the MatchRule syntaxdatatypes - The attributes and their associated datatypes
MatchRuleParseException - Each syntax error in the given rule causes
this exception with a short description
of what is wrong
MatchRuleExceptionMatchRule.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)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||