001package org.unix4j.codegen.xml; 002 003import java.util.ArrayList; 004import java.util.List; 005 006import org.w3c.dom.Element; 007import org.w3c.dom.NodeList; 008 009public class XmlUtil { 010 public static String getRequiredElementText(Element parent, Enum<?> child) { 011 final Element elChild = getSingleChildElement(parent, child); 012 return getRequiredElementText(elChild); 013 } 014 public static String getRequiredElementText(Element element) { 015 String text = element.getTextContent(); 016 if (text != null) { 017 text = text.trim(); 018 } 019 if (text != null && !text.isEmpty()) { 020 return text; 021 } 022 throw new IllegalArgumentException("text content missing or empty for XML element " + element.getNodeName()); 023 } 024 public static String getRequiredAttribute(Element element, Enum<?> attribute) { 025 final String value = getAttribute(element, attribute); 026 if (value != null) { 027 return value; 028 } 029 throw new IllegalArgumentException("attribute " + getXmlName(attribute) + " is missing or empty in XML element " + element.getNodeName()); 030 } 031 /** 032 * Returns the attribute value or {@code defaultValue} if it does not exist. 033 */ 034 public static String getAttribute(Element element, Enum<?> attribute, String defaultValue) { 035 final String value = getAttribute(element, attribute); 036 if (value != null) { 037 return value; 038 } 039 return defaultValue; 040 } 041 042 /** 043 * Returns the attribute value or {@code null} if it does not exist. 044 */ 045 public static String getAttribute(Element element, Enum<?> attribute) { 046 final String attName = getXmlName(attribute); 047 final String attValue = element.getAttribute(attName); 048 return attValue == null || attValue.isEmpty() ? null : attValue; 049 } 050 051 public static String getXmlName(Enum<?> node) { 052 String name = node.name(); 053 054 //remove leading or trailing underscore, it is used if a name conflicts with a Java keyword 055 if (name.length() > 0 && name.charAt(0) == '_') { 056 name = name.substring(1); 057 } 058 if (name.length() > 0 && name.charAt(name.length() - 1) == '_') { 059 name = name.substring(0, name.length() - 1); 060 } 061 return name.replace('_', '-');//use dashes instead of underscores in XML 062 } 063 064 public static Element getSingleChildElement(Element parent, Enum<?> child) { 065 final Element el = getSingleOptionalChildElement(parent, child); 066 if (el != null) { 067 return el; 068 } 069 throw new IllegalArgumentException("mandatory (single) child element " + getXmlName(child) + " missing for XML element " + parent.getNodeName()); 070 } 071 public static Element getSingleOptionalChildElement(Element parent, Enum<?> child) { 072 final String elName = getXmlName(child); 073 final NodeList list = parent.getElementsByTagName(elName); 074 if (list.getLength() == 1) { 075 return (Element)list.item(0); 076 } 077 if (list.getLength() == 0) { 078 return null; 079 } 080 throw new IllegalArgumentException("expected a single " + elName + " child element of XML element " + parent.getNodeName() + ", but found " + list.getLength()); 081 } 082 083 public static List<Element> getChildElements(Element parent, Enum<?> child) { 084 final String elName = getXmlName(child); 085 final NodeList list = parent.getElementsByTagName(elName); 086 final List<Element> children = new ArrayList<Element>(list.getLength()); 087 for (int i = 0; i < list.getLength(); i++) { 088 final Element elChild = (Element)list.item(i); 089 children.add(elChild); 090 } 091 return children; 092 } 093 094 //no instances 095 private XmlUtil() { 096 super(); 097 } 098}