package org.jboss.util.xml;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlHelper
{
public static void write(Writer out, Document dom)
throws Exception
{
DOMWriter writer = new DOMWriter(out, false);
writer.print(dom, true);
}
public static Iterator getChildrenByTagName(Element element,
String tagName)
{
if (element == null) return null;
NodeList children = element.getChildNodes();
ArrayList goodChildren = new ArrayList();
for (int i=0; i<children.getLength(); i++) {
Node currentChild = children.item(i);
if (currentChild.getNodeType() == Node.ELEMENT_NODE &&
((Element)currentChild).getTagName().equals(tagName)) {
goodChildren.add((Element)currentChild);
}
}
return goodChildren.iterator();
}
public static Element getUniqueChild(Element element, String tagName)
throws Exception
{
Iterator goodChildren = getChildrenByTagName(element, tagName);
if (goodChildren != null && goodChildren.hasNext()) {
Element child = (Element)goodChildren.next();
if (goodChildren.hasNext()) {
throw new Exception
("expected only one " + tagName + " tag");
}
return child;
} else {
throw new Exception
("expected one " + tagName + " tag");
}
}
public static Element getOptionalChild(Element element, String tagName)
throws Exception
{
return getOptionalChild(element, tagName, null);
}
public static Element getOptionalChild(Element element,
String tagName,
Element defaultElement)
throws Exception
{
Iterator goodChildren = getChildrenByTagName(element, tagName);
if (goodChildren != null && goodChildren.hasNext()) {
Element child = (Element)goodChildren.next();
if (goodChildren.hasNext()) {
throw new Exception
("expected only one " + tagName + " tag");
}
return child;
} else {
return defaultElement;
}
}
public static String getElementContent(final Element element)
throws Exception
{
return getElementContent(element, null);
}
public static String getElementContent(Element element, String defaultStr)
throws Exception
{
if (element == null)
return defaultStr;
NodeList children = element.getChildNodes();
String result = "";
for (int i = 0; i < children.getLength(); i++)
{
if (children.item(i).getNodeType() == Node.TEXT_NODE ||
children.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
{
result += children.item(i).getNodeValue();
}
else if( children.item(i).getNodeType() == Node.COMMENT_NODE )
{
}
}
return result.trim();
}
public static String getUniqueChildContent(Element element,
String tagName)
throws Exception
{
return getElementContent(getUniqueChild(element, tagName));
}
public static String getOptionalChildContent(Element element,
String tagName)
throws Exception
{
return getElementContent(getOptionalChild(element, tagName));
}
public static boolean getOptionalChildBooleanContent(Element element, String name) throws Exception
{
Element child = getOptionalChild(element, name);
if(child != null)
{
String value = getElementContent(child).toLowerCase();
return value.equals("true") || value.equals("yes");
}
return false;
}
}