Skip to content

Commit

Permalink
#98 aibolit.P13 fixed in RemoveDirective
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 13, 2020
1 parent 342577a commit 722f424
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/main/java/org/xembly/RemoveDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,33 @@ public Directive.Cursor exec(final Node dom,
final Directive.Cursor cursor, final Directive.Stack stack) {
final Collection<Node> parents = new HashSet<>(cursor.size());
for (final Node node : cursor) {
final Node parent;
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
final Attr attr = Attr.class.cast(node);
parent = attr.getOwnerElement();
Element.class.cast(parent).removeAttributeNode(attr);
} else {
parent = node.getParentNode();
if (parent == null) {
throw new IllegalArgumentException(
"you can't delete root document element form XML"
);
}
parent.removeChild(node);
}
parents.add(parent);
parents.add(RemoveDirective.parent(node));
}
return new DomCursor(parents);
}

/**
* Convert it to the parent.
* @param node The node
* @return Its parent
*/
@SuppressWarnings("aibolit.P13")
private static Node parent(final Node node) {
final Node parent;
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
final Attr attr = Attr.class.cast(node);
parent = attr.getOwnerElement();
Element.class.cast(parent).removeAttributeNode(attr);
} else {
parent = node.getParentNode();
if (parent == null) {
throw new IllegalArgumentException(
"You can't delete root element from the XML"
);
}
parent.removeChild(node);
}
return parent;
}

}

0 comments on commit 722f424

Please sign in to comment.