KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > extended > 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.extended;
10
11 import net.sourceforge.chaperon.common.Decoder;
12 import net.sourceforge.chaperon.common.SortedCharSet;
13 import net.sourceforge.chaperon.model.Violations;
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.8 2004/01/07 08:28:49 benedikta Exp $
21  */

22 public class CharacterClass extends Pattern
23 {
24   private SingleCharacter[] characters = new SingleCharacter[0];
25   private CharacterInterval[] intervals = new CharacterInterval[0];
26   private boolean exclusive = false;
27
28   /**
29    * Creates a pattern for a character class.
30    */

31   public CharacterClass() {}
32
33   public void addSingleCharacter(SingleCharacter character)
34   {
35     if (character==null)
36       return;
37
38     SingleCharacter[] newCharacters = new SingleCharacter[characters.length+1];
39     System.arraycopy(characters, 0, newCharacters, 0, characters.length);
40     newCharacters[characters.length] = character;
41     characters = newCharacters;
42   }
43
44   public SingleCharacter getSingleCharacter(int index)
45   {
46     return characters[index];
47   }
48
49   public SingleCharacter[] getSingleCharacters()
50   {
51     return characters;
52   }
53
54   public int getSingleCharacterCount()
55   {
56     return characters.length;
57   }
58
59   public void addCharacterInterval(CharacterInterval interval)
60   {
61     if (interval==null)
62       return;
63
64     CharacterInterval[] newIntervals = new CharacterInterval[intervals.length+1];
65     System.arraycopy(intervals, 0, newIntervals, 0, intervals.length);
66     newIntervals[intervals.length] = interval;
67     intervals = newIntervals;
68   }
69
70   public CharacterInterval getCharacterInterval(int index)
71   {
72     return intervals[index];
73   }
74
75   public CharacterInterval[] getCharacterIntervals()
76   {
77     return intervals;
78   }
79
80   public int getCharacterIntervalCount()
81   {
82     return intervals.length;
83   }
84
85   /**
86    * If the comparing character must match to the elements, or should not match to the elements.
87    *
88    * @param exclusive If the character class should be exclusive.
89    */

90   public void setExclusive(boolean exclusive)
91   {
92     this.exclusive = exclusive;
93   }
94
95   /**
96    * If this character class is exclusive
97    *
98    * @return If the character class should be exclusive.
99    */

100   public boolean isExclusive()
101   {
102     return exclusive;
103   }
104
105   public boolean isNullable()
106   {
107     return false;
108   }
109
110   public PatternSet getFirstSet()
111   {
112     PatternSet set = new PatternSet();
113     set.addPattern(this);
114     return set;
115   }
116
117   public PatternSet getLastSet()
118   {
119     PatternSet set = new PatternSet();
120     set.addPattern(this);
121     return set;
122   }
123
124   public char[] getLimits()
125   {
126     SortedCharSet limits = new SortedCharSet();
127     for (int i = 0; i<characters.length; i++)
128       limits.addChar(characters[i].getLimits());
129
130     for (int i = 0; i<intervals.length; i++)
131       limits.addChar(intervals[i].getLimits());
132
133     return limits.getChar();
134   }
135
136   public boolean contains(char minimum, char maximum)
137   {
138     if (!exclusive)
139     {
140       for (int i = 0; i<characters.length; i++)
141         if (characters[i].contains(minimum, maximum))
142           return true;
143
144       for (int i = 0; i<intervals.length; i++)
145         if (intervals[i].contains(minimum, maximum))
146           return true;
147
148       return false;
149     }
150
151     for (int i = 0; i<characters.length; i++)
152       if (characters[i].contains(minimum, maximum))
153         return false;
154
155     for (int i = 0; i<intervals.length; i++)
156       if (intervals[i].contains(minimum, maximum))
157         return false;
158
159     return true;
160   }
161
162   public boolean contains(char c)
163   {
164     if (!exclusive)
165     {
166       for (int i = 0; i<characters.length; i++)
167         if (characters[i].contains(c))
168           return true;
169
170       for (int i = 0; i<intervals.length; i++)
171         if (intervals[i].contains(c))
172           return true;
173
174       return false;
175     }
176
177     for (int i = 0; i<characters.length; i++)
178       if (characters[i].contains(c))
179         return false;
180
181     for (int i = 0; i<intervals.length; i++)
182       if (intervals[i].contains(c))
183         return false;
184
185     return true;
186   }
187
188   public String JavaDoc getSymbol()
189   {
190     return null;
191   }
192
193   /**
194    * Create a clone of this pattern.
195    *
196    * @return Clone of this pattern.
197    *
198    * @throws CloneNotSupportedException If an exception occurs during the cloning.
199    */

200   public String JavaDoc toString()
201   {
202     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
203
204     buffer.append("[");
205
206     if (exclusive)
207       buffer.append("^");
208
209     for (int i = 0; i<characters.length; i++)
210       buffer.append(Decoder.decode(characters[i].getCharacter(), Decoder.CLASS));
211
212     for (int i = 0; i<intervals.length; i++)
213     {
214       buffer.append(Decoder.decode(intervals[i].getFirstCharacter().getCharacter(), Decoder.CLASS));
215       buffer.append("-");
216       buffer.append(Decoder.decode(intervals[i].getLastCharacter().getCharacter(), Decoder.CLASS));
217     }
218
219     buffer.append("]");
220
221     buffer.append("[");
222     buffer.append(index);
223     buffer.append("]");
224
225     return buffer.toString();
226   }
227
228   /**
229    * Create a clone of this pattern.
230    *
231    * @return Clone of this pattern.
232    *
233    * @throws CloneNotSupportedException If an exception occurs during the cloning.
234    */

235   public Object JavaDoc clone()
236   {
237     CharacterClass clone = new CharacterClass();
238
239     for (int i = 0; i<characters.length; i++)
240       clone.addSingleCharacter(characters[i]);
241
242     for (int i = 0; i<intervals.length; i++)
243       clone.addCharacterInterval(intervals[i]);
244
245     clone.setExclusive(isExclusive());
246
247     return clone;
248   }
249
250   /**
251    * Validates this pattern.
252    *
253    * @return Return a list of violations, if this pattern isn't valid.
254    */

255   public Violations validate()
256   {
257     Violations violations = new Violations();
258
259     if ((characters.length+intervals.length)==0)
260       violations.addViolation("Character class is empty", getLocation());
261
262     for (int i = 0; i<characters.length; i++)
263       violations.addViolations(characters[i].validate());
264
265     for (int i = 0; i<intervals.length; i++)
266       violations.addViolations(intervals[i].validate());
267
268     return violations;
269   }
270 }
271
Popular Tags