1 package de.timroes.axmlrpc; 2 3 import de.timroes.axmlrpc.xmlcreator.XmlElement; 4 import org.w3c.dom.Element; 5 import org.w3c.dom.Node; 6 import org.w3c.dom.NodeList; 7 8 /** 9 * This class provides some utility methods for the use with the Java DOM parser. 10 * 11 * @author Tim Roes 12 */ 13 public class XMLUtil { 14 XMLUtil()15 private XMLUtil() {} 16 17 /** 18 * Returns the only child element in a given NodeList. 19 * Will throw an error if there is more then one child element or any other 20 * child that is not an element or an empty text string (whitespace are normal). 21 * 22 * @param list A NodeList of children nodes. 23 * @return The only child element in the given node list. 24 * @throws XMLRPCException Will be thrown if there is more then one child element 25 * except empty text nodes. 26 */ getOnlyChildElement(NodeList list)27 public static Element getOnlyChildElement(NodeList list) throws XMLRPCException { 28 29 Element e = null; 30 Node n; 31 for(int i = 0; i < list.getLength(); i++) { 32 n = list.item(i); 33 // Strip only whitespace text elements and comments 34 if((n.getNodeType() == Node.TEXT_NODE 35 && n.getNodeValue().trim().length() <= 0) 36 || n.getNodeType() == Node.COMMENT_NODE) 37 continue; 38 39 // Check if there is anything else than an element node. 40 if(n.getNodeType() != Node.ELEMENT_NODE) { 41 throw new XMLRPCException("Only element nodes allowed."); 42 } 43 44 // If there was already an element, throw exception. 45 if(e != null) { 46 throw new XMLRPCException("Element has more than one children."); 47 } 48 49 e = (Element)n; 50 51 } 52 53 return e; 54 55 } 56 57 /** 58 * Returns the text node from a given NodeList. If the list contains 59 * more then just text nodes, an exception will be thrown. 60 * 61 * @param list The given list of nodes. 62 * @return The text of the given node list. 63 * @throws XMLRPCException Will be thrown if there is more than just one 64 * text node within the list. 65 */ getOnlyTextContent(NodeList list)66 public static String getOnlyTextContent(NodeList list) throws XMLRPCException { 67 68 StringBuilder builder = new StringBuilder(); 69 Node n; 70 71 for(int i = 0; i < list.getLength(); i++) { 72 n = list.item(i); 73 74 // Skip comments inside text tag. 75 if(n.getNodeType() == Node.COMMENT_NODE) { 76 continue; 77 } 78 79 if(n.getNodeType() != Node.TEXT_NODE && n.getNodeType() != Node.CDATA_SECTION_NODE) { 80 throw new XMLRPCException("Element must contain only text elements."); 81 } 82 83 builder.append(n.getNodeValue()); 84 85 } 86 87 return builder.toString(); 88 89 } 90 91 /** 92 * Checks if the given {@link NodeList} contains a child element. 93 * 94 * @param list The {@link NodeList} to check. 95 * @return Whether the {@link NodeList} contains children. 96 */ hasChildElement(NodeList list)97 public static boolean hasChildElement(NodeList list) { 98 99 Node n; 100 101 for(int i = 0; i < list.getLength(); i++) { 102 n = list.item(i); 103 104 if(n.getNodeType() == Node.ELEMENT_NODE) { 105 return true; 106 } 107 } 108 109 return false; 110 111 } 112 113 /** 114 * Creates an xml tag with a given type and content. 115 * 116 * @param type The type of the xml tag. What will be filled in the <..>. 117 * @param content The content of the tag. 118 * @return The xml tag with its content as a string. 119 */ makeXmlTag(String type, String content)120 public static XmlElement makeXmlTag(String type, String content) { 121 XmlElement xml = new XmlElement(type); 122 xml.setContent(content); 123 return xml; 124 } 125 126 } 127