if (targetElem == null) {
throw new RuntimeException("Nothing was selected by the to expression " + to + " on " + targetExpr);
}
}
NodeList nl = null;
if (source instanceof org.w3c.dom.Node) {
nl = (NodeList) exprFrom.evaluate(source, XPathConstants.NODESET);
} else if (source instanceof String) {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
//quirky: create a temporary element, use its nodelist
Element temp = doc.createElementNS(null, "temp");
temp.appendChild(doc.createTextNode((String) source));
nl = temp.getChildNodes();
} else if (source == null) {
// don't throw errors yet ?
throw new RuntimeException("Source value was null for source " + sourceExpr);
}
if (nl.getLength() == 0) {
throw new RuntimeException("Nothing was selected by the from expression " + from + " on " + sourceExpr);
}
for (int i = 0 ; i < nl.getLength(); i++) {
if (!(targetElem instanceof org.w3c.dom.Node)) {
if (nl.item(i) instanceof Attr) {
targetElem = ((Attr) nl.item(i)).getValue();
} else if (nl.item(i) instanceof Text) {
targetElem = ((Text) nl.item(i)).getWholeText();
} else {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
targetElem = doc.importNode(nl.item(i), true);
}
target = targetElem;
} else {
org.w3c.dom.Node n = ((org.w3c.dom.Node) targetElem).getOwnerDocument().importNode(nl.item(i), true);
if (n instanceof Attr) {
((Element) targetElem).setAttributeNode((Attr) n);
} else {
((org.w3c.dom.Node) targetElem).appendChild(n);
}