throw new MicroKernelException("Path not found: " + path);
}
}
private void applyJsop(NodeBuilder builder, String jsonDiff) {
JsopTokenizer tokenizer = new JsopTokenizer(jsonDiff);
int token = tokenizer.read();
while (token != JsopReader.END) {
String path = tokenizer.readString();
String name = getName(path);
switch (token) {
case '+':
tokenizer.read(':');
tokenizer.read('{');
NodeBuilder parent = getNode(builder, getParentPath(path));
if (builder.hasChildNode(name)) {
throw new MicroKernelException(
"Node already exists: " + path);
}
addNode(parent.setChildNode(name), tokenizer);
break;
case '-':
getNode(builder, path).remove();
break;
case '^':
tokenizer.read(':');
NodeBuilder node = getNode(builder, getParentPath(path));
switch (tokenizer.read()) {
case JsopReader.NULL:
node.removeProperty(name);
break;
case JsopReader.FALSE:
node.setProperty(name, Boolean.FALSE);
break;
case JsopReader.TRUE:
node.setProperty(name, Boolean.TRUE);
break;
case JsopReader.STRING:
node.setProperty(name, tokenizer.getToken());
break;
case JsopReader.NUMBER:
String value = tokenizer.getToken();
try {
node.setProperty(name, Long.parseLong(value));
} catch (NumberFormatException e) {
node.setProperty(name, Double.parseDouble(value));
}
break;
default:
throw new UnsupportedOperationException();
}
break;
case '>':
tokenizer.read(':');
String targetPath = tokenizer.readString();
NodeBuilder targetParent =
getNode(builder, getParentPath(targetPath));
String targetName = getName(targetPath);
if (path.equals(targetPath) || PathUtils.isAncestor(path, targetPath)) {
throw new MicroKernelException(
"Target path must not be the same or a descendant of the source path: " + targetPath);
}
if (targetParent.hasChildNode(targetName)) {
throw new MicroKernelException(
"Target node exists: " + targetPath);
} else if (!getNode(builder, path).moveTo(
targetParent, targetName)) {
throw new MicroKernelException("Move failed");
}
break;
case '*':
tokenizer.read(':');
String copyTarget = tokenizer.readString();
if (!getNode(builder, path).copyTo(
getNode(builder, getParentPath(copyTarget)),
getName(copyTarget))) {
throw new MicroKernelException("Copy failed");
}
break;
default:
throw new MicroKernelException(
"Unexpected token: " + tokenizer.getEscapedToken());
}
token = tokenizer.read();
}
}