1 8 9 package net.sourceforge.chaperon.model.extended; 10 11 import net.sourceforge.chaperon.model.Violations; 12 13 19 public class Choice extends PatternList 20 { 21 24 public Choice() {} 25 26 public boolean isNullable() 27 { 28 boolean nullable = (getPatternCount()==0); 29 for (int i = 0; i<getPatternCount(); i++) 30 nullable |= getPattern(i).isNullable(); 31 32 return nullable; 33 } 34 35 public PatternSet getFirstSet() 36 { 37 PatternSet set = new PatternSet(); 38 for (int i = 0; i<getPatternCount(); i++) 39 set.addPattern(getPattern(i).getFirstSet()); 40 41 return set; 42 } 43 44 public PatternSet getLastSet() 45 { 46 PatternSet set = new PatternSet(); 47 for (int i = 0; i<getPatternCount(); i++) 48 set.addPattern(getPattern(i).getLastSet()); 49 50 return set; 51 } 52 53 public void update() 54 { 55 for (PatternIterator pattern = getPattern(); pattern.hasNext();) 56 pattern.next().update(); 57 } 58 59 public String toString() 60 { 61 StringBuffer buffer = new StringBuffer (); 62 63 if (getPatternCount()>1) 64 { 65 buffer.append("("); 66 for (int i = 0; i<getPatternCount(); i++) 67 { 68 if (i>0) 69 buffer.append("|"); 70 71 buffer.append(getPattern(i).toString()); 72 } 73 74 buffer.append(")"); 75 } 76 else if (getPatternCount()==1) 77 buffer.append(getPattern(0).toString()); 78 79 return buffer.toString(); 80 } 81 82 public String toString(PatternSet previous, PatternSet next) 83 { 84 StringBuffer buffer = new StringBuffer (); 85 86 if (getPatternCount()>1) 87 { 88 buffer.append("("); 89 for (int i = 0; i<getPatternCount(); i++) 90 { 91 if (i>0) 92 buffer.append("|"); 93 94 buffer.append(getPattern(i).toString(previous, next)); 95 } 96 97 buffer.append(")"); 98 } 99 else if (getPatternCount()==1) 100 buffer.append(getPattern(0).toString(previous, next)); 101 102 return buffer.toString(); 103 } 104 105 112 public Object clone() 113 { 114 Choice clone = new Choice(); 115 116 for (int i = 0; i<getPatternCount(); i++) 117 clone.addPattern(getPattern(i)); 118 119 return clone; 120 } 121 122 127 public Violations validate() 128 { 129 Violations violations = new Violations(); 130 131 if (getPatternCount()==0) 132 violations.addViolation("Choice doesn't contain any elements", getLocation()); 133 134 for (int i = 0; i<getPatternCount(); i++) 135 violations.addViolations(getPattern(i).validate()); 136 137 return violations; 138 } 139 } 140 | Popular Tags |