1 28 29 package com.caucho.relaxng.program; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.util.L10N; 33 import com.caucho.xml.QName; 34 35 import java.util.ArrayList ; 36 import java.util.HashSet ; 37 38 41 public class ChoiceNameItem extends NameClassItem { 42 protected final static L10N L = new L10N(ChoiceNameItem.class); 43 44 private ArrayList <NameClassItem> _items = new ArrayList <NameClassItem>(); 45 46 public ChoiceNameItem() 47 { 48 } 49 50 public static NameClassItem create(NameClassItem left, NameClassItem right) 51 { 52 ChoiceNameItem choice = new ChoiceNameItem(); 53 choice.addItem(left); 54 choice.addItem(right); 55 56 return choice.getMin(); 57 } 58 59 public void addItem(NameClassItem item) 60 { 61 if (item == null) 62 return; 63 else if (item instanceof ChoiceNameItem) { 64 ChoiceNameItem choice = (ChoiceNameItem) item; 65 66 for (int i = 0; i < choice._items.size(); i++) 67 addItem(choice._items.get(i)); 68 69 return; 70 } 71 72 for (int i = 0; i < _items.size(); i++) { 73 NameClassItem subItem = _items.get(i); 74 75 if (item.equals(subItem)) 76 return; 77 } 78 79 _items.add(item); 80 } 81 82 public NameClassItem getMin() 83 { 84 if (_items.size() == 0) 85 return null; 86 else if (_items.size() == 1) 87 return _items.get(0); 88 else 89 return this; 90 } 91 92 95 public void firstSet(HashSet <QName> set) 96 { 97 for (int i = 0; i < _items.size(); i++) 98 _items.get(i).firstSet(set); 99 } 100 101 104 public boolean matches(QName name) 105 { 106 for (int i = 0; i < _items.size(); i++) 107 if (_items.get(i).matches(name)) 108 return true; 109 110 return false; 111 } 112 113 116 public String toSyntaxDescription(String prefix) 117 { 118 CharBuffer cb = new CharBuffer(); 119 120 cb.append("("); 121 122 for (int i = 0; i < _items.size(); i++) { 123 if (i != 0) 124 cb.append(" | "); 125 126 cb.append(_items.get(i).toSyntaxDescription(prefix)); 127 } 128 129 cb.append(")"); 130 131 return cb.toString(); 132 } 133 134 137 public int hashCode() 138 { 139 int hash = 37; 140 141 for (int i = 0; i < _items.size(); i++) 142 hash += _items.get(i).hashCode(); 143 144 return hash; 145 } 146 147 150 public boolean equals(Object o) 151 { 152 if (this == o) 153 return true; 154 155 if (! (o instanceof ChoiceNameItem)) 156 return false; 157 158 ChoiceNameItem choice = (ChoiceNameItem) o; 159 160 return isSubset(choice) && choice.isSubset(this); 161 } 162 163 private boolean isSubset(ChoiceNameItem item) 164 { 165 if (_items.size() != item._items.size()) 166 return false; 167 168 for (int i = 0; i < _items.size(); i++) { 169 NameClassItem subItem = _items.get(i); 170 171 if (! item._items.contains(subItem)) 172 return false; 173 } 174 175 return true; 176 } 177 178 public String toString() 179 { 180 StringBuffer sb = new StringBuffer (); 181 182 sb.append("ChoiceNameItem["); 183 for (int i = 0; i < _items.size(); i++) { 184 if (i != 0) 185 sb.append(", "); 186 sb.append(_items.get(i)); 187 } 188 189 sb.append("]"); 190 191 return sb.toString(); 192 } 193 } 194 195 | Popular Tags |