1 28 29 package com.caucho.relaxng.pattern; 30 31 import com.caucho.relaxng.RelaxException; 32 import com.caucho.relaxng.program.ElementItem; 33 import com.caucho.relaxng.program.Item; 34 35 38 public class ElementPattern extends Pattern { 39 private String _defName; 40 41 private NameClassPattern _name; 42 private GroupPattern _children = new GroupPattern(); 43 44 private Item _item; 45 46 49 public ElementPattern(String defName) 50 { 51 _defName = defName; 52 } 53 54 57 public String getTagName() 58 { 59 return "element"; 60 } 61 62 65 public String getDefName() 66 { 67 return _defName; 68 } 69 70 73 public GroupPattern getChildren() 74 { 75 return _children; 76 } 77 78 81 public boolean hasElement() 82 { 83 return true; 84 } 85 86 89 public void addNameChild(NameClassPattern child) 90 throws RelaxException 91 { 92 _name = child; 93 setElementName(_name.toProduction()); 94 } 95 96 99 public NameClassPattern getNameChild() 100 throws RelaxException 101 { 102 return _name; 103 } 104 105 108 public void addChild(Pattern child) 109 throws RelaxException 110 { 111 if (_name == null) 112 throw new RelaxException(L.l("<element> must have <name> definitions before other children.")); 113 114 child.setParent(_children); 115 child.setElementName(_children.getElementName()); 116 117 _children.addChild(child); 118 } 119 120 123 public void endElement() 124 throws RelaxException 125 { 126 if (_name == null) 127 throw new RelaxException(L.l("<element> must have a <name> definition.")); 128 129 if (_children.getSize() == 0) 130 throw new RelaxException(L.l("<element> tag `{0}' must have a child grammar production.", 131 _name.toProduction())); 132 } 133 134 137 public Item createItem(GrammarPattern grammar) 138 throws RelaxException 139 { 140 if (_item == null) { 141 ElementItem item = new ElementItem(this, _name.createNameItem()); 142 _item = item; 143 item.setChildrenItem(_children.createItem(grammar)); 144 } 145 146 return _item; 147 } 148 149 152 public String toProduction() 153 { 154 return _name.toProduction(); 155 } 156 157 public boolean equals(Object o) 158 { 159 if (this == o) 160 return true; 161 162 if (! (o instanceof ElementPattern)) 163 return false; 164 165 ElementPattern elt = (ElementPattern) o; 166 167 return _defName.equals(elt._defName); 168 } 169 170 173 public String toString() 174 { 175 return "ElementPattern[" + _name + "]"; 176 } 177 } 178 179 | Popular Tags |