}
catch (UnsupportedEncodingException e) {
// retarded
}
JcrNode existing = null;
try {
// there doesn't seem to be a way in JCR to check if there is
// such node in workspace
// except trying to get it and then catching the exception
existing = session.getNodeByIdentifier(uuid);
}
catch (JcrException e) {
}
int uuidBehavior;
if (existing != null && existing.getParent().equals(parentNode)) {
uuidBehavior = ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING;
} else {
uuidBehavior = ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;
}
if (uuidBehavior != ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW) {
// simpler alternative - if we replace node or throw error on UUID
// clash
session.importXML(parentNode.getPath(), stream, uuidBehavior);
return session.getNodeByIdentifier(uuid);
} else {
// more complicated alternative - on uuid clash node gets new uuid
// and all
// cloned references should use the new one
boolean exists = existing != null;
session.importXML(parentNode.getPath(), stream, uuidBehavior);
if (exists == false) {
// if there was no node with such uuid in target workspace
return session.getNodeByIdentifier(uuid);
} else {
// otherwise get the latest child with such name
JcrNodeIterator iterator = parentNode.getNodes(name);
iterator.skip(iterator.getSize() - 1);
JcrNode newNode = iterator.nextNode();
String newUuid = newNode.getIdentifier();
// and if it has uuid other than the existing one (should always
// be the case)
if (uuid.equals(newUuid) == false) {
uuidMap.put(uuid, newUuid);