// (see VBM:2004102003) but this visitor is so fragile I am
// loath to change it. So, in this case we just check for the
// trivial case where all the child elements are redundant and
// remove them.
boolean allChildrenRedundant = true;
Node childNode = element.getHead();
while (childNode != null && allChildrenRedundant) {
if (childNode instanceof Element) {
Element childElement = (Element) childNode;
if (elementName.equals(childElement.getName())) {
// Check to see if all the child attributes are
// present and the same in the parent.
Attribute attribute = childElement.getAttributes();
while (attribute != null && allChildrenRedundant) {
String value = element.getAttributeValue(
attribute.getName());
if (!attribute.getValue().equals(value)) {
// child's attributes are different
allChildrenRedundant = false;
}
attribute = attribute.getNext();
}
} else {
// element names are different
allChildrenRedundant = false;
}
}
childNode = childNode.getNext();
}
if (allChildrenRedundant) {
// remove all the children *carefully*
childNode = element.getHead();
while (childNode != null) {
// Remember the next node in case we disturb it.
Node next = childNode.getNext();
// Remove the element.
if (childNode instanceof Element) {
removeElement((Element) childNode);
}
// Move to the next node.
childNode = next;
}
}
// close the tracker.
tracker.closed(elementName);
element.forEachChild(this);
} else {
Node sibling = element.getNext();
processElementToBeRemoved(element);
// Visit siblings to the right.
while (sibling != null) {
sibling.accept(this);
sibling = sibling.getNext();
}
// continue current traversal.
}
} else {
// Track this element (opened).