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: /**
014: * This class describes a alternation of pattern.
015: *
016: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
017: * @version CVS $Id: Choice.java,v 1.4 2004/01/07 08:28:49 benedikta Exp $
018: */
019: public class Choice extends PatternList {
020: /**
021: * Create a pattern for alternation of pattern.
022: */
023: public Choice() {
024: }
025:
026: public boolean isNullable() {
027: boolean nullable = (getPatternCount() == 0);
028: for (int i = 0; i < getPatternCount(); i++)
029: nullable |= getPattern(i).isNullable();
030:
031: return nullable;
032: }
033:
034: public PatternSet getFirstSet() {
035: PatternSet set = new PatternSet();
036: for (int i = 0; i < getPatternCount(); i++)
037: set.addPattern(getPattern(i).getFirstSet());
038:
039: return set;
040: }
041:
042: public PatternSet getLastSet() {
043: PatternSet set = new PatternSet();
044: for (int i = 0; i < getPatternCount(); i++)
045: set.addPattern(getPattern(i).getLastSet());
046:
047: return set;
048: }
049:
050: public void update() {
051: for (PatternIterator pattern = getPattern(); pattern.hasNext();)
052: pattern.next().update();
053: }
054:
055: public String toString() {
056: StringBuffer buffer = new StringBuffer();
057:
058: if (getPatternCount() > 1) {
059: buffer.append("(");
060: for (int i = 0; i < getPatternCount(); i++) {
061: if (i > 0)
062: buffer.append("|");
063:
064: buffer.append(getPattern(i).toString());
065: }
066:
067: buffer.append(")");
068: } else if (getPatternCount() == 1)
069: buffer.append(getPattern(0).toString());
070:
071: return buffer.toString();
072: }
073:
074: public String toString(PatternSet previous, PatternSet next) {
075: StringBuffer buffer = new StringBuffer();
076:
077: if (getPatternCount() > 1) {
078: buffer.append("(");
079: for (int i = 0; i < getPatternCount(); i++) {
080: if (i > 0)
081: buffer.append("|");
082:
083: buffer.append(getPattern(i).toString(previous, next));
084: }
085:
086: buffer.append(")");
087: } else if (getPatternCount() == 1)
088: buffer.append(getPattern(0).toString(previous, next));
089:
090: return buffer.toString();
091: }
092:
093: /**
094: * Create a clone of this pattern.
095: *
096: * @return Clone of this pattern.
097: *
098: * @throws CloneNotSupportedException If an exception occurs during the cloning.
099: */
100: public Object clone() {
101: Choice clone = new Choice();
102:
103: for (int i = 0; i < getPatternCount(); i++)
104: clone.addPattern(getPattern(i));
105:
106: return clone;
107: }
108:
109: /**
110: * Validates this pattern.
111: *
112: * @return Return a list of violations, if this pattern isn't valid.
113: */
114: public Violations validate() {
115: Violations violations = new Violations();
116:
117: if (getPatternCount() == 0)
118: violations.addViolation(
119: "Choice doesn't contain any elements",
120: getLocation());
121:
122: for (int i = 0; i < getPatternCount(); i++)
123: violations.addViolations(getPattern(i).validate());
124:
125: return violations;
126: }
127: }
|