1 28 29 package com.caucho.relaxng.pattern; 30 31 import com.caucho.relaxng.RelaxException; 32 import com.caucho.relaxng.program.InterleaveItem; 33 import com.caucho.relaxng.program.Item; 34 import com.caucho.util.CharBuffer; 35 36 import java.util.ArrayList ; 37 38 41 public class InterleavePattern extends Pattern { 42 private ArrayList <Pattern> _patterns = new ArrayList <Pattern>(); 43 44 private Item _item; 45 46 49 public InterleavePattern() 50 { 51 } 52 53 56 public String getTagName() 57 { 58 return "interleave"; 59 } 60 61 64 public int getSize() 65 { 66 return _patterns.size(); 67 } 68 69 72 public Pattern getChild(int i) 73 { 74 return _patterns.get(i); 75 } 76 77 80 public boolean hasData() 81 { 82 for (int i = 0; i < _patterns.size(); i++) { 83 if (_patterns.get(i).hasData()) 84 return true; 85 } 86 87 return false; 88 } 89 90 93 public boolean hasElement() 94 { 95 for (int i = 0; i < _patterns.size(); i++) { 96 if (_patterns.get(i).hasElement()) 97 return true; 98 } 99 100 return false; 101 } 102 103 106 public void addChild(Pattern child) 107 throws RelaxException 108 { 109 if (child instanceof DataPattern) 110 throw new RelaxException(L.l("<data> or <string> may not be used with interleave. Use <text> instead.")); 111 112 if (child instanceof EmptyPattern) 113 throw new RelaxException(L.l("<empty> is not allowed as a child of <interleave>")); 114 115 child.setParent(this); 116 child.setElementName(getElementName()); 117 118 if (child instanceof InterleavePattern) { 119 InterleavePattern interleave = (InterleavePattern) child; 120 121 for (int i = 0; i < interleave.getSize(); i++) 122 addChild(interleave.getChild(i)); 123 124 return; 125 } 126 127 if (_patterns.contains(child)) 128 return; 129 130 _patterns.add(child); 131 } 132 133 136 public Item createItem(GrammarPattern grammar) 137 throws RelaxException 138 { 139 if (_item == null) { 140 InterleaveItem item = new InterleaveItem(); 141 142 for (int i = 0; i < _patterns.size(); i++) { 143 item.addItem(_patterns.get(i).createItem(grammar)); 144 } 145 146 _item = item.getMin(); 147 } 148 149 return _item; 150 } 151 152 155 public String toProduction() 156 { 157 CharBuffer cb = new CharBuffer(); 158 159 for (int i = 0; i < _patterns.size(); i++) { 160 if (i != 0) 161 cb.append(" & "); 162 cb.append(_patterns.get(i).toProduction()); 163 } 164 165 return cb.toString(); 166 } 167 168 public boolean equals(Object o) 169 { 170 if (this == o) 171 return true; 172 173 if (! (o instanceof InterleavePattern)) 174 return false; 175 176 InterleavePattern interleave = (InterleavePattern) o; 177 178 if (_patterns.size() != interleave._patterns.size()) 179 return false; 180 181 return isSubset(interleave) && interleave.isSubset(this); 182 } 183 184 private boolean isSubset(InterleavePattern item) 185 { 186 if (_patterns.size() != item._patterns.size()) 187 return false; 188 189 for (int i = 0; i < _patterns.size(); i++) { 190 Pattern subPattern = _patterns.get(i); 191 192 if (! item._patterns.contains(subPattern)) 193 return false; 194 } 195 196 return true; 197 } 198 199 202 public String toString() 203 { 204 return "InterleavePattern" + _patterns; 205 } 206 } 207 208 | Popular Tags |