use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.
the class DiffCache method parseJsopDiff.
/**
* Parses the jsop diff returned by
* {@link #getChanges(RevisionVector, RevisionVector, String, Loader)} and reports the
* changes by calling the appropriate methods on {@link Diff}.
*
* @param jsop the jsop diff to parse.
* @param diff the diff handler.
* @return {@code true} it the complete jsop was processed or {@code false}
* if one of the {@code diff} callbacks requested a stop.
* @throws IllegalArgumentException if {@code jsop} is malformed.
*/
static boolean parseJsopDiff(@Nonnull String jsop, @Nonnull Diff diff) {
if (jsop.trim().isEmpty()) {
return true;
}
JsopTokenizer t = new JsopTokenizer(jsop);
boolean continueComparison = true;
while (continueComparison) {
int r = t.read();
if (r == JsopReader.END) {
break;
}
switch(r) {
case '+':
{
String name = unshareString(t.readString());
t.read(':');
t.read('{');
while (t.read() != '}') {
// skip properties
}
continueComparison = diff.childNodeAdded(name);
break;
}
case '-':
{
String name = unshareString(t.readString());
continueComparison = diff.childNodeDeleted(name);
break;
}
case '^':
{
String name = unshareString(t.readString());
t.read(':');
t.read('{');
t.read('}');
continueComparison = diff.childNodeChanged(name);
break;
}
default:
throw new IllegalArgumentException("jsonDiff: illegal token '" + t.getToken() + "' at pos: " + t.getLastPos() + ' ' + jsop);
}
}
return continueComparison;
}
use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.
the class DocumentMK method parseJsonDiff.
//------------------------------< internal >--------------------------------
private void parseJsonDiff(Commit commit, String json, String rootPath) {
RevisionVector baseRev = commit.getBaseRevision();
String baseRevId = baseRev != null ? baseRev.toString() : null;
Set<String> added = Sets.newHashSet();
JsopReader t = new JsopTokenizer(json);
while (true) {
int r = t.read();
if (r == JsopReader.END) {
break;
}
String path = PathUtils.concat(rootPath, t.readString());
switch(r) {
case '+':
t.read(':');
t.read('{');
parseAddNode(commit, t, path);
added.add(path);
break;
case '-':
DocumentNodeState toRemove = nodeStore.getNode(path, commit.getBaseRevision());
if (toRemove == null) {
throw new DocumentStoreException("Node not found: " + path + " in revision " + baseRevId);
}
commit.removeNode(path, toRemove);
markAsDeleted(toRemove, commit, true);
break;
case '^':
t.read(':');
String value;
if (t.matches(JsopReader.NULL)) {
value = null;
} else {
value = t.readRawValue().trim();
}
String p = PathUtils.getParentPath(path);
if (!added.contains(p) && nodeStore.getNode(p, commit.getBaseRevision()) == null) {
throw new DocumentStoreException("Node not found: " + path + " in revision " + baseRevId);
}
String propertyName = PathUtils.getName(path);
commit.updateProperty(p, propertyName, value);
break;
case '>':
{
t.read(':');
String targetPath = t.readString();
if (!PathUtils.isAbsolute(targetPath)) {
targetPath = PathUtils.concat(rootPath, targetPath);
}
DocumentNodeState source = nodeStore.getNode(path, baseRev);
if (source == null) {
throw new DocumentStoreException("Node not found: " + path + " in revision " + baseRevId);
} else if (nodeExists(targetPath, baseRevId)) {
throw new DocumentStoreException("Node already exists: " + targetPath + " in revision " + baseRevId);
}
moveNode(source, targetPath, commit);
break;
}
case '*':
{
t.read(':');
String targetPath = t.readString();
if (!PathUtils.isAbsolute(targetPath)) {
targetPath = PathUtils.concat(rootPath, targetPath);
}
DocumentNodeState source = nodeStore.getNode(path, baseRev);
if (source == null) {
throw new DocumentStoreException("Node not found: " + path + " in revision " + baseRevId);
} else if (nodeExists(targetPath, baseRevId)) {
throw new DocumentStoreException("Node already exists: " + targetPath + " in revision " + baseRevId);
}
copyNode(source, targetPath, commit);
break;
}
default:
throw new DocumentStoreException("token: " + (char) t.getTokenType());
}
}
}
use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.
the class JournalEntry method getChanges.
@Nonnull
private TreeNode getChanges() {
if (changes == null) {
TreeNode node = new TreeNode(concurrent);
String c = (String) get(CHANGES);
if (c != null) {
node.parse(new JsopTokenizer(c));
}
changes = node;
}
return changes;
}
use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.
the class DocumentNodeState method fromString.
public static DocumentNodeState fromString(DocumentNodeStore store, String s) {
JsopTokenizer json = new JsopTokenizer(s);
String path = null;
RevisionVector rootRev = null;
RevisionVector lastRev = null;
boolean hasChildren = false;
HashMap<String, String> map = new HashMap<String, String>();
while (true) {
String k = json.readString();
json.read(':');
if ("path".equals(k)) {
path = json.readString();
} else if ("rev".equals(k)) {
rootRev = RevisionVector.fromString(json.readString());
} else if ("lastRev".equals(k)) {
lastRev = RevisionVector.fromString(json.readString());
} else if ("hasChildren".equals(k)) {
hasChildren = json.read() == JsopReader.TRUE;
} else if ("prop".equals(k)) {
json.read('{');
while (true) {
if (json.matches('}')) {
break;
}
k = json.readString();
json.read(':');
String v = json.readString();
map.put(k, v);
json.matches(',');
}
}
if (json.matches(JsopReader.END)) {
break;
}
json.read(',');
}
List<PropertyState> props = Lists.newArrayListWithCapacity(map.size());
for (Entry<String, String> e : map.entrySet()) {
String value = e.getValue();
if (value != null) {
props.add(store.createPropertyState(e.getKey(), value));
}
}
return new DocumentNodeState(store, path, rootRev, props, hasChildren, lastRev);
}
use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.
the class RandomizedClusterTest method normalize.
private static String normalize(String json) {
JsopTokenizer t = new JsopTokenizer(json);
t.read('{');
JsonObject o = JsonObject.create(t);
JsopBuilder w = new JsopBuilder();
o.toJson(w);
return w.toString();
}
Aggregations