1 11 12 package org.eclipse.pde.internal.core.util; 13 14 import org.w3c.dom.Attr ; 15 import org.w3c.dom.Element ; 16 import org.w3c.dom.NamedNodeMap ; 17 import org.w3c.dom.Node ; 18 import org.w3c.dom.NodeList ; 19 import org.w3c.dom.Text ; 20 21 25 public class CheatSheetUtil { 26 27 33 public static String parseElementText(Element element) { 34 element.normalize(); 38 if (element.getChildNodes().getLength() > 0) { 40 NodeList children = element.getChildNodes(); 41 StringBuffer buffer = new StringBuffer (); 42 for (int i = 0; i < children.getLength(); i++) { 44 Node node = children.item(i); 45 if (node.getNodeType() == Node.TEXT_NODE) { 46 buffer.append(((Text )node).getData()); 48 } else if (node.getNodeType() == Node.ELEMENT_NODE) { 49 Element subElement = (Element )node; 50 buffer.append('<'); 52 buffer.append(subElement.getNodeName()); 54 String elementAttributeText = 56 parseElementAttributes(subElement); 57 if (elementAttributeText != null) { 59 buffer.append(elementAttributeText); 60 } 61 String value = parseElementText(subElement); 63 if (value.length() > 0) { 65 buffer.append('>'); 69 buffer.append(value); 71 buffer.append('<'); 73 buffer.append('/'); 74 buffer.append(subElement.getNodeName()); 75 } else { 76 buffer.append('/'); 79 } 80 buffer.append('>'); 82 } 83 } 84 return buffer.toString(); 87 } 88 return ""; } 90 91 97 private static String parseElementAttributes(Element element) { 98 if (element.hasAttributes() == false) { 100 return null; 101 } 102 StringBuffer buffer = new StringBuffer (); 104 NamedNodeMap attributeMap = element.getAttributes(); 106 for (int i = 0; i < attributeMap.getLength(); i++) { 108 Node node = attributeMap.item(i); 109 if (node.getNodeType() == Node.ATTRIBUTE_NODE) { 110 Attr attribute = (Attr )node; 111 buffer.append(' '); 113 buffer.append(attribute.getName()); 115 buffer.append('='); 117 buffer.append('"'); 119 buffer.append(attribute.getValue()); 121 buffer.append('"'); 123 } 124 } 125 126 return buffer.toString(); 127 } 128 129 } 130 | Popular Tags |