1 5 package com.opensymphony.workflow.loader; 6 7 import com.opensymphony.workflow.util.XMLizable; 8 9 import org.w3c.dom.Element ; 10 import org.w3c.dom.Node ; 11 import org.w3c.dom.NodeList ; 12 13 import java.io.PrintWriter ; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 19 24 public class XMLUtil { 25 27 public static Element getChildElement(Element parent, String childName) { 28 NodeList children = parent.getChildNodes(); 29 int size = children.getLength(); 30 31 for (int i = 0; i < size; i++) { 32 Node node = children.item(i); 33 34 if (node.getNodeType() == Node.ELEMENT_NODE) { 35 Element element = (Element ) node; 36 37 if (childName.equals(element.getNodeName())) { 38 return element; 39 } 40 } 41 } 42 43 return null; 44 } 45 46 public static List getChildElements(Element parent, String childName) { 47 NodeList children = parent.getChildNodes(); 48 List list = new ArrayList (); 49 int size = children.getLength(); 50 51 for (int i = 0; i < size; i++) { 52 Node node = children.item(i); 53 54 if (node.getNodeType() == Node.ELEMENT_NODE) { 55 Element element = (Element ) node; 56 57 if (childName.equals(element.getNodeName())) { 58 list.add(element); 59 } 60 } 61 } 62 63 return list; 64 } 65 66 public static String getChildText(Element parent, String childName) { 67 Element child = getChildElement(parent, childName); 68 69 if (child == null) { 70 return null; 71 } 72 73 return getText(child); 74 } 75 76 public static String getText(Element node) { 77 StringBuffer sb = new StringBuffer (); 78 NodeList list = node.getChildNodes(); 79 80 for (int i = 0; i < list.getLength(); i++) { 81 Node child = list.item(i); 82 83 switch (child.getNodeType()) { 84 case Node.CDATA_SECTION_NODE: 85 case Node.TEXT_NODE: 86 sb.append(child.getNodeValue()); 87 } 88 } 89 90 return sb.toString(); 91 } 92 93 public static String encode(Object string) { 94 if (string == null) { 95 return ""; 96 } 97 98 char[] chars = string.toString().toCharArray(); 99 StringBuffer out = new StringBuffer (); 100 101 for (int i = 0; i < chars.length; i++) { 102 switch (chars[i]) { 103 case '&': 104 out.append("&"); 105 106 break; 107 108 case '<': 109 out.append("<"); 110 111 break; 112 113 case '>': 114 out.append(">"); 115 116 break; 117 118 case '\"': 119 out.append("""); 120 121 break; 122 123 default: 124 out.append(chars[i]); 125 } 126 } 127 128 return out.toString(); 129 } 130 131 public static void printIndent(PrintWriter out, int indent) { 132 for (int i = 0; i < indent; i++) { 133 out.print(XMLizable.INDENT); 134 } 135 } 136 } 137 | Popular Tags |