List of usage examples for org.jdom2 Content getParent
public Parent getParent()
From source file:org.xflatdb.xflat.query.XPathUpdate.java
License:Apache License
/** * Applies the update operations to the given DOM Element representing * the data in a selected row./*from w ww. j a v a 2 s .com*/ * @param rowData The DOM Element representing the data in a selected row. * @return true if any updates were applied. */ public int apply(Element rowData) { int updateCount = 0; for (Update update : this.updates) { //the update's value will be one or the other, don't know which Content asContent = null; String asString = null; if (update.value instanceof String) { asString = (String) update.value; } if (update.value instanceof Content) { asContent = (Content) update.value; } for (Object node : update.path.evaluate(rowData)) { if (node == null) continue; Parent parent; Element parentElement; if (update.getUpdateType() == UpdateType.UNSET) { if (node instanceof Attribute) { parentElement = ((Attribute) node).getParent(); if (parentElement != null) { parentElement.removeAttribute((Attribute) node); updateCount++; } } else if (node instanceof Content) { parent = ((Content) node).getParent(); //remove this node from its parent element if (parent != null) { parent.removeContent((Content) node); updateCount++; } } continue; } //it's a set if (node instanceof Attribute) { //for attributes we set the value to empty string //this way it can still be selected by xpath for future updates if (update.value == null) { ((Attribute) node).setValue(""); updateCount++; } else { if (asString == null) { asString = getStringValue(update.value); } //if we fail conversion then do nothing. if (asString != null) { ((Attribute) node).setValue(asString); updateCount++; } } continue; } else if (!(node instanceof Content)) { //can't do anything continue; } Content contentNode = (Content) node; //need to convert if (update.value != null && asContent == null) { asContent = getContentValue(update.value); if (asContent == null) { //failed conversion, try text asString = getStringValue(update.value); if (asString != null) { //success! asContent = new Text(asString); } } } if (node instanceof Element) { //for elements we also set the value, but the value could be Content if (update.value == null) { ((Element) node).removeContent(); updateCount++; } else if (asContent != null) { if (asContent.getParent() != null) { //we used the content before, need to clone it asContent = asContent.clone(); } ((Element) node).setContent(asContent); updateCount++; } continue; } //at this point the node is Text, CDATA or something else. //The strategy now is to replace the value in its parent. parentElement = contentNode.getParentElement(); if (parentElement == null) { //can't do anything continue; } if (update.value == null || asContent != null) { //replace this content in the parent element int index = parentElement.indexOf(contentNode); parentElement.removeContent(index); if (update.value != null) { //if it was null then act like an unset, otherwise //its a replace if (asContent.getParent() != null) { //we used the content before, need to clone it asContent = asContent.clone(); } parentElement.addContent(index, asContent); } updateCount++; } } } return updateCount; }