1 29 30 package com.caucho.relaxng.pattern; 31 32 import com.caucho.relaxng.RelaxException; 33 import com.caucho.relaxng.program.ChoiceNameItem; 34 import com.caucho.relaxng.program.NameClassItem; 35 import com.caucho.util.CharBuffer; 36 37 import java.util.ArrayList ; 38 39 42 public class ChoiceNamePattern extends NameClassPattern { 43 private ArrayList <NameClassPattern> _patterns 44 = new ArrayList <NameClassPattern>(); 45 46 private NameClassItem _item; 47 48 51 public ChoiceNamePattern() 52 { 53 } 54 55 58 public int getSize() 59 { 60 return _patterns.size(); 61 } 62 63 66 public NameClassPattern getChild(int i) 67 { 68 return _patterns.get(i); 69 } 70 71 74 public void addNameChild(NameClassPattern child) 75 throws RelaxException 76 { 77 if (child instanceof ChoiceNamePattern) { 78 ChoiceNamePattern list = (ChoiceNamePattern) child; 79 80 for (int i = 0; i < list.getSize(); i++) 81 addChild(list.getChild(i)); 82 83 return; 84 } 85 86 if (! _patterns.contains(child)) 87 _patterns.add(child); 88 } 89 90 93 public String getTagName() 94 { 95 return "choice"; 96 } 97 98 101 public NameClassItem createNameItem() 102 throws RelaxException 103 { 104 if (_item == null) { 105 ChoiceNameItem item = new ChoiceNameItem(); 106 107 for (int i = 0; i < _patterns.size(); i++) { 108 item.addItem(_patterns.get(i).createNameItem()); 109 } 110 111 _item = item.getMin(); 112 } 113 114 return _item; 115 } 116 117 120 public String toProduction() 121 { 122 CharBuffer cb = new CharBuffer(); 123 124 for (int i = 0; i < _patterns.size(); i++) { 125 if (i != 0) 126 cb.append(" | "); 127 cb.append(_patterns.get(i).toProduction()); 128 } 129 130 return cb.toString(); 131 } 132 133 public boolean equals(Object o) 134 { 135 if (this == o) 136 return true; 137 138 if (! (o instanceof ChoiceNamePattern)) 139 return false; 140 141 ChoiceNamePattern choice = (ChoiceNamePattern) o; 142 143 if (_patterns.size() != choice._patterns.size()) 144 return false; 145 146 return isSubset(choice) && choice.isSubset(this); 147 } 148 149 private boolean isSubset(ChoiceNamePattern item) 150 { 151 if (_patterns.size() != item._patterns.size()) 152 return false; 153 154 for (int i = 0; i < _patterns.size(); i++) { 155 Pattern subPattern = _patterns.get(i); 156 157 if (! item._patterns.contains(subPattern)) 158 return false; 159 } 160 161 return true; 162 } 163 164 167 public String toString() 168 { 169 return "ChoiceNamePattern" + _patterns; 170 } 171 } 172 173 | Popular Tags |