KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > pattern > Alternation


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.pattern;
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: Alternation.java,v 1.4 2003/12/09 19:55:52 benedikta Exp $
18  */

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

24   public Alternation() {}
25
26   /**
27    * Create a clone of this pattern.
28    *
29    * @return Clone of this pattern.
30    *
31    * @throws CloneNotSupportedException If an exception occurs during the cloning.
32    */

33   public String JavaDoc toString()
34   {
35     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
36
37     for (int i = 0; i<getPatternCount(); i++)
38     {
39       if (i>0)
40         buffer.append("|");
41
42       buffer.append(getPattern(i).toString());
43     }
44
45     if ((getMinOccurs()==1) && (getMaxOccurs()==1))
46     {
47       // nothing
48
}
49     else if ((getMinOccurs()==0) && (getMaxOccurs()==1))
50       buffer.append("?");
51     else if ((getMinOccurs()==0) && (getMaxOccurs()==Integer.MAX_VALUE))
52       buffer.append("*");
53     else if ((getMinOccurs()==1) && (getMaxOccurs()==Integer.MAX_VALUE))
54       buffer.append("+");
55     else
56     {
57       buffer.append("{");
58       buffer.append(String.valueOf(getMinOccurs()));
59       buffer.append(",");
60       buffer.append(String.valueOf(getMaxOccurs()));
61       buffer.append("}");
62     }
63
64     return buffer.toString();
65   }
66
67   /**
68    * Create a clone of this pattern.
69    *
70    * @return Clone of this pattern.
71    *
72    * @throws CloneNotSupportedException If an exception occurs during the cloning.
73    */

74   public Object JavaDoc clone()
75   {
76     Alternation clone = new Alternation();
77
78     clone.setMinOccurs(getMinOccurs());
79     clone.setMaxOccurs(getMaxOccurs());
80
81     for (int i = 0; i<getPatternCount(); i++)
82       clone.addPattern(getPattern(i));
83
84     return clone;
85   }
86
87   /**
88    * Validates this pattern.
89    *
90    * @return Return a list of violations, if this pattern isn't valid.
91    */

92   public Violations validate()
93   {
94     Violations violations = new Violations();
95
96     if (getPatternCount()==0)
97       violations.addViolation("Alternation doesn't contain any elements", getLocation());
98
99     for (int i = 0; i<getPatternCount(); i++)
100       violations.addViolations(getPattern(i).validate());
101
102     return violations;
103   }
104 }
105
Popular Tags