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.8.2.2 $
021: */package java.util.regex;
022:
023: /**
024: * Group node over subexpression w/o alternations.
025: * @author Nikolay A. Kuznetsov
026: * @version $Revision: 1.8.2.2 $
027: */
028: class SingleSet extends JointSet {
029:
030: protected AbstractSet kid;
031:
032: public SingleSet(AbstractSet child, FSet fSet) {
033: this .kid = child;
034: this .fSet = fSet;
035: this .groupIndex = fSet.getGroupIndex();
036: }
037:
038: public int matches(int stringIndex, CharSequence testString,
039: MatchResultImpl matchResult) {
040: int start = matchResult.getStart(groupIndex);
041: matchResult.setStart(groupIndex, stringIndex);
042: int shift = kid.matches(stringIndex, testString, matchResult);
043: if (shift >= 0) {
044: return shift;
045: }
046: matchResult.setStart(groupIndex, start);
047: return -1;
048: }
049:
050: public int find(int stringIndex, CharSequence testString,
051: MatchResultImpl matchResult) {
052: int res = kid.find(stringIndex, testString, matchResult);
053: if (res >= 0)
054: matchResult.setStart(groupIndex, res);
055: return res;
056: }
057:
058: public int findBack(int stringIndex, int lastIndex,
059: CharSequence testString, MatchResultImpl matchResult) {
060: int res = kid.findBack(stringIndex, lastIndex, testString,
061: matchResult);
062: if (res >= 0)
063: matchResult.setStart(groupIndex, res);
064: return res;
065: }
066:
067: public boolean first(AbstractSet set) {
068: return kid.first(set);
069: }
070:
071: /**
072: * This method is used for replacement backreferenced
073: * sets.
074: */
075: public JointSet processBackRefReplacement() {
076: BackReferencedSingleSet set = new BackReferencedSingleSet(this );
077:
078: /*
079: * We will store a reference to created BackReferencedSingleSet
080: * in next field. This is needed toprocess replacement
081: * of sets correctly since sometimes we cannot renew all references to
082: * detachable set in the current point of traverse. See
083: * QuantifierSet and AbstractSet processSecondPass() methods for
084: * more details.
085: */
086: next = set;
087: return set;
088: }
089:
090: /**
091: * This method is used for traversing nodes after the
092: * first stage of compilation.
093: */
094: public void processSecondPass() {
095: this .isSecondPassVisited = true;
096:
097: if (fSet != null && !fSet.isSecondPassVisited) {
098:
099: /*
100: * Add here code to do during the pass
101: */
102:
103: /*
104: * End code to do during the pass
105: */
106: fSet.processSecondPass();
107: }
108:
109: if (kid != null && !kid.isSecondPassVisited) {
110:
111: /*
112: * Add here code to do during the pass
113: */
114: JointSet set = kid.processBackRefReplacement();
115:
116: if (set != null) {
117: kid.isSecondPassVisited = true;
118: kid = (AbstractSet) set;
119: }
120:
121: /*
122: * End code to do during the pass
123: */
124:
125: kid.processSecondPass();
126: }
127: }
128: }
|