@Override public Element getRootElement() { for (final Child child : children) { if (child.getType() == NodeType.ELEMENT) { return (Element) child; } } return null; }
@Override public boolean canAddChild(Child child) { if (child == null) { return false; } else if (child.getType() == NodeType.ELEMENT) { // TODO check recursion return true; } else { return true; } }
@Override public boolean canAddChild(Child child) { if (child == null) { return false; } else if (child.getType() == NodeType.ELEMENT) { return getRootElement() == null; } else { return true; } }
/** * Merges all consecutive texts locally. */ public default void mergeTexts() { if (getChildrenCount() > 1) { Child ref = getChildAt(0); int index = 1; while (index < getChildrenCount()) { final Child next = getChildAt(index); if (ref.getType() == NodeType.TEXT && next.getType() == NodeType.TEXT) { // ref and next are both texts: merge them ((Text) ref).appendContent(((Text) next).getContent()); // Remove next next.detach(); // Do not change index and ref } else { ref = next; index++; } } } }
/** * Merges all consecutive comments locally. */ public default void mergeComments() { if (getChildrenCount() > 1) { Child ref = getChildAt(0); int index = 1; while (index < getChildrenCount()) { final Child next = getChildAt(index); if (ref.getType() == NodeType.COMMENT && next.getType() == NodeType.COMMENT) { // ref and next are both texts: merge them ((Comment) ref).appendContent(((Comment) next).getContent()); // Remove next next.detach(); // Do not change index and ref } else { ref = next; index++; } } } }
/** * Adds a comment as last child. * * @param content The comment content. * @param merge If {@code true}, then if last child exists and is a comment, * {@code content} is added to this last child.<br> * Otherwise, a comment child is created and added to last position. * @return The modified or created comment. */ public default Comment addComment(String content, boolean merge) { if (merge) { final Child last = getLastChild(); if (last != null && last.getType() == NodeType.COMMENT) { ((Comment) last).appendContent(content); return (Comment) last; } } final Comment result = new Comment(content); addChild(result); return result; }
/** * Adds a text as last child. * * @param content The text content. * @param merge If {@code true}, then if last child exists and is a text, * {@code content} is added to this last child.<br> * Otherwise, a text child is created and added to last position. * @return The modified or created text. */ public Text addText(String content, boolean merge) { if (merge) { final Child last = getLastChild(); if (last != null && last.getType() == NodeType.TEXT) { ((Text) last).appendContent(content); return (Text) last; } } final Text result = new Text(content); addChild(result); return result; }