001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Nikolay A. Kuznetsov
020: * @version $Revision: 1.11.2.2 $
021: */package java.util.regex;
022:
023: /**
024: * Back reference node, i.e. \1-9;
025: *
026: * @author Nikolay A. Kuznetsov
027: * @version $Revision: 1.11.2.2 $
028: */
029: class BackReferenceSet extends CIBackReferenceSet {
030:
031: public BackReferenceSet(int groupIndex, int consCounter) {
032: super (groupIndex, consCounter);
033: }
034:
035: public int matches(int stringIndex, CharSequence testString,
036: MatchResultImpl matchResult) {
037: String group = getString(matchResult);
038: if (group == null
039: || (stringIndex + group.length()) > matchResult
040: .getRightBound())
041: return -1;
042: int shift = testString.toString()
043: .startsWith(group, stringIndex) ? group.length() : -1;
044:
045: if (shift < 0) {
046: return -1;
047: }
048: matchResult.setConsumed(consCounter, shift);
049: return next.matches(stringIndex + shift, testString,
050: matchResult);
051: }
052:
053: public int find(int strIndex, CharSequence testString,
054: MatchResultImpl matchResult) {
055: String group = getString(matchResult);
056: int strLength = matchResult.getLeftBound();
057:
058: if (group == null || (strIndex + group.length()) > strLength)
059: return -1;
060:
061: String testStr = testString.toString();
062:
063: while (strIndex <= strLength) {
064: strIndex = testStr.indexOf(group, strIndex);
065:
066: if (strIndex < 0)
067: return -1;
068: if (next.matches(strIndex + group.length(), testString,
069: matchResult) >= 0) {
070: return strIndex;
071: }
072:
073: strIndex++;
074: }
075:
076: return -1;
077: }
078:
079: public int findBack(int strIndex, int lastIndex,
080: CharSequence testString, MatchResultImpl matchResult) {
081: String group = getString(matchResult);
082:
083: if (group == null)
084: return -1;
085:
086: String testStr = testString.toString();
087:
088: while (lastIndex >= strIndex) {
089: lastIndex = testStr.lastIndexOf(group, lastIndex);
090:
091: if (lastIndex < 0 || lastIndex < strIndex)
092: return -1;
093: if (next.matches(lastIndex + group.length(), testString,
094: matchResult) >= 0) {
095: return lastIndex;
096: }
097:
098: lastIndex--;
099: }
100: return -1;
101: }
102:
103: public boolean first(AbstractSet set) {
104: return true;
105: }
106:
107: public String getName() {
108: return "back reference: " + this .groupIndex; //$NON-NLS-1$
109: }
110: }
|