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.12.2.2 $
021: */package java.util.regex;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025:
026: /**
027: * Represents group, which is alternation of other subexpression.
028: * One should think about "group" in this model as JointSet opening
029: * group and corresponding FSet closing group.
030: */
031: class JointSet extends AbstractSet {
032:
033: protected ArrayList children;
034:
035: protected AbstractSet fSet;
036:
037: protected int groupIndex;
038:
039: protected JointSet() {
040: }
041:
042: public JointSet(ArrayList children, FSet fSet) {
043: this .children = children;
044: this .fSet = fSet;
045: this .groupIndex = fSet.getGroupIndex();
046: }
047:
048: /**
049: * Returns stringIndex+shift, the next position to match
050: */
051: public int matches(int stringIndex, CharSequence testString,
052: MatchResultImpl matchResult) {
053: if (children == null) {
054: return -1;
055: }
056: int start = matchResult.getStart(groupIndex);
057: matchResult.setStart(groupIndex, stringIndex);
058: int size = children.size();
059: for (int i = 0; i < size; i++) {
060: AbstractSet e = (AbstractSet) children.get(i);
061: int shift = e.matches(stringIndex, testString, matchResult);
062: if (shift >= 0) {
063: return shift;
064: }
065: }
066: matchResult.setStart(groupIndex, start);
067: return -1;
068: }
069:
070: public void setNext(AbstractSet next) {
071: fSet.setNext(next);
072: }
073:
074: public AbstractSet getNext() {
075: return fSet.getNext();
076: }
077:
078: protected String getName() {
079: return "JointSet"; //$NON-NLS-1$
080: }
081:
082: public int getGroup() {
083: return groupIndex;
084: }
085:
086: public boolean first(AbstractSet set) {
087: if (children != null) {
088: for (Iterator i = children.iterator(); i.hasNext();) {
089: if (((AbstractSet) i.next()).first(set)) {
090: return true;
091: }
092: }
093: }
094:
095: return false;
096: }
097:
098: public boolean hasConsumed(MatchResultImpl matchResult) {
099: return !(matchResult.getEnd(groupIndex) >= 0 && matchResult
100: .getStart(groupIndex) == matchResult.getEnd(groupIndex));
101: }
102:
103: /**
104: * This method is used for traversing nodes after the
105: * first stage of compilation.
106: */
107: public void processSecondPass() {
108: this .isSecondPassVisited = true;
109:
110: if (fSet != null && !fSet.isSecondPassVisited) {
111:
112: /*
113: * Add here code to do during the pass
114: */
115:
116: /*
117: * End code to do during the pass
118: */
119: fSet.processSecondPass();
120: }
121:
122: if (children != null) {
123: int childrenSize = children.size();
124:
125: for (int i = 0; i < childrenSize; i++) {
126: AbstractSet child = (AbstractSet) children.get(i);
127:
128: /*
129: * Add here code to do during the pass
130: */
131:
132: JointSet set = child.processBackRefReplacement();
133:
134: if (set != null) {
135: child.isSecondPassVisited = true;
136: children.remove(i);
137: children.add(i, set);
138: child = (AbstractSet) set;
139: }
140:
141: /*
142: * End code to do during the pass
143: */
144: if (!child.isSecondPassVisited) {
145: child.processSecondPass();
146: }
147: }
148: }
149:
150: if (next != null) {
151: super.processSecondPass();
152: }
153: }
154: }
|