org.pfsw.text
Class MatchRule

java.lang.Object
  extended by org.pfsw.text.MatchRule
All Implemented Interfaces:
java.io.Serializable

public class MatchRule
extends java.lang.Object
implements java.io.Serializable

The purpose of class MatchRule is to provide an easy-to-use component, that checks a dictionary (i.e. Map) of attributes and corresponding values against a defined rule. Such a rule is a string containing logical operations on attributes and their values.
Particularly in the context of LDAP objects and their attributes this component can be very helpful.

Any instance of this class can handle one rule. A rule can be create through one of the different parsers.
(e.g. DefaultMatchRuleParser or LdapFilterParser ) If the constructor with a rule string as parameter is used, it automatically uses the DefaultMatchRuleParser to parse the rule.

Example:

See Also:
Serialized Form

Constructor Summary
MatchRule()
          Initialize the new instance with default values.
MatchRule(boolean ignoreCase)
          Initialize the new instance with default values.
MatchRule(MatchGroup aGroup)
          Initialize the new instance with a root group
MatchRule(java.lang.String rule)
          Initialize the new instance with a rule for later pattern-matching.
MatchRule(java.lang.String rule, MatchRuleChars charSet)
          Initialize the new instance with rule for later pattern-matching and a set of special characters which are the operators and separators on the rule.
 
Method Summary
protected  void appendGroup(MatchGroup group)
           
 void apply(MatchRuleVisitor visitor)
          Iterates through all elements of the rule and calls the appropriate methods of the given visitor.
protected  boolean getIgnoreCase()
           
protected  boolean getIgnoreCaseInNames()
          Returns true, if the parser produces MatchRules that treat attribute names case-insensitive.
protected  boolean getMultiCharWildcardMatchesEmptyString()
          Returns true, if this match rule allows empty strings at the position of the multi character wildcard ('*').
protected  DefaultMatchRuleParser getParser()
           
protected  MatchGroup getRootGroup()
           
 void ignoreCase(boolean ignoreIt)
          Defines whether or not the case of characters in value comparison must be ignored.
 void ignoreCaseInNames(boolean ignoreIt)
          Defines whether or not the case of characters in attribute names must be ignored.
 boolean matches(java.util.Map<java.lang.String,?> dictionary)
          Returns true, if the attributes and their values in the given dictionary comply to the rules of the receiver.
 void mergeAnd(MatchRule rule)
          Append the given rule to the receiver with a logical AND
 void mergeOr(MatchRule rule)
          Append the given rule to the receiver with a logical OR
 void multiCharWildcardMatchesEmptyString(boolean yesOrNo)
          Sets whether or not this match rule allows empty strings at the position of the multi character wildcard ('*').
 void optimize()
          Optimize the rule for best performance.
protected  DefaultMatchRuleParser parser()
           
protected  void parseRule(java.lang.String rule, MatchRuleChars charSet)
           
 void setDatatypes(java.util.Map<java.lang.String,java.lang.Class<?>> datatypes)
          Sets the datatypes specified in the given map.
protected  void setIgnoreCase(boolean newValue)
           
protected  void setParser(DefaultMatchRuleParser newValue)
           
protected  void setRootGroup(MatchGroup newValue)
           
 void setRule(java.lang.String rule)
          Set (and parse) the given rule and keep it for later checks with matches().
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MatchRule

public MatchRule()
Initialize the new instance with default values. The rule can be set later with the method setRule().


MatchRule

public MatchRule(boolean ignoreCase)
Initialize the new instance with default values. The rule can be set late with the method setRule(). Here it can be predefined if case sensitivity is switched off or not


MatchRule

public MatchRule(java.lang.String rule)
          throws MatchRuleParseException
Initialize the new instance with a rule for later pattern-matching.

Throws:
MatchRuleParseException

MatchRule

public MatchRule(MatchGroup aGroup)
Initialize the new instance with a root group


MatchRule

public MatchRule(java.lang.String rule,
                 MatchRuleChars charSet)
          throws MatchRuleParseException
Initialize the new instance with rule for later pattern-matching and a set of special characters which are the operators and separators on the rule.

Throws:
MatchRuleParseException
Method Detail

setRule

public void setRule(java.lang.String rule)
             throws MatchRuleParseException
Set (and parse) the given rule and keep it for later checks with matches(). This replaces any other rule given to this instance before.

Throws:
MatchRuleParseException

ignoreCase

public void ignoreCase(boolean ignoreIt)
Defines whether or not the case of characters in value comparison must be ignored.


ignoreCaseInNames

public void ignoreCaseInNames(boolean ignoreIt)
Defines whether or not the case of characters in attribute names must be ignored.


multiCharWildcardMatchesEmptyString

public void multiCharWildcardMatchesEmptyString(boolean yesOrNo)
Sets whether or not this match rule allows empty strings at the position of the multi character wildcard ('*').

The default value is false.


mergeAnd

public void mergeAnd(MatchRule rule)
Append the given rule to the receiver with a logical AND


mergeOr

public void mergeOr(MatchRule rule)
Append the given rule to the receiver with a logical OR


matches

public boolean matches(java.util.Map<java.lang.String,?> dictionary)
Returns true, if the attributes and their values in the given dictionary comply to the rules of the receiver.

Parameters:
dictionary - The attribute-value pairs that have to be checked against the rules

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

apply

public void apply(MatchRuleVisitor visitor)
Iterates through all elements of the rule and calls the appropriate methods of the given visitor.
As the name indicates, this is implemented following the Visitor design pattern from the book Design Patterns from Erich Gamma et. al.

Parameters:
visitor - the object that receives all callbacks for the visited elements

optimize

public void optimize()
Optimize the rule for best performance. This method might change the internal structure of the rule.


setDatatypes

public void setDatatypes(java.util.Map<java.lang.String,java.lang.Class<?>> datatypes)
                  throws MatchRuleException
Sets the datatypes specified in the given map. The keys are the attribute names and the values are the corresponding types.

Currently supported datatypes are:

Parameters:
datatypes - The attributes and their corresponding types
Throws:
MatchRuleException - if a value of the rule cannot be converted to the specified datatype

parser

protected DefaultMatchRuleParser parser()

parseRule

protected void parseRule(java.lang.String rule,
                         MatchRuleChars charSet)
                  throws MatchRuleParseException
Throws:
MatchRuleParseException

appendGroup

protected void appendGroup(MatchGroup group)

getRootGroup

protected MatchGroup getRootGroup()

setRootGroup

protected void setRootGroup(MatchGroup newValue)

getIgnoreCase

protected boolean getIgnoreCase()

setIgnoreCase

protected void setIgnoreCase(boolean newValue)

getIgnoreCaseInNames

protected boolean getIgnoreCaseInNames()
Returns true, if the parser produces MatchRules that treat attribute names case-insensitive.


getParser

protected DefaultMatchRuleParser getParser()

setParser

protected void setParser(DefaultMatchRuleParser newValue)

getMultiCharWildcardMatchesEmptyString

protected boolean getMultiCharWildcardMatchesEmptyString()
Returns true, if this match rule allows empty strings at the position of the multi character wildcard ('*').

The default value is false.