if ((contextNode == null) || !(contextNode instanceof Element) ||
!(xpathExpr instanceof XPathExpressionImpl)) {
return;
}
Expression expression = ((XPathExpressionImpl) xpathExpr).getInternalExpression();
Iterator<Expression> subExpressions = (Iterator<Expression>) expression.iterateSubExpressions();
if (!subExpressions.hasNext()) {
return;
}
Expression subExpr = (Expression) subExpressions.next();
if (!(subExpr instanceof PathExpression)) {
return;
}
Document document = DOMUtils.toDOMDocument(contextNode);
PathExpression pathExpr = (PathExpression) subExpr;
Expression step = pathExpr.getFirstStep();
while (step != null) {
if (step instanceof AxisExpression) {
AxisExpression axisExpr = (AxisExpression) step;
NodeTest nodeTest = axisExpr.getNodeTest();
if (!(nodeTest instanceof NameTest)) {
break;
}
NameTest nameTest = (NameTest) nodeTest;
QName childName = getQualifiedName(nameTest.getFingerprint(),
namePool, contextUris);
if (Axis.CHILD == axisExpr.getAxis()) {
if (NodeKindTest.ELEMENT.getNodeKindMask() != nameTest.getNodeKindMask()) {
break;
}
NodeList children = ((Element) contextNode).getElementsByTagNameNS(childName.getNamespaceURI(),
childName.getLocalPart());
if ((children == null) || (children.getLength() == 0)) {
Node child = document.createElementNS(childName.getNamespaceURI(),
DOMUtils.getQualifiedName(childName));
contextNode.appendChild(child);
contextNode = child;
} else if (children.getLength() == 1) {
contextNode = children.item(0);
} else {
break;
}
} else if (Axis.ATTRIBUTE == axisExpr.getAxis()) {
if (NodeKindTest.ATTRIBUTE.getNodeKindMask() != nameTest.getNodeKindMask()) {
break;
}
Attr attribute = ((Element) contextNode).getAttributeNodeNS(childName.getNamespaceURI(), childName.getLocalPart());
if (attribute == null) {
attribute = document.createAttributeNS(childName.getNamespaceURI(), childName.getLocalPart());
((Element) contextNode).setAttributeNode(attribute);
contextNode = attribute;
} else {
break;
}
} else {
break;
}
} else if (step instanceof ItemChecker) {
ItemChecker itemChecker = (ItemChecker) step;
Expression baseExpr = itemChecker.getBaseExpression();
if (!(baseExpr instanceof VariableReference)) {
break;
}
} else {
break;
}
if (pathExpr != null) {
Expression remainingSteps = pathExpr.getRemainingSteps();
if (remainingSteps instanceof PathExpression) {
pathExpr = (PathExpression) remainingSteps;
step = pathExpr.getFirstStep();
} else if (remainingSteps instanceof AxisExpression) {