1 16 17 22 package org.ditchnet.xml.dom; 23 24 import java.util.List ; 25 import java.util.Iterator ; 26 import java.util.ArrayList ; 27 import org.w3c.dom.*; 28 import org.ditchnet.xml.Xhtml; 29 30 37 public class DomUtils { 38 39 42 private DomUtils() { } 43 44 51 public static boolean hasId(final Element target,final String id) { 52 return target.getAttribute(Xhtml.Attr.ID.toString()).equals(id); 53 } 54 55 79 public static boolean hasClassName(final Element target, 81 String className) { 82 className = className.trim(); 83 88 String cn = target.getAttribute(Xhtml.Attr.CLASS.toString()); 89 if (null == cn || 0 == cn.length()) { 90 return false; 91 } 92 cn = cn.trim(); 93 if (cn.equals(className)) { 94 return true; 95 } 96 if (cn.indexOf(className + " ") > -1) { 97 return true; 98 } 99 if (_isLastOfMultpleClassNames(cn,className)) { 100 return true; 101 } 102 return false; 103 } 104 105 private static boolean _isLastOfMultpleClassNames(final String all, 106 final String className) { 107 int spaceBefore = all.lastIndexOf(className)-1; 108 return all.endsWith(className) && 109 all.substring(spaceBefore,spaceBefore+1).equals(" "); 110 } 111 112 117 public static boolean isElementNode(final Node target) { 118 return Node.ELEMENT_NODE == target.getNodeType(); 119 } 120 121 126 public static Element getFirstAncestorByClassName(final Node target, 127 final String className) { 128 Element parent = (Element)target; 129 while ((parent = (Element)parent.getParentNode()) != null) { 130 if (hasClassName(parent,className)) { 131 return parent; 132 } 133 } 134 return null; 135 } 136 137 142 public static Element getFirstAncestorOrSelfByClassName( 143 final Node target, 144 final String className) { 145 Element parent = (Element)target; 146 do { 147 if (hasClassName(parent,className)) { 148 return parent; 149 } 150 } while ((parent = (Element)parent.getParentNode()) != null); 151 return null; 152 } 153 154 159 public static Element getFirstChildByClassName( 160 final Node target, 161 final String className) { 162 Node childNode; 163 for (int i = 0; i < target.getChildNodes().getLength(); i++) { 164 childNode = target.getChildNodes().item(i); 165 if ( Node.ELEMENT_NODE != childNode.getNodeType() ) { 166 continue; 167 } 168 if (hasClassName(((Element)childNode),className)) { 169 return (Element)childNode; 170 } 171 } 172 return null; 173 } 174 175 180 public static Element getFirstDescendantByClassNameBreadthFirst( 181 final Node target, 182 final String className) { 183 Element result; 184 if ((result = getFirstChildByClassName(target,className)) != null) { 185 return result; 186 } 187 for (int i = 0; i < target.getChildNodes().getLength(); i++) { 188 result = getFirstDescendantByClassNameBreadthFirst( 189 target.getChildNodes().item(i), 190 className ); 191 if (null != result) { 192 return result; 193 } 194 } 195 return null; 196 } 197 198 203 public static Element getFirstDescendantByClassNameDepthFirst( 204 final Node target, 205 final String className) { 206 Node child; 207 Element result; 208 for (int i = 0; i < target.getChildNodes().getLength(); i++) { 209 child = target.getChildNodes().item(i); 210 if (isElementNode(child) && hasClassName(((Element)child),className)) { 211 return (Element)child; 212 } 213 result = getFirstDescendantByClassNameDepthFirst( 214 target.getChildNodes().item(i), 215 className ); 216 if (null != result) { 217 return result; 218 } 219 } 220 return null; 221 } 222 223 231 public static List getChildrenByClassName(final Node target, 232 final String className) { 233 List result = new ArrayList (); 234 Node child; 235 for (int i = 0; i < target.getChildNodes().getLength(); i++) { 236 child = target.getChildNodes().item(i); 237 if (isElementNode(child) && hasClassName(((Element)child),className)) { 238 result.add(child); 239 } 240 } 241 return result; 242 } 243 244 252 public static List getDescendantsByClassName(final Node target, 253 final String className) { 254 List result = new ArrayList (); 255 result.addAll(getChildrenByClassName(target,className)); 256 for (int i = 0; i < target.getChildNodes().getLength(); i++) { 257 result.addAll(getDescendantsByClassName( 258 target.getChildNodes().item(i), 259 className)); 260 } 261 return result; 262 } 263 264 265 266 } 267 | Popular Tags |