KickJava   Java API By Example, From Geeks To Geeks.

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


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 special marked concatenation of pattern elements. This group is used get
15  * parts of the matched pattern.
16  *
17  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
18  * @version CVS $Id: PatternGroup.java,v 1.3 2003/12/09 19:55:53 benedikta Exp $
19  */

20 public class PatternGroup extends PatternList
21 {
22   /**
23    * Creates a pattern for a group.
24    */

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

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

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

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