1 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 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 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 90 public void setExclusive(boolean exclusive) 91 { 92 this.exclusive = exclusive; 93 } 94 95 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 getSymbol() 189 { 190 return null; 191 } 192 193 200 public String toString() 201 { 202 StringBuffer buffer = new StringBuffer (); 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 235 public Object 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 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 |