org.dom4j.io
class: SAXModifyElementHandler [javadoc |
source]
java.lang.Object
org.dom4j.io.SAXModifyElementHandler
All Implemented Interfaces:
ElementHandler
This
org.dom4j.ElementHandler is used to trigger
ElementModifier objects in order to modify (parts of) the Document on the
fly.
When an element is completely parsed, a copy is handed to the associated (if
any) ElementModifier that on his turn returns the modified element
that has to come in the tree.
- author:
Wonne - Keysers (Realsoftware.be)
| Method from org.dom4j.io.SAXModifyElementHandler Detail: |
protected Element getModifiedElement() {
return modifiedElement;
}
|
public void onEnd(ElementPath elementPath) {
try {
Element origElement = elementPath.getCurrent();
Element currentParent = origElement.getParent();
if (currentParent != null) {
// Clone sets parent + document to null
Element clonedElem = (Element) origElement.clone();
// Ask for modified element
modifiedElement = elemModifier.modifyElement(clonedElem);
if (modifiedElement != null) {
// Restore parent + document
modifiedElement.setParent(origElement.getParent());
modifiedElement.setDocument(origElement.getDocument());
// Replace old with new element in parent
int contentIndex = currentParent.indexOf(origElement);
currentParent.content().set(contentIndex, modifiedElement);
}
// Remove the old element
origElement.detach();
} else {
if (origElement.isRootElement()) {
// Clone sets parent + document to null
Element clonedElem = (Element) origElement.clone();
// Ask for modified element
modifiedElement = elemModifier.modifyElement(clonedElem);
if (modifiedElement != null) {
// Restore parent + document
modifiedElement.setDocument(origElement.getDocument());
// Replace old with new element in parent
Document doc = origElement.getDocument();
doc.setRootElement(modifiedElement);
}
// Remove the old element
origElement.detach();
}
}
// Put the new element on the ElementStack, it might get pruned by
// the PruningDispatchHandler
if (elementPath instanceof ElementStack) {
ElementStack elementStack = ((ElementStack) elementPath);
elementStack.popElement();
elementStack.pushElement(modifiedElement);
}
} catch (Exception ex) {
throw new SAXModifyException(ex);
}
}
|
public void onStart(ElementPath elementPath) {
this.modifiedElement = elementPath.getCurrent();
}
|