try {
source = this.resolver.resolveURI( systemID );
if ( ! (source instanceof ModifiableSource)) {
throw new ProcessingException("Source '"+systemID+"' is not writeable.");
}
ModifiableSource ws = (ModifiableSource)source;
exists = ws.exists();
target = source.getURI();
if ( exists && this.state == STATE_INSERT ) {
message = "content inserted at: " + path;
resource = SourceUtil.toDOM( source );
// import the fragment
Node importNode = resource.importNode(fragment, true);
// get the node
Node parent = DOMUtil.selectSingleNode(resource, path);
// replace?
if (replacePath != null) {
try {
Node replaceNode = DOMUtil.getSingleNode(parent, replacePath);
// now get the parent of this node until it is the parent node for insertion
while (replaceNode != null && replaceNode.getParentNode().equals(parent) == false) {
replaceNode = replaceNode.getParentNode();
}
if (replaceNode != null) {
if (overwrite) {
if (parent.getNodeType() == Node.DOCUMENT_NODE) {
// replacing of the document element is not allowed
DOMParser parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
try {
resource = parser.createDocument();
} finally {
this.manager.release( parser );
}
resource.appendChild(resource.importNode(importNode, true));
parent = resource;
replaceNode = resource.importNode(replaceNode, true);
} else {
parent.replaceChild(importNode, replaceNode);
}
message += ", replacing: " + replacePath;
if (reinsertPath != null) {
Node insertAt = DOMUtil.getSingleNode(parent, reinsertPath);
if (insertAt != null) {
while (replaceNode.hasChildNodes()) {
insertAt.appendChild(replaceNode.getFirstChild());
}
} else { // reinsert point null
message = "replace failed, could not find your reinsert path: " + reinsertPath;
resource = null;
}
}
} else { // overwrite was false
message = "replace failed, no overwrite allowed.";
resource = null;
}
} else { // specified replaceNode was not found
parent.appendChild(importNode);
}
} catch (javax.xml.transform.TransformerException sax) {
throw new ProcessingException("TransformerException: " + sax, sax);
}
} else { // no replace path, just do an insert at end
parent.appendChild(importNode);
}
} else if (create) {
DOMParser parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
try {
resource = parser.createDocument();
} finally {
this.manager.release( parser );
}
// import the fragment
Node importNode = resource.importNode(fragment, true);
if ( path.equals("") ) { // this is allowed in write
resource.appendChild(importNode.getFirstChild());
message = "entire source overwritten";
} else {
// get the node
Node parent = DOMUtil.selectSingleNode(resource, path);
// add fragment
parent.appendChild(importNode);
message = "content appended to: " + path;
}
} else {
message = "create not allowed";
resource = null;/**/
}
// write source
if ( resource != null) {
resource.normalize();
// use serializer
if (localSerializer == null) localSerializer = this.configuredSerializerName;
if (localSerializer != null) {
// Lookup the Serializer
ServiceSelector selector = null;
Serializer serializer = null;
OutputStream oStream = null;
try {
selector = (ServiceSelector)manager.lookup(Serializer.ROLE + "Selector");
serializer = (Serializer)selector.select(localSerializer);
oStream = ws.getOutputStream();
serializer.setOutputStream(oStream);
DOMStreamer streamer = new DOMStreamer(serializer);
streamer.stream(resource);
} finally {
if (oStream != null) {