001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.model.extended;
010:
011: /**
012: * This class represents a abstract list of pattern.
013: *
014: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
015: * @version CVS $Id: PatternList.java,v 1.10 2004/01/08 11:30:52 benedikta Exp $
016: */
017: public abstract class PatternList extends Pattern {
018: private PatternListEntry first = null;
019: private PatternListEntry last = null;
020: private int count = 0;
021:
022: /**
023: * Adds a pattern to this list
024: *
025: * @param element Pattern.
026: */
027: public void addPattern(Pattern pattern) {
028: if (first == null)
029: last = first = new PatternListEntry(pattern, null);
030: else {
031: last.next = new PatternListEntry(pattern, last);
032: last = last.next;
033: }
034:
035: count++;
036: }
037:
038: /**
039: * Returns a pattern given by an index.
040: *
041: * @param index Index of the pattern
042: *
043: * @return Pattern
044: */
045: public Pattern getPattern(int index) {
046: int i = 0;
047: for (PatternListEntry entry = first; entry != null; entry = entry.next, i++)
048: if (i == index)
049: return entry.pattern;
050:
051: return null;
052: }
053:
054: public PatternIterator getPattern() {
055: return new PatternListEntryIterator(first);
056: }
057:
058: public Pattern[] getPatternAsArray() {
059: Pattern[] pattern = new Pattern[count];
060: int i = 0;
061: for (PatternListEntry entry = first; entry != null; entry = entry.next, i++)
062: pattern[i] = entry.pattern;
063:
064: return pattern;
065: }
066:
067: /**
068: * Return the count of pattern in this list.
069: *
070: * @return Count of pattern.
071: */
072: public int getPatternCount() {
073: return count;
074: }
075:
076: public boolean isNullable() {
077: boolean nullable = true;
078: for (PatternListEntry entry = first; entry != null; entry = entry.next)
079: nullable &= entry.pattern.isNullable();
080:
081: return nullable;
082: }
083:
084: public PatternSet getFirstSet() {
085: PatternSet set = new PatternSet();
086: for (PatternListEntry entry = first; entry != null; entry = entry.next) {
087: set.addPattern(entry.pattern.getFirstSet());
088: if (!entry.pattern.isNullable())
089: break;
090: }
091:
092: return set;
093: }
094:
095: public PatternSet getLastSet() {
096: PatternSet set = new PatternSet();
097: for (PatternListEntry entry = last; entry != null; entry = entry.previous) {
098: set.addPattern(entry.pattern.getLastSet());
099: if (!entry.pattern.isNullable())
100: break;
101: }
102:
103: return set;
104: }
105:
106: public void update() {
107: for (PatternListEntry entry = first; entry != null; entry = entry.next)
108: entry.pattern.update();
109:
110: for (PatternListEntry entry = first; entry != null; entry = entry.next) {
111: for (PatternListEntry entry2 = entry.next; entry2 != null; entry2 = entry2.next) {
112: for (PatternIterator i = entry.pattern.getLastSet()
113: .getPattern(); i.hasNext();) {
114: Pattern lastPattern = i.next();
115: for (PatternIterator j = entry2.pattern
116: .getFirstSet().getPattern(); j.hasNext();) {
117: Pattern firstPattern = j.next();
118: lastPattern.addSuccessor(firstPattern);
119: }
120: }
121:
122: if (!entry2.pattern.isNullable())
123: break;
124: }
125: }
126: }
127:
128: public PatternSet getAllPattern() {
129: PatternSet set = new PatternSet();
130: set.addPattern(this );
131: for (PatternListEntry entry = first; entry != null; entry = entry.next)
132: set.addPattern(entry.pattern.getAllPattern());
133:
134: return set;
135: }
136:
137: public char[] getLimits() {
138: return new char[0];
139: }
140:
141: public boolean contains(char minimum, char maximum) {
142: return false;
143: }
144:
145: public boolean contains(char c) {
146: return false;
147: }
148:
149: public String getSymbol() {
150: return null;
151: }
152:
153: public String toString() {
154: return toString(null, null);
155: }
156:
157: public String toString(PatternSet previous, PatternSet next) {
158: StringBuffer buffer = new StringBuffer();
159:
160: if ((previous != null) && (previous.contains(this ))) {
161: if ((next != null) && (next.contains(this )))
162: buffer.append("." + toString() + ".");
163: else
164: buffer.append(toString() + ".");
165: } else if ((next != null) && (next.contains(this )))
166: buffer.append("." + toString());
167:
168: if (count > 1) {
169: buffer.append("(");
170: for (PatternListEntry entry = first; entry != null; entry = entry.next) {
171: if (entry != first)
172: buffer.append(" ");
173:
174: buffer.append(entry.pattern.toString(previous, next));
175: }
176:
177: buffer.append(")");
178: } else if (count == 1)
179: buffer.append(first.pattern.toString(previous, next));
180:
181: return buffer.toString();
182: }
183:
184: private class PatternListEntry {
185: public final Pattern pattern;
186: public PatternListEntry previous = null;
187: public PatternListEntry next = null;
188:
189: private PatternListEntry(Pattern pattern,
190: PatternListEntry previous) {
191: this .pattern = pattern;
192: this .previous = previous;
193: }
194: }
195:
196: private class PatternListEntryIterator implements PatternIterator {
197: private PatternListEntry entry;
198:
199: private PatternListEntryIterator(PatternListEntry entry) {
200: this .entry = entry;
201: }
202:
203: public boolean hasNext() {
204: return (entry != null);
205: }
206:
207: public Pattern next() {
208: Pattern pattern = entry.pattern;
209: entry = entry.next;
210: return pattern;
211: }
212: }
213: }
|