01: /*
02: * Copyright (C) Chaperon. All rights reserved.
03: * -------------------------------------------------------------------------
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08:
09: package net.sourceforge.chaperon.model.extended;
10:
11: import net.sourceforge.chaperon.model.Violations;
12:
13: /**
14: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
15: * @version CVS $Id: ZeroOrMore.java,v 1.7 2004/01/07 08:28:49 benedikta Exp $
16: */
17: public class ZeroOrMore extends PatternList {
18: public ZeroOrMore() {
19: }
20:
21: public boolean isNullable() {
22: return true;
23: }
24:
25: public void update() {
26: super .update();
27:
28: PatternSet firstSet = getFirstSet();
29: for (PatternIterator i = getLastSet().getPattern(); i.hasNext();) {
30: Pattern lastPattern = i.next();
31: for (PatternIterator j = firstSet.getPattern(); j.hasNext();) {
32: Pattern firstPattern = j.next();
33: lastPattern.addSuccessor(firstPattern);
34: }
35: }
36: }
37:
38: /**
39: * Return a string representation of this pattern
40: *
41: * @return String representation of the pattern.
42: */
43: public String toString() {
44: StringBuffer buffer = new StringBuffer();
45:
46: if (getPatternCount() > 0) {
47: buffer.append(super .toString());
48: buffer.append("*");
49: }
50:
51: return buffer.toString();
52: }
53:
54: public String toString(PatternSet previous, PatternSet next) {
55: StringBuffer buffer = new StringBuffer();
56:
57: if (getPatternCount() > 0) {
58: buffer.append(super .toString(previous, next));
59: buffer.append("*");
60: }
61:
62: return buffer.toString();
63: }
64:
65: /**
66: * Create a clone this pattern.
67: *
68: * @return Clone of this pattern.
69: *
70: * @throws CloneNotSupportedException If an exception occurs during the cloning.
71: */
72: public Object clone() throws CloneNotSupportedException {
73: ZeroOrMore clone = new ZeroOrMore();
74:
75: for (int i = 0; i < getPatternCount(); i++)
76: clone.addPattern((Pattern) getPattern(i).clone());
77:
78: return clone;
79: }
80:
81: /**
82: * Validates this pattern.
83: *
84: * @return Return a list of violations, if this pattern isn't valid.
85: */
86: public Violations validate() {
87: Violations violations = new Violations();
88:
89: /*if (pattern==null)
90: violations.addViolation("Multiplier contains no pattern",
91: getLocation());*/
92: for (int i = 0; i < getPatternCount(); i++)
93: violations.addViolations(getPattern(i).validate());
94:
95: return violations;
96: }
97: }
|