KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > extended > ZeroOrMore


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 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 {
19   public ZeroOrMore() {}
20
21   public boolean isNullable()
22   {
23     return true;
24   }
25
26   public void update()
27   {
28     super.update();
29
30     PatternSet firstSet = getFirstSet();
31     for (PatternIterator i = getLastSet().getPattern(); i.hasNext();)
32     {
33       Pattern lastPattern = i.next();
34       for (PatternIterator j = firstSet.getPattern(); j.hasNext();)
35       {
36         Pattern firstPattern = j.next();
37         lastPattern.addSuccessor(firstPattern);
38       }
39     }
40   }
41
42   /**
43    * Return a string representation of this pattern
44    *
45    * @return String representation of the pattern.
46    */

47   public String JavaDoc toString()
48   {
49     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
50
51     if (getPatternCount()>0)
52     {
53       buffer.append(super.toString());
54       buffer.append("*");
55     }
56
57     return buffer.toString();
58   }
59
60   public String JavaDoc toString(PatternSet previous, PatternSet next)
61   {
62     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
63
64     if (getPatternCount()>0)
65     {
66       buffer.append(super.toString(previous, next));
67       buffer.append("*");
68     }
69
70     return buffer.toString();
71   }
72
73   /**
74    * Create a clone this pattern.
75    *
76    * @return Clone of this pattern.
77    *
78    * @throws CloneNotSupportedException If an exception occurs during the cloning.
79    */

80   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
81   {
82     ZeroOrMore clone = new ZeroOrMore();
83
84     for (int i = 0; i<getPatternCount(); i++)
85       clone.addPattern((Pattern)getPattern(i).clone());
86
87     return clone;
88   }
89
90   /**
91    * Validates this pattern.
92    *
93    * @return Return a list of violations, if this pattern isn't valid.
94    */

95   public Violations validate()
96   {
97     Violations violations = new Violations();
98
99     /*if (pattern==null)
100       violations.addViolation("Multiplier contains no pattern",
101                               getLocation());*/

102     for (int i = 0; i<getPatternCount(); i++)
103       violations.addViolations(getPattern(i).validate());
104
105     return violations;
106   }
107 }
108
Popular Tags