KickJava   Java API By Example, From Geeks To Geeks.

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


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  * This class describes a alternation of pattern.
15  *
16  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
17  * @version CVS $Id: Choice.java,v 1.4 2004/01/07 08:28:49 benedikta Exp $
18  */

19 public class Choice extends PatternList
20 {
21   /**
22    * Create a pattern for alternation of pattern.
23    */

24   public Choice() {}
25
26   public boolean isNullable()
27   {
28     boolean nullable = (getPatternCount()==0);
29     for (int i = 0; i<getPatternCount(); i++)
30       nullable |= getPattern(i).isNullable();
31
32     return nullable;
33   }
34
35   public PatternSet getFirstSet()
36   {
37     PatternSet set = new PatternSet();
38     for (int i = 0; i<getPatternCount(); i++)
39       set.addPattern(getPattern(i).getFirstSet());
40
41     return set;
42   }
43
44   public PatternSet getLastSet()
45   {
46     PatternSet set = new PatternSet();
47     for (int i = 0; i<getPatternCount(); i++)
48       set.addPattern(getPattern(i).getLastSet());
49
50     return set;
51   }
52
53   public void update()
54   {
55     for (PatternIterator pattern = getPattern(); pattern.hasNext();)
56       pattern.next().update();
57   }
58
59   public String JavaDoc toString()
60   {
61     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
62
63     if (getPatternCount()>1)
64     {
65       buffer.append("(");
66       for (int i = 0; i<getPatternCount(); i++)
67       {
68         if (i>0)
69           buffer.append("|");
70
71         buffer.append(getPattern(i).toString());
72       }
73
74       buffer.append(")");
75     }
76     else if (getPatternCount()==1)
77       buffer.append(getPattern(0).toString());
78
79     return buffer.toString();
80   }
81
82   public String JavaDoc toString(PatternSet previous, PatternSet next)
83   {
84     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
85
86     if (getPatternCount()>1)
87     {
88       buffer.append("(");
89       for (int i = 0; i<getPatternCount(); i++)
90       {
91         if (i>0)
92           buffer.append("|");
93
94         buffer.append(getPattern(i).toString(previous, next));
95       }
96
97       buffer.append(")");
98     }
99     else if (getPatternCount()==1)
100       buffer.append(getPattern(0).toString(previous, next));
101
102     return buffer.toString();
103   }
104
105   /**
106    * Create a clone of this pattern.
107    *
108    * @return Clone of this pattern.
109    *
110    * @throws CloneNotSupportedException If an exception occurs during the cloning.
111    */

112   public Object JavaDoc clone()
113   {
114     Choice clone = new Choice();
115
116     for (int i = 0; i<getPatternCount(); i++)
117       clone.addPattern(getPattern(i));
118
119     return clone;
120   }
121
122   /**
123    * Validates this pattern.
124    *
125    * @return Return a list of violations, if this pattern isn't valid.
126    */

127   public Violations validate()
128   {
129     Violations violations = new Violations();
130
131     if (getPatternCount()==0)
132       violations.addViolation("Choice doesn't contain any elements", getLocation());
133
134     for (int i = 0; i<getPatternCount(); i++)
135       violations.addViolations(getPattern(i).validate());
136
137     return violations;
138   }
139 }
140
Popular Tags