FileDocCategorySizeDatePackage
StringPattern.javaAPI DocAzureus 3.0.3.416257Fri Mar 12 11:00:20 GMT 2004org.pf.text

StringPattern

public class StringPattern extends Object implements Serializable
This class provides services for checking strings against string-patterns. Currently it supports the wildcards
'*' for any number of any character and
'?' for any one character. The API is very simple:

There are only the two class methods match() and matchIgnoreCase().
Example:
StringPattern.match( 'Hello World", "H* W*" ) ; --> evaluates to true
StringPattern.matchIgnoreCase( 'StringPattern", "str???pat*" ) ; --> evaluates to true
author
Manfred Duchrow
version
1.7

Fields Summary
protected static final String
MULTI_WILDCARD
protected static final char
MULTICHAR_WILDCARD
protected static final char
SINGLECHAR_WILDCARD
private boolean
ignoreCase
private String
pattern
private Character
digitWildcard
Constructors Summary
public StringPattern(String pattern)
Initializes the new instance with the string pattern. The default is case sensitive checking.

param
pattern The pattern to check against ( May contain '*' and '?' wildcards )

  	this( pattern, false) ;
  
public StringPattern(String pattern, char digitWildcard)
Initializes the new instance with the string pattern and a digit wildcard character. The default is case sensitive checking.

param
pattern The pattern to check against ( May contain '*', '?' wildcards and the digit wildcard )
param
digitWildcard A wildcard character that stands as placeholder for digits

  	this( pattern, false, digitWildcard ) ;
  
public StringPattern(String pattern, boolean ignoreCase, char digitWildcard)
Initializes the new instance with the string pattern and the selecteion, if case should be ignored when comparing characters plus a wildcard character for digits.

param
pattern The pattern to check against ( May contain '*' and '?' wildcards )
param
ignoreCase Definition, if case sensitive character comparison or not.
param
digitWildcard A wildcard character that stands as placeholder for digits

  	this.setPattern( pattern ) ;
  	this.setIgnoreCase( ignoreCase ) ;  	
  	this.setDigitWildcardChar( digitWildcard ) ;
  
public StringPattern(String pattern, boolean ignoreCase)
Initializes the new instance with the string pattern and the selecteion, if case should be ignored when comparing characters.

param
pattern The pattern to check against ( May contain '*' and '?' wildcards )
param
ignoreCase Definition, if case sensitive character comparison or not.

  	this.setPattern( pattern ) ;
  	this.setIgnoreCase( ignoreCase ) ;  	
  
Methods Summary
protected booleancharsAreEqual(char probeChar, char patternChar)

		if ( this.hasDigitWildcard() )
		{
			if ( patternChar == this.digitWildcardChar() )
			{
				return Character.isDigit( probeChar ) ; 
			}
		}
		
		if ( this.getIgnoreCase() )
		{
			return ( Character.toUpperCase(probeChar) == Character.toUpperCase( patternChar ) ) ;
		}
		else
		{
			return ( probeChar == patternChar ) ;
		}
	
protected java.lang.CharacterdigitWildcard()

      return digitWildcard ; 
protected voiddigitWildcard(java.lang.Character newValue)

 digitWildcard = newValue ; 
protected chardigitWildcardChar()

		if ( this.hasDigitWildcard() )
			return this.digitWildcard().charValue() ;
		else
			return '\0" ;
	
protected booleanendNotReached(char character)

		return ( ! endReached( character ) ) ;
	
protected booleanendReached(char character)

		return ( character == StringExaminer.END_REACHED ) ;
	
public booleangetIgnoreCase()
Returns whether or not the pattern matching ignores upper and lower case

                 
      return ignoreCase ; 
public java.lang.StringgetPattern()
Returns the pattern as string.

          
      return pattern ; 
protected chargetPatternChar(StringExaminer patternIterator, char probeCh)

		char patternCh ;
	
		patternCh = patternIterator.nextChar() ;	
			
		return ( ( patternCh == SINGLECHAR_WILDCARD ) ? probeCh : patternCh ) ;
	
protected booleanhasDigitWildcard()

		return this.digitWildcard() != null ;
	
public booleanhasWildcard()
Returns true if the pattern contains any '*' or '?' wildcard character.

		if ( this.getPattern() == null )
			return false ;

		if ( this.hasDigitWildcard() )
		{
			if ( this.getPattern().indexOf( this.digitWildcardChar() ) >= 0 )
				return true ;
		}
	
		return 	( this.getPattern().indexOf( MULTI_WILDCARD ) >= 0 ) ||
						( this.getPattern().indexOf( SINGLECHAR_WILDCARD ) >= 0 ) ;		
	
public static booleanmatch(java.lang.String probe, java.lang.String pattern)
Returns true, if the given probe string matches the given pattern.
The character comparison is done case sensitive.

param
probe The string to check against the pattern.
param
pattern The patter, that probably contains wildcards ( '*' or '?' )

		StringPattern stringPattern = new StringPattern( pattern, false ) ;
		return ( stringPattern.matches( probe ) ) ;
	
public static booleanmatchIgnoreCase(java.lang.String probe, java.lang.String pattern)
Returns true, if the given probe string matches the given pattern.
The character comparison is done ignoring upper/lower-case.

param
probe The string to check against the pattern.
param
pattern The patter, that probably contains wildcards ( '*' or '?' )

		StringPattern stringPattern = new StringPattern( pattern, true ) ;
		return ( stringPattern.matches( probe ) ) ;
	
protected booleanmatchReverse(java.lang.String pattern, StringExaminer probeIterator)

		String newPattern ;
		String newProbe ;
		StringPattern newMatcher ;
		
		newPattern = MULTI_WILDCARD + pattern ;
		newProbe = this.upToEnd( probeIterator ) ;
		newPattern = this.strUtil().reverse( newPattern ) ;
		newProbe = this.strUtil().reverse( newProbe ) ;
		newMatcher = new StringPattern( newPattern, this.getIgnoreCase() ) ;
		if ( this.hasDigitWildcard() )
			newMatcher.setDigitWildcardChar( this.digitWildcardChar() ) ;
			
		return newMatcher.matches( newProbe ) ;		
	
public booleanmatches(java.lang.String probe)
Tests if a specified string matches the pattern.

param
probe The string to compare to the pattern
return
true if and only if the probe matches the pattern, false otherwise.

		StringExaminer patternIterator	= null ;
		StringExaminer probeIterator		= null ;
		char patternCh	= '-" ;
		char probeCh	= '-" ;
		String newPattern = null ;
		String subPattern = null ;
		int charIndex = 0 ;
				
		if ( probe == null ) return false ;
		if ( probe.length() == 0 ) return false ;
		
		patternIterator = this.newExaminer( this.getPattern() ) ;
		probeIterator = this.newExaminer( probe ) ;
		
		probeCh = probeIterator.nextChar() ;
		patternCh = this.getPatternChar( patternIterator, probeCh ) ;
		
		while ( ( this.endNotReached( patternCh ) ) &&
					  ( this.endNotReached( probeCh ) ) )
		{
						
			if ( patternCh == MULTICHAR_WILDCARD ) 
			{
				patternCh = this.skipWildcards( patternIterator ) ;
				if ( this.endReached( patternCh ) )
				{
					return true ; // No more characters after multi wildcard - So everything matches
				}
				else
				{
					patternIterator.skip(-1) ;
					newPattern = this.upToEnd( patternIterator ) ;
					charIndex = newPattern.indexOf( MULTICHAR_WILDCARD ) ;
					if ( charIndex >= 0 )  
					{
						subPattern = newPattern.substring( 0, charIndex ) ;
						
						if ( this.skipAfter( probeIterator, subPattern ) )
						{
							patternIterator = this.newExaminer( newPattern.substring( charIndex ) ) ;
							patternCh = probeCh ;
						}
						else
						{
							return false ;
						}
					}
					else
					{
						probeIterator.skip(-1) ;
						return this.matchReverse( newPattern, probeIterator ) ;
					}
				}
			}
				
			if ( this.charsAreEqual( probeCh, patternCh ) )
			{
				if ( this.endNotReached(patternCh) )
				{
					probeCh = probeIterator.nextChar() ;
					patternCh = this.getPatternChar( patternIterator, probeCh ) ;
				}
			}
			else
			{
        if ( patternCh != MULTICHAR_WILDCARD )
          return false ;   // character is not matching - return immediately
			}
		} // while() 
		
		return ( ( this.endReached( patternCh ) ) && ( this.endReached( probeCh ) ) ) ;
	
protected StringExaminernewExaminer(java.lang.String str)

		return new StringExaminer( str, this.getIgnoreCase() ) ;
	
public voidsetDigitWildcardChar(char digitWildcard)
Sets the given character as a wildcard character in this pattern to match only digits ('0'-'9').

param
digitWildcard The placeholder character for digits

		if ( digitWildcard <= 0 )
		{
			this.digitWildcard( null ) ;
		}
		else
		{
			this.digitWildcard( new Character( digitWildcard ) ) ;
		}
	
public voidsetIgnoreCase(boolean newValue)
Sets whether the pattern matching should ignore case or not

 ignoreCase = newValue ; 
public voidsetPattern(java.lang.String newValue)
Sets the pattern to a new value

 pattern = newValue ; 
protected booleanskipAfter(StringExaminer examiner, java.lang.String matchString)
Increments the given iterator up to the last character that matched the character sequence in the given matchString. Returns true, if the matchString was found, otherwise false.

param
matchString The string to be found (must not contain *)

		// Do not use the method of StringExaminer anymore, because digit wildcard
		// support is in the charsAreEqual() method which is unknown to the examiner.
		// return examiner.skipAfter( matchString ) ;
		
		char ch			= '-" ;
		char matchChar = ' " ;
		boolean found = false ;
		int index = 0 ;
		
		if ( ( matchString == null ) || ( matchString.length() == 0 ) )
			return false ;
		
		ch = examiner.nextChar() ;
		while ( ( examiner.endNotReached( ch ) ) && ( ! found ) )  
		{
			matchChar = matchString.charAt( index ) ;
			if ( this.charsAreEqual( ch, matchChar ) )
			{
				index++ ;
				if ( index >= matchString.length() ) // whole matchString checked ?
				{
					found = true ;
				}
				else
				{
					ch = examiner.nextChar() ;
				}
			}
			else
			{
				if ( index == 0 )
				{
					ch = examiner.nextChar() ;
				}
				else
				{
					index = 0 ;	
				}
			}
		}
		return found ;
	
protected charskipWildcards(StringExaminer iterator)
Moves the iterator position to the next character that is no wildcard. Doesn't skip digit wildcards !

		char result			= '-" ;
		
		do
		{
			result = iterator.nextChar() ;
		}
		while ( ( result == MULTICHAR_WILDCARD ) || ( result == SINGLECHAR_WILDCARD ) ) ;
		return result ;
	
protected StringUtilstrUtil()

		return StringUtil.current() ;
	
public java.lang.StringtoString()
Returns the pattern string.

see
java.lang.Object#toString()

		if ( this.getPattern() == null )
			return super.toString() ;
		else
			return this.getPattern() ;
	
protected java.lang.StringupToEnd(StringExaminer iterator)

		return iterator.upToEnd() ;