1 28 29 package com.caucho.relaxng.pattern; 30 31 import com.caucho.relaxng.RelaxException; 32 import com.caucho.relaxng.program.ChoiceItem; 33 import com.caucho.relaxng.program.EmptyItem; 34 import com.caucho.relaxng.program.Item; 35 import com.caucho.util.CharBuffer; 36 37 import java.util.ArrayList ; 38 39 42 public class ChoicePattern extends Pattern { 43 private ArrayList <Pattern> _patterns = new ArrayList <Pattern>(); 44 private boolean _hasEmpty; 45 46 private Item _item; 47 48 51 public ChoicePattern() 52 { 53 } 54 55 58 public int getSize() 59 { 60 return _patterns.size(); 61 } 62 63 66 public Pattern getChild(int i) 67 { 68 return _patterns.get(i); 69 } 70 71 public boolean hasEmpty() 72 { 73 return _hasEmpty; 74 } 75 76 79 public boolean hasData() 80 { 81 for (int i = 0; i < _patterns.size(); i++) { 82 if (_patterns.get(i).hasData()) 83 return true; 84 } 85 86 return false; 87 } 88 89 92 public boolean hasElement() 93 { 94 for (int i = 0; i < _patterns.size(); i++) { 95 if (_patterns.get(i).hasElement()) 96 return true; 97 } 98 99 return false; 100 } 101 102 105 public void addChild(Pattern child) 106 throws RelaxException 107 { 108 child.setElementName(getElementName()); 109 110 if (child instanceof ChoicePattern) { 111 ChoicePattern list = (ChoicePattern) child; 112 113 if (list._hasEmpty) 114 _hasEmpty = true; 115 116 for (int i = 0; i < list.getSize(); i++) 117 addChild(list.getChild(i)); 118 119 return; 120 } 121 122 if (child instanceof EmptyPattern) { 123 _hasEmpty = true; 124 return; 125 } 126 127 if (! _patterns.contains(child)) 128 _patterns.add(child); 129 } 130 131 134 public String getTagName() 135 { 136 return "choice"; 137 } 138 139 142 public Item createItem(GrammarPattern grammar) 143 throws RelaxException 144 { 145 if (_item == null) { 146 ChoiceItem item = new ChoiceItem(); 147 148 for (int i = 0; i < _patterns.size(); i++) { 149 item.addItem(_patterns.get(i).createItem(grammar)); 150 } 151 152 if (_hasEmpty) 153 item.addItem(EmptyItem.create()); 154 155 _item = item.getMin(); 156 } 157 158 return _item; 159 } 160 161 164 public String toProduction() 165 { 166 if (_hasEmpty && _patterns.size() == 1) 167 return "(" + _patterns.get(0).toProduction() + ")?"; 168 169 CharBuffer cb = new CharBuffer(); 170 171 if (_hasEmpty) 172 cb.append("("); 173 174 for (int i = 0; i < _patterns.size(); i++) { 175 if (i != 0) 176 cb.append(" | "); 177 cb.append(_patterns.get(i).toProduction()); 178 } 179 180 if (_hasEmpty) 181 cb.append(")?"); 182 183 return cb.toString(); 184 } 185 186 public boolean equals(Object o) 187 { 188 if (this == o) 189 return true; 190 191 if (! (o instanceof ChoicePattern)) 192 return false; 193 194 ChoicePattern choice = (ChoicePattern) o; 195 196 if (_hasEmpty != choice._hasEmpty) 197 return false; 198 199 if (_patterns.size() != choice._patterns.size()) 200 return false; 201 202 return isSubset(choice) && choice.isSubset(this); 203 } 204 205 private boolean isSubset(ChoicePattern item) 206 { 207 if (_patterns.size() != item._patterns.size()) 208 return false; 209 210 for (int i = 0; i < _patterns.size(); i++) { 211 Pattern subPattern = _patterns.get(i); 212 213 if (! item._patterns.contains(subPattern)) 214 return false; 215 } 216 217 return true; 218 } 219 220 223 public String toString() 224 { 225 return "ChoicePattern" + _patterns; 226 } 227 } 228 229 | Popular Tags |