001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.kvem.midp.pim.formats;
028:
029: import com.sun.kvem.midp.pim.LineReader;
030:
031: /**
032: * Implementation of LineReader.Matcher that matches a case insensitive line
033: * of the form "end\w*:\w*${param}", where ${param} is the argument to
034: * EndMatcher's constructor.
035: */
036: public class EndMatcher implements LineReader.Matcher {
037: /** Original pattern for matching. */
038: private final char[] parameter;
039: /** Inverted case of original pattern string. */
040: private final char[] parameter2;
041:
042: /**
043: * Constructs an end matcher.
044: * @param s pattern to match
045: */
046: public EndMatcher(String s) {
047: this .parameter = s.toCharArray();
048: // make parameter2 have the opposite case in every character
049: // to parameter
050: this .parameter2 = new char[parameter.length];
051: int delta = 'a' - 'A';
052: for (int i = 0; i < parameter.length; i++) {
053: char c = parameter[i];
054: if (c >= 'A' && c <= 'Z') {
055: c += delta;
056: } else if (c >= 'a' && c <= 'z') {
057: c -= delta;
058: }
059: parameter2[i] = c;
060: }
061: }
062:
063: /**
064: * Matches string pattern.
065: * @param sb input buffer for matching
066: * @return <code>true</code> if matches
067: */
068: public boolean match(StringBuffer sb) {
069: int length = sb.length();
070: // does the string start with 'end' ?
071: int index = -1;
072: int stopIndex = length - parameter.length - 3;
073: for (int i = 0; i < stopIndex && index == -1; i++) {
074: switch (sb.charAt(i)) {
075: case ' ':
076: case '\t':
077: continue;
078: case 'e':
079: case 'E':
080: switch (sb.charAt(i + 1)) {
081: case 'n':
082: case 'N':
083: switch (sb.charAt(i + 2)) {
084: case 'd':
085: case 'D':
086: index = i + 3;
087: break;
088: default:
089: return false;
090: }
091: break;
092: default:
093: return false;
094: }
095: break;
096: default:
097: return false;
098: }
099: }
100: if (index == -1) {
101: return false;
102: }
103: boolean foundColon = false;
104: stopIndex = length - parameter.length + 1;
105: while (index < stopIndex) {
106: switch (sb.charAt(index)) {
107: case ':':
108: if (foundColon) {
109: return false;
110: } else {
111: foundColon = true;
112: }
113: case ' ':
114: case '\t':
115: break;
116: default:
117: if (index != stopIndex - 1) {
118: return false;
119: }
120: char[] cs = new char[parameter.length];
121: sb.getChars(index, index + parameter.length, cs, 0);
122: for (int i = 0; i < cs.length; i++) {
123: char c = cs[i];
124: if (c != parameter[i] && c != parameter2[i]) {
125: return false;
126: }
127: }
128: return true;
129: }
130: index++;
131: }
132: return false;
133: }
134:
135: }
|