FileDocCategorySizeDatePackage
EndMatcher.javaAPI DocphoneME MR2 API (J2ME)4600Wed May 02 18:00:28 BST 2007com.sun.kvem.midp.pim.formats

EndMatcher

public class EndMatcher extends Object implements LineReader.Matcher
Implementation of LineReader.Matcher that matches a case insensitive line of the form "end\w*:\w*${param}", where ${param} is the argument to EndMatcher's constructor.

Fields Summary
private final char[]
parameter
Original pattern for matching.
private final char[]
parameter2
Inverted case of original pattern string.
Constructors Summary
public EndMatcher(String s)
Constructs an end matcher.

param
s pattern to match

        this.parameter = s.toCharArray();
        // make parameter2 have the opposite case in every character
        // to parameter
        this.parameter2 = new char[parameter.length];
        int delta = 'a" - 'A";
        for (int i = 0; i < parameter.length; i++) {
            char c = parameter[i];
            if (c >= 'A" && c <= 'Z") {
                c += delta;
            } else if (c >= 'a" && c <= 'z") {
                c -= delta;
            }
            parameter2[i] = c;
        }
    
Methods Summary
public booleanmatch(java.lang.StringBuffer sb)
Matches string pattern.

param
sb input buffer for matching
return
true if matches

        int length = sb.length();
        // does the string start with 'end' ?
        int index = -1;
        int stopIndex = length - parameter.length - 3;
        for (int i = 0; i < stopIndex && index == -1; i++) {
            switch (sb.charAt(i)) {
                case ' ":
                case '\t":
                    continue;
                case 'e":
                case 'E":
                    switch (sb.charAt(i + 1)) {
                        case 'n":
                        case 'N":
                            switch (sb.charAt(i + 2)) {
                                case 'd":
                                case 'D":
                                    index = i + 3;
                                    break;
                                default:
                                    return false;
                            }
                            break;
                        default:
                            return false;
                    }
                    break;
                default:
                    return false;
            }
        }
        if (index == -1) {
            return false;
        }
        boolean foundColon = false;
        stopIndex = length - parameter.length + 1;
        while (index < stopIndex) {
            switch (sb.charAt(index)) {
                case ':":
                    if (foundColon) {
                        return false;
                    } else {
                        foundColon = true;
                    }
                case ' ":
                case '\t":
                    break;
                default:
                    if (index != stopIndex - 1) {
                        return false;
                    }
                    char[] cs = new char[parameter.length];
                    sb.getChars(index, index + parameter.length, cs, 0);
                    for (int i = 0; i < cs.length; i++) {
                        char c = cs[i];
                        if (c != parameter[i] && c != parameter2[i]) {
                            return false;
                        }
                    }
                    return true;
            }
            index ++;
        }
        return false;