Search in sources :

Example 1 with JsopTokenizer

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;
}
Also used : Utils.unshareString(org.apache.jackrabbit.oak.plugins.document.util.Utils.unshareString) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 2 with JsopTokenizer

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());
        }
    }
}
Also used : JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 3 with JsopTokenizer

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;
}
Also used : JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer) Nonnull(javax.annotation.Nonnull)

Example 4 with JsopTokenizer

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);
}
Also used : HashMap(java.util.HashMap) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer) PropertyState(org.apache.jackrabbit.oak.api.PropertyState)

Example 5 with JsopTokenizer

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();
}
Also used : JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Aggregations

JsopTokenizer (org.apache.jackrabbit.oak.commons.json.JsopTokenizer)19 JsopReader (org.apache.jackrabbit.oak.commons.json.JsopReader)5 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)3 JsonObject (org.apache.jackrabbit.oak.commons.json.JsonObject)3 Nonnull (javax.annotation.Nonnull)2 Blob (org.apache.jackrabbit.oak.api.Blob)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Node (javax.jcr.Node)1 Tree (org.apache.jackrabbit.oak.api.Tree)1 JsopBuilder (org.apache.jackrabbit.oak.commons.json.JsopBuilder)1 ReferencedBlob (org.apache.jackrabbit.oak.plugins.blob.ReferencedBlob)1 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)1 RDBJSONSupport.appendJsonString (org.apache.jackrabbit.oak.plugins.document.rdb.RDBJSONSupport.appendJsonString)1 Utils.unshareString (org.apache.jackrabbit.oak.plugins.document.util.Utils.unshareString)1