KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
14
15 /**
16  * This class describes a pattern for a character class, which means the a character matches
17  * against a element of this class.
18  *
19  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
20  * @version CVS $Id: CharacterClass.java,v 1.7 2003/12/10 16:34:38 benedikta Exp $
21  */

22 public class CharacterClass extends Pattern
23 {
24   private Vector JavaDoc childs = new Vector JavaDoc();
25   private boolean exclusive = false;
26
27   /**
28    * Creates a pattern for a character class.
29    */

30   public CharacterClass() {}
31
32   /**
33    * Add a character class element
34    *
35    * @param element Element, which should be added
36    */

37   public void addCharacterClassElement(CharacterClassElement element)
38   {
39     if (element!=null)
40       childs.addElement(element);
41   }
42
43   /**
44    * Returns a character class element
45    *
46    * @param index Index of the character class element
47    *
48    * @return Character class element
49    */

50   public CharacterClassElement getCharacterClassElement(int index)
51   {
52     return (CharacterClassElement)childs.elementAt(index);
53   }
54
55   /**
56    * Returns the count of character class elements
57    *
58    * @return Count of character class elements
59    */

60   public int getCharacterClassElementCount()
61   {
62     return childs.size();
63   }
64
65   /**
66    * If the comparing character must match to the elements, or should not match to the elements.
67    *
68    * @param exclusive If the character class should be exclusive.
69    */

70   public void setExclusive(boolean exclusive)
71   {
72     this.exclusive = exclusive;
73   }
74
75   /**
76    * If this character class is exclusive
77    *
78    * @return If the character class should be exclusive.
79    */

80   public boolean isExclusive()
81   {
82     return exclusive;
83   }
84
85   /**
86    * Create a clone of this pattern.
87    *
88    * @return Clone of this pattern.
89    *
90    * @throws CloneNotSupportedException If an exception occurs during the cloning.
91    */

92   public String JavaDoc toString()
93   {
94     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
95
96     buffer.append("[");
97
98     if (exclusive)
99       buffer.append("^");
100
101     for (int i = 0; i<getCharacterClassElementCount(); i++)
102       buffer.append(getCharacterClassElement(i).toString());
103
104     buffer.append("]");
105
106     if ((getMinOccurs()==1) && (getMaxOccurs()==1))
107     {
108       // nothing
109
}
110     else if ((getMinOccurs()==0) && (getMaxOccurs()==1))
111       buffer.append("?");
112     else if ((getMinOccurs()==0) && (getMaxOccurs()==Integer.MAX_VALUE))
113       buffer.append("*");
114     else if ((getMinOccurs()==1) && (getMaxOccurs()==Integer.MAX_VALUE))
115       buffer.append("+");
116     else
117     {
118       buffer.append("{");
119       buffer.append(String.valueOf(getMinOccurs()));
120       buffer.append(",");
121       buffer.append(String.valueOf(getMaxOccurs()));
122       buffer.append("}");
123     }
124
125     return buffer.toString();
126   }
127
128   /**
129    * Create a clone of this pattern.
130    *
131    * @return Clone of this pattern.
132    *
133    * @throws CloneNotSupportedException If an exception occurs during the cloning.
134    */

135   public Object JavaDoc clone()
136   {
137     CharacterClass clone = new CharacterClass();
138
139     clone.setMinOccurs(getMinOccurs());
140     clone.setMaxOccurs(getMaxOccurs());
141
142     for (int i = 0; i<getCharacterClassElementCount(); i++)
143       clone.addCharacterClassElement(getCharacterClassElement(i));
144
145     return clone;
146   }
147
148   /**
149    * Validates this pattern.
150    *
151    * @return Return a list of violations, if this pattern isn't valid.
152    */

153   public Violations validate()
154   {
155     Violations violations = new Violations();
156
157     if (getCharacterClassElementCount()<1)
158       violations.addViolation("Character class doesn't contain 1 or more elements", getLocation());
159
160     for (int i = 0; i<getCharacterClassElementCount(); i++)
161       violations.addViolations(getCharacterClassElement(i).validate());
162
163     return violations;
164   }
165 }
166
Popular Tags