1 8 9 package net.sourceforge.chaperon.model.pattern; 10 11 import net.sourceforge.chaperon.model.Violations; 12 13 19 public class Alternation extends PatternList 20 { 21 24 public Alternation() {} 25 26 33 public String toString() 34 { 35 StringBuffer buffer = new StringBuffer (); 36 37 for (int i = 0; i<getPatternCount(); i++) 38 { 39 if (i>0) 40 buffer.append("|"); 41 42 buffer.append(getPattern(i).toString()); 43 } 44 45 if ((getMinOccurs()==1) && (getMaxOccurs()==1)) 46 { 47 } 49 else if ((getMinOccurs()==0) && (getMaxOccurs()==1)) 50 buffer.append("?"); 51 else if ((getMinOccurs()==0) && (getMaxOccurs()==Integer.MAX_VALUE)) 52 buffer.append("*"); 53 else if ((getMinOccurs()==1) && (getMaxOccurs()==Integer.MAX_VALUE)) 54 buffer.append("+"); 55 else 56 { 57 buffer.append("{"); 58 buffer.append(String.valueOf(getMinOccurs())); 59 buffer.append(","); 60 buffer.append(String.valueOf(getMaxOccurs())); 61 buffer.append("}"); 62 } 63 64 return buffer.toString(); 65 } 66 67 74 public Object clone() 75 { 76 Alternation clone = new Alternation(); 77 78 clone.setMinOccurs(getMinOccurs()); 79 clone.setMaxOccurs(getMaxOccurs()); 80 81 for (int i = 0; i<getPatternCount(); i++) 82 clone.addPattern(getPattern(i)); 83 84 return clone; 85 } 86 87 92 public Violations validate() 93 { 94 Violations violations = new Violations(); 95 96 if (getPatternCount()==0) 97 violations.addViolation("Alternation doesn't contain any elements", getLocation()); 98 99 for (int i = 0; i<getPatternCount(); i++) 100 violations.addViolations(getPattern(i).validate()); 101 102 return violations; 103 } 104 } 105 | Popular Tags |