1 28 29 package com.caucho.relaxng.pattern; 30 31 import com.caucho.relaxng.RelaxException; 32 import com.caucho.relaxng.program.GroupItem; 33 import com.caucho.relaxng.program.Item; 34 import com.caucho.util.CharBuffer; 35 36 import java.util.ArrayList ; 37 38 41 public class GroupPattern extends Pattern { 42 private ArrayList <Pattern> _patterns = new ArrayList <Pattern>(); 43 44 47 public GroupPattern() 48 { 49 } 50 51 54 public String getTagName() 55 { 56 return "group"; 57 } 58 59 62 public int getSize() 63 { 64 return _patterns.size(); 65 } 66 67 70 public Pattern getChild(int i) 71 { 72 return _patterns.get(i); 73 } 74 75 78 public void addChild(Pattern child) 79 throws RelaxException 80 { 81 child.setParent(this); 82 child.setElementName(getElementName()); 83 84 if (child instanceof GroupPattern) { 85 GroupPattern list = (GroupPattern) child; 86 87 for (int i = 0; i < list.getSize(); i++) 88 addChild(list.getChild(i)); 89 90 return; 91 } 92 93 _patterns.add(child); 94 95 boolean hasData = false; 96 boolean hasElement = false; 97 for (int i = 0; i < _patterns.size(); i++) { 98 if (_patterns.get(i).hasData()) 99 hasData = true; 100 else if (_patterns.get(i).hasElement()) 101 hasElement = true; 102 } 103 104 if (hasData && hasElement) 105 throw new RelaxException(L.l("<data> may not be in a <group>. Use <text> instead.")); 106 } 107 108 111 public Item createItem(GrammarPattern grammar) 112 throws RelaxException 113 { 114 if (_patterns.size() == 0) 115 return null; 116 117 Item tail = _patterns.get(_patterns.size() - 1).createItem(grammar); 118 119 for (int i = _patterns.size() - 2; i >= 0; i--) 120 tail = GroupItem.create(_patterns.get(i).createItem(grammar), tail); 121 122 return tail; 123 } 124 125 128 public String toProduction() 129 { 130 if (_patterns.size() == 0) 131 return "notAllowed"; 132 133 CharBuffer cb = new CharBuffer(); 134 135 for (int i = 0; i < _patterns.size(); i++) { 136 if (i != 0) 137 cb.append(", "); 138 139 Pattern pattern = _patterns.get(i); 140 141 if (pattern instanceof ChoicePattern && 142 ! (((ChoicePattern) pattern).hasEmpty())) 143 cb.append("(" + _patterns.get(i).toProduction() + ")"); 144 else 145 cb.append(_patterns.get(i).toProduction()); 146 } 147 148 return cb.toString(); 149 } 150 151 public boolean equals(Object o) 152 { 153 if (this == o) 154 return true; 155 156 if (! (o instanceof GroupPattern)) 157 return false; 158 159 GroupPattern group = (GroupPattern) o; 160 161 if (_patterns.size() != group._patterns.size()) 162 return false; 163 164 for (int i = 0; i < _patterns.size(); i++) 165 if (! _patterns.get(i).equals(group._patterns.get(i))) 166 return false; 167 168 return true; 169 } 170 171 174 public String toString() 175 { 176 return "GroupPattern" + _patterns; 177 } 178 } 179 180 | Popular Tags |