import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 *
 * ChildElementWalker provides a convenience class used to
 * walk through all childs of an element that are elements 
 * and to skip text nodes that are assumed to be white space
 * 
 * @author Udo Altmann
 * @version 1.0
 */
public class ChildElementWalker {

Element parent;
Node    current;
String  tagname;

/**
 * @param p parent element
 * @param t null (every element is considered) or name of specific elements
 * 
 */
ChildElementWalker(Element p, String t) {
	parent  = p;
	tagname = t;
	reset();
}

/**
 * resets to first child
 */
public void reset() {
	if( parent != null ) {
		current = parent.getFirstChild();
	} else {
		current = null;
	}
}


/**
 * 
 * @return next element found or null if there is no more element 
 */
public Element getNextElement() {
	Element rueckgabe = null;
	while(current != null) {
		if(current.getNodeType() == Node.ELEMENT_NODE) {
			if(tagname == null) {
				rueckgabe = (Element) current;
			} else {
				if(current.getNodeName().equals(tagname) || tagname.equals("*") ) {
					rueckgabe = (Element) current;
				}
			}
		}
		current = current.getNextSibling();
		if(rueckgabe != null) {
			return rueckgabe;
		}
	}
	return null;
}
		

}