1 20 package org.enhydra.barracuda.core.util.dom; 21 22 import java.io.*; 23 import java.util.*; 24 import org.w3c.dom.*; 25 26 import org.enhydra.barracuda.core.util.*; 27 import org.enhydra.barracuda.plankton.*; 28 29 32 public class DOMUtil { 33 34 private static byte[] sep = System.getProperty("line.separator").getBytes(); 35 36 44 public static Text findFirstText(Node node) { 45 if (node instanceof Text) return (Text) node; 46 for (Node child = node.getFirstChild(); child!=null; child = child.getNextSibling()) { 47 Text text = findFirstText(child); 48 if (text!=null) return text; 49 } 50 return null; 51 } 52 53 63 public static Text getFirstText(Node node) { 64 Text text = findFirstText(node); 65 if (text==null) { 66 String msg = "No child text mode found for element"; 67 String id = getID(node); 68 throw new DOMException((short) -1, msg+(id!=null ? "; id=\""+id+"\"" : "")); 69 } 70 return text; 71 } 72 73 85 97 98 99 112 public static Node setTextInNode(Node node, String text, boolean allowMarkupInText) { 113 Comment comment = node.getOwnerDocument().createComment(""); 115 Node newNode = null; 116 117 if (allowMarkupInText) newNode = node.getOwnerDocument().createCDATASection(text); 119 else newNode = node.getOwnerDocument().createTextNode(text); 120 122 Text textComp = DOMUtil.findFirstText((Element) node); 123 if (textComp==null) { 125 node.appendChild(comment); 126 } else { 127 Node parent = textComp.getParentNode(); 128 parent.replaceChild(comment, textComp); 129 } 130 131 removeAllTextNodes(node); 133 134 Node parent = comment.getParentNode(); 136 parent.replaceChild(newNode, comment); 137 return node; 141 } 142 143 148 public static void removeAllTextNodes(Node node) { 149 if (node==null) return; 150 if (!node.hasChildNodes()) return; 151 NodeList nl = node.getChildNodes(); 152 for (int i=nl.getLength()-1; i>=0; i--) { 153 Node n = (Node) nl.item(i); 154 if (n instanceof Text) node.removeChild(n); 155 else removeAllTextNodes(n); 156 } 157 } 158 159 169 public static String getID(Node node) { 170 return getID(node, null); 171 } 172 173 185 public static String getID(Node node, String nullResponse) { 186 String nodeName = nullResponse; 187 if (node instanceof Element) { 188 nodeName = ((Element) node).getAttribute("id"); 189 } 190 return nodeName; 191 } 192 193 196 public static void printStackTrace(Node node) { 197 printStackTrace(node, System.out, 0); 198 } 199 200 210 public static void printStackTrace(Node node, OutputStream out, int depth) { 211 if (depth<0) depth = 0; 212 if (depth>25) depth = 25; 213 String spaces = " "; 214 String inset = spaces.substring(0,depth*3); 215 print (out, inset+node.getClass().getName()+"@"+Integer.toHexString(node.hashCode())); 216 StringBuffer sb = new StringBuffer (200); 217 String sep = ""; 218 Iterator it = Classes.getAllInterfaces(node).iterator(); 219 while (it.hasNext()) { 220 sb.append(sep+Classes.getShortClassName((Class ) it.next())); 221 sep = ", "; 222 } 223 print (out, inset+" implements: {"+sb.toString()+"}"); 224 print (out, inset+" name:"+node.getNodeName()); 225 print (out, inset+" attr:"+(node.hasAttributes() ? "" : " (n/a)")); 226 if (node.hasAttributes()) { 227 NamedNodeMap nnm = node.getAttributes(); 228 for (int i=0,max=nnm.getLength(); i<max; i++) { 229 Attr attr = (Attr) nnm.item(i); 230 print (out, inset+" "+attr.getName()+":"+attr.getValue()); 231 } 232 print (out, inset+" /end attr"); 233 } 234 print (out, inset+" children:"+(node.hasChildNodes() ? "" : " (n/a)")); 235 if (node.hasChildNodes()) { 236 NodeList nl = node.getChildNodes(); 237 for (int i=0,max=nl.getLength(); i<max; i++) { 238 printStackTrace(nl.item(i), out, depth+2); 239 } 240 print (out, inset+" /end children"); 241 } 242 print (out, inset+"/end @" + Integer.toHexString(node.hashCode())); 243 if (out!=null) try {out.flush();} catch (IOException ioe) {} 244 } 245 246 247 protected static void print(OutputStream out, String s) { 248 if (out!=null) try { 249 out.write(s.getBytes()); 250 out.write(sep); 251 } catch (IOException ioe) {} 252 } 253 254 public static void printMarkup(Node node) { 255 printMarkup(node, new PrintWriter(System.out, true), true, true, 0); 256 } 257 258 public static void printMarkup(Node node, PrintWriter out, boolean isHtml, boolean skipComments, int depth) { 259 if (depth<0) depth = 0; 260 if (depth>35) depth = 35; 261 String spaces = " "; 262 String inset = spaces.substring(0,depth*2); 263 264 if (node instanceof Element) { 266 Element el = (Element) node; 267 out.print(inset+"<"+el.getTagName()); 268 String sep = " "; 269 NamedNodeMap nnm = el.getAttributes(); 270 for (int i=0, max=nnm.getLength(); i<max; i++) { 271 Attr attr = (Attr) nnm.item(i); 272 out.print(sep+attr.getName()+"=\""+attr.getValue()+"\""); 273 } 274 out.println(">"+" {@"+Integer.toHexString(node.hashCode())+"}"); 275 276 printChildMarkup(node, out, isHtml, skipComments, depth); 278 279 String tag = el.getTagName().toLowerCase(); 281 if (!isHtml || 282 (!tag.equals("area") && !tag.equals("base") && !tag.equals("basefont") && 283 !tag.equals("br") && !tag.equals("col") && !tag.equals("frame") && 284 !tag.equals("hr") && !tag.equals("image") && !tag.equals("input") && 285 !tag.equals("isindex") && !tag.equals("link") && !tag.equals("meta") && 286 !tag.equals("param"))) { 287 out.println(inset+"</"+el.getTagName()+">"); 288 } 289 290 } else if (node instanceof CharacterData) { 292 if (node instanceof Comment) { 293 if (!skipComments) { 294 out.println(inset+"<!-- "+((CharacterData) node).getData()+" -->"); 295 } 296 } else { 297 out.println(inset+((CharacterData) node).getData()); 298 } 299 300 } else if (node.hasChildNodes()) { 302 printChildMarkup(node, out, isHtml, skipComments, depth); 303 304 } else { 306 System.out.println("Unhandled element:"+node.getClass().getName()); 307 org.enhydra.barracuda.plankton.data.CollectionsUtil.printStackTrace(Classes.getAllInterfaces(node)); 308 } 309 } 310 311 protected static void printChildMarkup(Node node, PrintWriter out, boolean isHtml, boolean skipComments, int depth) { 312 Node child = node.getFirstChild(); 313 if (child==null) return; 314 do { 315 printMarkup(child, out, isHtml, skipComments, depth+1); 316 child = child.getNextSibling(); 317 } while (child!=null); 318 } 319 320 321 } 322 | Popular Tags |