| Constructor: |
public StringPattern(String pattern) {
this(pattern, false);
}
Initializes the new instance with the string pattern. The default is
case sensitive checking. Parameters:
pattern - The pattern to check against ( May contain '' and '?'
wildcards )
|
public StringPattern(String pattern,
boolean ignoreCase) {
// -------------------------------------------------------------------------
// =========================================================================
// CONSTRUCTORS
// =========================================================================
this.setPattern(pattern);
this.setIgnoreCase(ignoreCase);
}
Initializes the new instance with the string pattern and the selecteion,
if case should be ignored when comparing characters. Parameters:
pattern - The pattern to check against ( May contain '' and '?'
wildcards )
ignoreCase - Definition, if case sensitive character comparison or
not.
|
public StringPattern(String pattern,
char digitWildcard) {
this(pattern, false, digitWildcard);
}
Initializes the new instance with the string pattern and a digit
wildcard character. The default is case sensitive checking. Parameters:
pattern - The pattern to check against ( May contain '', '?'
wildcards and the digit wildcard )
digitWildcard - A wildcard character that stands as placeholder for
digits
|
public StringPattern(String pattern,
boolean ignoreCase,
char digitWildcard) {
this.setPattern(pattern);
this.setIgnoreCase(ignoreCase);
this.setDigitWildcardChar(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. Parameters:
pattern - The pattern to check against ( May contain '' and '?'
wildcards )
ignoreCase - Definition, if case sensitive character comparison or
not.
digitWildcard - A wildcard character that stands as placeholder for
digits
|
| Method from com.sshtools.daemon.util.StringPattern Detail: |
protected boolean charsAreEqual(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 Character digitWildcard() {
return digitWildcard;
}
|
protected void digitWildcard(Character newValue) {
digitWildcard = newValue;
}
|
protected char digitWildcardChar() {
if (this.hasDigitWildcard()) {
return this.digitWildcard().charValue();
} else {
return '\0";
}
}
|
protected boolean endNotReached(char character) {
return (!endReached(character));
}
|
protected boolean endReached(char character) {
return (character == StringExaminer.END_REACHED);
}
|
public boolean getIgnoreCase() {
return ignoreCase;
}
Returns whether or not the pattern matching ignores upper and lower case |
public String getPattern() {
return pattern;
}
Returns the pattern as string. |
protected char getPatternChar(StringExaminer patternIterator,
char probeCh) {
char patternCh;
patternCh = patternIterator.nextChar();
return ((patternCh == SINGLECHAR_WILDCARD) ? probeCh : patternCh);
}
|
protected boolean hasDigitWildcard() {
return this.digitWildcard() != null;
}
|
public boolean hasWildcard() {
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);
}
Returns true if the pattern contains any '' or '?' wildcard character. |
public static boolean match(String probe,
String pattern) {
StringPattern stringPattern = new StringPattern(pattern, false);
return (stringPattern.matches(probe));
}
Returns true, if the given probe string matches the given pattern.
The character comparison is done case sensitive. |
public static boolean matchIgnoreCase(String probe,
String pattern) {
StringPattern stringPattern = new StringPattern(pattern, true);
return (stringPattern.matches(probe));
}
Returns true, if the given probe string matches the given pattern.
The character comparison is done ignoring upper/lower-case. |
protected boolean matchReverse(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 boolean matches(String probe) {
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)));
}
Tests if a specified string matches the pattern. |
protected StringExaminer newExaminer(String str) {
return new StringExaminer(str, this.getIgnoreCase());
}
|
public void setDigitWildcardChar(char digitWildcard) {
if (digitWildcard < = 0) {
this.digitWildcard(null);
} else {
this.digitWildcard(new Character(digitWildcard));
}
}
Sets the given character as a wildcard character in this pattern to
match only digits ('0'-'9').
|
public void setIgnoreCase(boolean newValue) {
ignoreCase = newValue;
}
Sets whether the pattern matching should ignore case or not |
public void setPattern(String newValue) {
pattern = newValue;
}
Sets the pattern to a new value |
protected boolean skipAfter(StringExaminer examiner,
String matchString) {
// 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;
}
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. |
protected char skipWildcards(StringExaminer iterator) {
char result = '-";
do {
result = iterator.nextChar();
} while ((result == MULTICHAR_WILDCARD) ||
(result == SINGLECHAR_WILDCARD));
return result;
}
Moves the iterator position to the next character that is no wildcard.
Doesn't skip digit wildcards ! |
protected StringUtil strUtil() {
return StringUtil.current();
}
|
public String toString() {
if (this.getPattern() == null) {
return super.toString();
} else {
return this.getPattern();
}
}
Returns the pattern string. |
protected String upToEnd(StringExaminer iterator) {
return iterator.upToEnd();
}
|