EXistException, XPathException, TriggerException {
final NodeList children = content;
if (children.getLength() == 0)
{return 0;}
if (children.getLength() > 1)
{throw new EXistException("xupdate:replace requires exactly one content node");}
LOG.debug("processing replace ...");
int modifications = children.getLength();
try {
final StoredNode ql[] = selectAndLock(transaction);
final IndexListener listener = new IndexListener(ql);
final NotificationService notifier = broker.getBrokerPool().getNotificationService();
Node temp;
TextImpl text;
AttrImpl attribute;
ElementImpl parent;
for (int i = 0; i < ql.length; i++) {
final StoredNode node = ql[i];
if (node == null) {
LOG.warn("select " + selectStmt + " returned empty node set");
continue;
}
final DocumentImpl doc = (DocumentImpl)node.getOwnerDocument();
doc.getMetadata().setIndexListener(listener);
if (!doc.getPermissions().validate(broker.getSubject(), Permission.WRITE)) {
throw new PermissionDeniedException("User '" + broker.getSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
}
parent = (ElementImpl) node.getParentStoredNode();
if (parent == null)
{throw new EXistException("The root element of a document can not be replaced with 'xu:replace'. " +
"Please consider removing the document or use 'xu:update' to just replace the children of the root.");}
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
if (modifications == 0) {modifications = 1;}
temp = children.item(0);
parent.replaceChild(transaction, temp, node);
break;
case Node.TEXT_NODE:
temp = children.item(0);
text = new TextImpl(temp.getNodeValue());
modifications = 1;
text.setOwnerDocument(doc);
parent.updateChild(transaction, node, text);
break;
case Node.ATTRIBUTE_NODE:
final AttrImpl attr = (AttrImpl) node;
temp = children.item(0);
attribute = new AttrImpl(attr.getQName(), temp.getNodeValue(), broker.getBrokerPool().getSymbols());
attribute.setOwnerDocument(doc);
parent.updateChild(transaction, node, attribute);
break;
default:
throw new EXistException("unsupported node-type");
}
doc.getMetadata().clearIndexListener();
doc.getMetadata().setLastModified(System.currentTimeMillis());
modifiedDocuments.add(doc);
broker.storeXMLResource(transaction, doc);