* @param parent The Node that will get newChild as a new child node
* @param newChild The Node that wants to be appended to parent
* @return the element that is placed after newChild or null if newChild can be appended at last position
*/
protected Node getNextExistentElement(Element parent, Element newChild) {
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(parent.getOwnerDocument());
List<CMNode> cmNodes = getAvailableChildrenAtIndex(parent, 0, 0);
//get CMNode representation of newChild
CMNode newChildCmNode = null;
for (CMNode cmNode : cmNodes) {
String curModelNodeName = cmNode.getNodeName();
if (curModelNodeName.equals(newChild.getNodeName())) {
newChildCmNode = cmNode;
break;
}
}
//CMNode had been found, check all existent elements if newChild can be inserted before them
if (newChildCmNode != null) {
NodeList nodeList = parent.getChildNodes();
int len = nodeList.getLength();
for (int i=0; i<len; i++) {
try {
if (modelQuery.canInsert(parent, newChildCmNode, i, ModelQuery.VALIDITY_STRICT)) {
return nodeList.item(i);
}
} catch (Exception e) {
e.printStackTrace();
}