1 8 9 package net.sourceforge.chaperon.model.pattern; 10 11 import net.sourceforge.chaperon.model.Violations; 12 13 import java.util.Vector ; 14 15 22 public class CharacterClass extends Pattern 23 { 24 private Vector childs = new Vector (); 25 private boolean exclusive = false; 26 27 30 public CharacterClass() {} 31 32 37 public void addCharacterClassElement(CharacterClassElement element) 38 { 39 if (element!=null) 40 childs.addElement(element); 41 } 42 43 50 public CharacterClassElement getCharacterClassElement(int index) 51 { 52 return (CharacterClassElement)childs.elementAt(index); 53 } 54 55 60 public int getCharacterClassElementCount() 61 { 62 return childs.size(); 63 } 64 65 70 public void setExclusive(boolean exclusive) 71 { 72 this.exclusive = exclusive; 73 } 74 75 80 public boolean isExclusive() 81 { 82 return exclusive; 83 } 84 85 92 public String toString() 93 { 94 StringBuffer buffer = new StringBuffer (); 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 } 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 135 public Object 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 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 |