1 package org.enhydra.xml; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import org.w3c.dom.DOMException ; 7 import org.w3c.dom.Document ; 8 import org.w3c.dom.Element ; 9 import org.w3c.dom.Node ; 10 import org.w3c.dom.NodeList ; 11 12 23 public class SearchElement extends HashMapElement { 24 25 public static String TAG_SEPARATOR = "/"; 26 public static String ATTR_SEPARATOR = "@"; 27 public static String EQUAL_SEPARATOR = "="; 28 29 32 public SearchElement() { 33 } 34 35 41 public SearchElement(Document ownerDoc, String name) { 42 super(ownerDoc, name); 43 } 44 45 50 public SearchElement(Node node) { 51 super(node); 52 } 53 54 59 public SearchElement(SearchElement node) { 60 super((HashMapElement) node); 61 } 62 63 68 protected Node newElementInstance(Node node) { 69 return new SearchElement(node); 70 } 71 72 80 public static Element newInstance(Document document) { 81 Node root = document.getDocumentElement(); 82 return new SearchElement(root); 83 } 84 85 92 public NodeList getSubElementsByTagName(String namePath) { 93 List list = new ArrayList (); 94 getSubElementsByTagName(namePath, list); 95 return new NodeListImpl(list); 96 } 97 98 101 private void getSubElementsByTagName(String name, List list) { 102 String [] keys = name.split(this.TAG_SEPARATOR, 2); 103 if (keys.length == 1) { 104 List l = (List ) this.children.get(name); 105 if (l != null) 106 list.addAll(l); 107 return; 108 } 109 NodeList tagChildren = this.getChildrenByTagName(keys[0]); 110 if (tagChildren != null) 111 for (int i = 0; i < tagChildren.getLength(); i++) 112 ((SearchElement) tagChildren.item(i)).getSubElementsByTagName( 113 keys[1], 114 list); 115 } 116 117 127 public NodeList getSubElementsByAttrValue(String attrPath, String attrValue) { 128 String [] keys = attrPath.split(this.ATTR_SEPARATOR, 2); 129 if (keys.length != 2) 130 throw new DOMException ( 131 DOMException.INVALID_ACCESS_ERR, 132 "Parameter not supported"); 133 List list = new ArrayList (); 134 getSubElementsByAttrValue(keys[0], keys[1], attrValue, list); 135 return new NodeListImpl(list); 136 } 137 138 139 142 private void getSubElementsByAttrValue(String tagName, String attrName, String attrValue, List list) { 143 String [] keys = tagName.split(this.TAG_SEPARATOR, 2); 144 if (keys.length == 1) { 145 List fList = (List ) this.children.get(tagName); 146 if (fList != null) { 147 for (int i = 0; i < fList.size(); i++) { 148 Element elm = (Element ) fList.get(i); 149 String val = (String ) elm.getAttribute(attrName); 150 if (val != null) 151 if (val.equals(attrValue)) 152 list.add(elm); 153 } 154 } 155 return; 156 } 157 NodeList tagChildren = this.getChildrenByTagName(keys[0]); 158 if (tagChildren != null) { 159 for (int i = 0; i < tagChildren.getLength(); i++) 160 ((SearchElement) tagChildren.item(i)).getSubElementsByAttrValue( 161 keys[1], 162 attrName, 163 attrValue, 164 list); 165 } 166 } 167 168 169 179 public NodeList getSubElementsByTagText(String tagPath, String tagValue) { 180 List list = new ArrayList (); 181 getSubElementsByTagText(tagPath, tagValue, list); 182 return new NodeListImpl(list); 183 } 184 185 186 189 private void getSubElementsByTagText( 190 String tagName, 191 String tagValue, 192 List list) { 193 String [] keys = tagName.split(this.TAG_SEPARATOR, 2); 194 if (keys.length == 1) { 195 List fList = (List ) this.children.get(tagName); 196 if (fList != null) { 197 for (int i = 0; i < fList.size(); i++) { 198 HashMapElement elm = (HashMapElement) fList.get(i); 199 String val = (String ) elm.getText(); 200 if (val != null) 201 if (val.equals(tagValue)) 202 list.add(elm); 203 } 204 } 205 return; 206 } 207 NodeList tagChildren = this.getChildrenByTagName(keys[0]); 208 if (tagChildren != null) { 209 for (int i = 0; i < tagChildren.getLength(); i++) 210 ((SearchElement) tagChildren.item(i)).getSubElementsByTagText( 211 keys[1], 212 tagValue, 213 list); 214 } 215 } 216 217 218 227 public NodeList getSubElementsByCondition(String condition) { 228 if (!condition.matches("([^@=]*)(@?[^@=/]*=[^@=/]*)")) 229 throw new DOMException ( 230 DOMException.INVALID_ACCESS_ERR, 231 "Parameter not supported"); 232 String [] keys = condition.split(this.EQUAL_SEPARATOR, 2); 233 String namePath = keys[0]; 234 if (namePath.indexOf(ATTR_SEPARATOR) != -1) 235 return getSubElementsByAttrValue(namePath, keys[1]); 236 else 237 return getSubElementsByTagText(namePath, keys[1]); 238 } 239 240 241 250 public Element getFirstSubElementsByCondition(String condition) { 251 NodeList nodes = getSubElementsByCondition(condition); 252 if (nodes != null && nodes.getLength() > 0) 253 return (Element ) nodes.item(0); 254 return null; 255 } 256 257 258 267 public Element getFirstSubElementByTagName(String namePath) { 268 NodeList nodes = getSubElementsByTagName(namePath); 269 if (nodes != null && nodes.getLength() > 0) 270 return (Element ) nodes.item(0); 271 return null; 272 } 273 274 275 282 public String getText(String namePath) { 283 NodeList nodes = this.getSubElementsByTagName(namePath); 284 if (nodes != null && nodes.getLength() > 0) 285 return ((SearchElement) nodes.item(0)).getText(); 286 return null; 287 } 288 289 290 296 public void setText(String namePath, String text) { 297 NodeList nodes = this.getSubElementsByTagName(namePath); 298 if (nodes != null && nodes.getLength() > 0) 299 ((SearchElement) nodes.item(0)).setText(text); 300 } 301 302 303 309 public void setAttr(String namePath, String value) { 310 if (!namePath.matches("([^@]*)(@[^@/]*)")) 311 throw new DOMException ( 312 DOMException.INVALID_ACCESS_ERR, 313 "Parameter not supported"); 314 String [] keys = namePath.split(this.ATTR_SEPARATOR, 2); 315 316 NodeList nodes = this.getSubElementsByTagName(keys[0]); 317 if (nodes != null && nodes.getLength() > 0) 318 ((SearchElement) nodes.item(0)).setAttribute(keys[1], value); 319 } 320 321 322 } 323 | Popular Tags |