KickJava   Java API By Example, From Geeks To Geeks.

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


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 represents a concatenation of pattern.
15  *
16  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
17  * @version CVS $Id: Concatenation.java,v 1.3 2003/12/09 19:55:52 benedikta Exp $
18  */

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

24   public Concatenation() {}
25
26   /**
27    * Return a string representation of this pattern
28    *
29    * @return String representation of the pattern.
30    */

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

67   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
68   {
69     Concatenation clone = new Concatenation();
70
71     clone.setMinOccurs(getMinOccurs());
72     clone.setMaxOccurs(getMaxOccurs());
73
74     for (int i = 0; i<getPatternCount(); i++)
75       clone.addPattern((Pattern)getPattern(i).clone());
76
77     return clone;
78   }
79
80   /**
81    * Validates this pattern.
82    *
83    * @return Return a list of violations, if this pattern isn't valid.
84    */

85   public Violations validate()
86   {
87     Violations violations = new Violations();
88
89     if (getPatternCount()==0)
90       violations.addViolation("Concatenation doesn't contain any elements", getLocation());
91
92     for (int i = 0; i<getPatternCount(); i++)
93       violations.addViolations(getPattern(i).validate());
94
95     return violations;
96   }
97 }
98
Popular Tags