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: import net.sourceforge.chaperon.model.Violations;
012:
013: import java.io.Serializable;
014:
015: /**
016: * This class describes an abstract pattern element.
017: *
018: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
019: * @version CVS $Id: Pattern.java,v 1.10 2004/01/08 11:30:52 benedikta Exp $
020: */
021: public abstract class Pattern implements Serializable, Cloneable {
022: private String location = null;
023: private static int internalPatternCount = 0;
024: public final int index = internalPatternCount++;
025: private Definition definition = null;
026: private PatternSet ancestors = new PatternSet();
027: private PatternSet successors = new PatternSet();
028: private PatternSet ascendingAncestors = new PatternSet();
029: private PatternSet ascendingSuccessors = new PatternSet();
030: private PatternSet descendingAncestors = new PatternSet();
031: private PatternSet descendingSuccessors = new PatternSet();
032:
033: public abstract boolean isNullable();
034:
035: public abstract PatternSet getFirstSet();
036:
037: public abstract PatternSet getLastSet();
038:
039: public abstract boolean contains(char c);
040:
041: public abstract char[] getLimits();
042:
043: public abstract boolean contains(char minimum, char maximum);
044:
045: public abstract String getSymbol();
046:
047: public PatternSet getAllPattern() {
048: PatternSet set = new PatternSet();
049: set.addPattern(this );
050: return set;
051: }
052:
053: public void setDefinition(Definition definition) {
054: this .definition = definition;
055: }
056:
057: public Definition getDefinition() {
058: return definition;
059: }
060:
061: public PatternIterator getAncestors() {
062: return ancestors.getPattern();
063: }
064:
065: public boolean hasAncestor(Pattern pattern) {
066: for (PatternIterator ancestor = ancestors.getPattern(); ancestor
067: .hasNext();)
068: if (ancestor.next() == pattern)
069: return true;
070:
071: return false;
072: }
073:
074: public boolean addSuccessor(Pattern pattern) {
075: return successors.addPattern(pattern)
076: | pattern.ancestors.addPattern(this );
077: }
078:
079: public PatternIterator getSuccessors() {
080: return successors.getPattern();
081: }
082:
083: public boolean hasSuccessor(Pattern pattern) {
084: for (PatternIterator successor = successors.getPattern(); successor
085: .hasNext();)
086: if (successor.next() == pattern)
087: return true;
088:
089: return false;
090: }
091:
092: public PatternIterator getAscendingAncestors() {
093: return ascendingAncestors.getPattern();
094: }
095:
096: public boolean hasAscendingAncestor(Pattern pattern) {
097: for (PatternIterator ancestor = ascendingAncestors.getPattern(); ancestor
098: .hasNext();)
099: if (ancestor.next() == pattern)
100: return true;
101:
102: return false;
103: }
104:
105: public boolean addAscendingSuccessor(Pattern pattern) {
106: return ascendingSuccessors.addPattern(pattern)
107: | pattern.ascendingAncestors.addPattern(this );
108: }
109:
110: public PatternIterator getAscendingSuccessors() {
111: return ascendingSuccessors.getPattern();
112: }
113:
114: public boolean hasAscendingSuccessor(Pattern pattern) {
115: for (PatternIterator successor = ascendingSuccessors
116: .getPattern(); successor.hasNext();)
117: if (successor.next() == pattern)
118: return true;
119:
120: return false;
121: }
122:
123: public PatternIterator getDescendingAncestors() {
124: return descendingAncestors.getPattern();
125: }
126:
127: public boolean hasDescendingAncestor(Pattern pattern) {
128: for (PatternIterator ancestor = descendingAncestors
129: .getPattern(); ancestor.hasNext();)
130: if (ancestor.next() == pattern)
131: return true;
132:
133: return false;
134: }
135:
136: public boolean addDescendingSuccessor(Pattern pattern) {
137: return descendingSuccessors.addPattern(pattern)
138: | pattern.descendingAncestors.addPattern(this );
139: }
140:
141: public PatternIterator getDescendingSuccessors() {
142: return descendingSuccessors.getPattern();
143: }
144:
145: public boolean hasDescendingSuccessor(Pattern pattern) {
146: for (PatternIterator successor = descendingSuccessors
147: .getPattern(); successor.hasNext();)
148: if (successor.next() == pattern)
149: return true;
150:
151: return false;
152: }
153:
154: public void update() {
155: ancestors.clear();
156: successors.clear();
157: ascendingAncestors.clear();
158: ascendingSuccessors.clear();
159: descendingAncestors.clear();
160: descendingSuccessors.clear();
161: }
162:
163: /**
164: * Create a clone this pattern.
165: *
166: * @return Clone of this pattern.
167: *
168: * @throws CloneNotSupportedException If an exception occurs during the cloning.
169: */
170: public abstract Object clone() throws CloneNotSupportedException;
171:
172: /**
173: * Set the location from the input source.
174: *
175: * @param location Location in the input source.
176: */
177: public void setLocation(String location) {
178: this .location = location;
179: }
180:
181: /**
182: * Returns the location from the input source.
183: *
184: * @return Location in the input source.
185: */
186: public String getLocation() {
187: return location;
188: }
189:
190: /**
191: * Validates this pattern.
192: *
193: * @return Return a list of violations, if this pattern isn't valid.
194: */
195: public abstract Violations validate();
196:
197: public String toString(PatternSet previous, PatternSet next) {
198: if ((previous != null) && (previous.contains(this ))) {
199: if ((next != null) && (next.contains(this )))
200: return "." + toString() + ".";
201: else
202: return toString() + ".";
203: } else if ((next != null) && (next.contains(this )))
204: return "." + toString();
205:
206: return toString();
207: }
208: }
|