Search in sources :

Example 1 with JsopReader

use of org.apache.jackrabbit.oak.commons.json.JsopReader in project jackrabbit-oak by apache.

the class DocumentNodeStoreHelper method loadValue.

private static void loadValue(String v, java.util.Collection<Blob> blobs, DocumentNodeStore nodeStore) {
    JsopReader reader = new JsopTokenizer(v);
    PropertyState p;
    if (reader.matches('[')) {
        p = DocumentPropertyState.readArrayProperty("x", nodeStore, reader);
        if (p.getType() == Type.BINARIES) {
            for (int i = 0; i < p.count(); i++) {
                Blob b = p.getValue(Type.BINARY, i);
                blobs.add(b);
            }
        }
    } else {
        p = DocumentPropertyState.readProperty("x", nodeStore, reader);
        if (p.getType() == Type.BINARY) {
            Blob b = p.getValue(Type.BINARY);
            blobs.add(b);
        }
    }
}
Also used : JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) Blob(org.apache.jackrabbit.oak.api.Blob) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer) PropertyState(org.apache.jackrabbit.oak.api.PropertyState)

Example 2 with JsopReader

use of org.apache.jackrabbit.oak.commons.json.JsopReader in project jackrabbit-oak by apache.

the class ChangeSet method fromString.

public static ChangeSet fromString(String json) {
    JsopReader reader = new JsopTokenizer(json);
    int maxPathDepth = 0;
    Set<String> parentPaths = null;
    Set<String> parentNodeNames = null;
    Set<String> parentNodeTypes = null;
    Set<String> propertyNames = null;
    Set<String> allNodeTypes = null;
    reader.read('{');
    if (!reader.matches('}')) {
        do {
            String name = reader.readString();
            reader.read(':');
            if ("maxPathDepth".equals(name)) {
                maxPathDepth = Integer.parseInt(reader.read(JsopReader.NUMBER));
            } else {
                Set<String> data = readArrayAsSet(reader);
                if ("parentPaths".equals(name)) {
                    parentPaths = data;
                } else if ("parentNodeNames".equals(name)) {
                    parentNodeNames = data;
                } else if ("parentNodeTypes".equals(name)) {
                    parentNodeTypes = data;
                } else if ("propertyNames".equals(name)) {
                    propertyNames = data;
                } else if ("allNodeTypes".equals(name)) {
                    allNodeTypes = data;
                }
            }
        } while (reader.matches(','));
        reader.read('}');
    }
    reader.read(JsopReader.END);
    return new ChangeSet(maxPathDepth, parentPaths, parentNodeNames, parentNodeTypes, propertyNames, allNodeTypes);
}
Also used : JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 3 with JsopReader

use of org.apache.jackrabbit.oak.commons.json.JsopReader in project jackrabbit-oak by apache.

the class LuceneJournalPropertyBuilder method addSerializedProperty.

// ~---------------------------------< deserialize >
@Override
public void addSerializedProperty(@Nullable String json) {
    if (json == null || json.isEmpty()) {
        return;
    }
    // TODO Add support for overflow
    JsopReader reader = new JsopTokenizer(json);
    reader.read('{');
    if (!reader.matches('}')) {
        do {
            String path = reader.readString();
            reader.read(':');
            reader.read('[');
            for (boolean first = true; !reader.matches(']'); first = false) {
                if (!first) {
                    reader.read(',');
                }
                if (sizeWithinLimits()) {
                    indexedNodes.put(path, reader.readString());
                }
            }
        } while (reader.matches(','));
        reader.read('}');
    }
    reader.read(JsopReader.END);
}
Also used : JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 4 with JsopReader

use of org.apache.jackrabbit.oak.commons.json.JsopReader 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 5 with JsopReader

use of org.apache.jackrabbit.oak.commons.json.JsopReader in project jackrabbit-oak by apache.

the class ChangeSet method fromString.

public static ChangeSet fromString(String json) {
    JsopReader reader = new JsopTokenizer(json);
    int maxPathDepth = 0;
    Set<String> parentPaths = null;
    Set<String> parentNodeNames = null;
    Set<String> parentNodeTypes = null;
    Set<String> propertyNames = null;
    Set<String> allNodeTypes = null;
    reader.read('{');
    if (!reader.matches('}')) {
        do {
            String name = reader.readString();
            reader.read(':');
            if ("maxPathDepth".equals(name)) {
                maxPathDepth = Integer.parseInt(reader.read(JsopReader.NUMBER));
            } else {
                Set<String> data = readArrayAsSet(reader);
                if ("parentPaths".equals(name)) {
                    parentPaths = data;
                } else if ("parentNodeNames".equals(name)) {
                    parentNodeNames = data;
                } else if ("parentNodeTypes".equals(name)) {
                    parentNodeTypes = data;
                } else if ("propertyNames".equals(name)) {
                    propertyNames = data;
                } else if ("allNodeTypes".equals(name)) {
                    allNodeTypes = data;
                }
            }
        } while (reader.matches(','));
        reader.read('}');
    }
    reader.read(JsopReader.END);
    return new ChangeSet(maxPathDepth, parentPaths, parentNodeNames, parentNodeTypes, propertyNames, allNodeTypes);
}
Also used : JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Aggregations

JsopReader (org.apache.jackrabbit.oak.commons.json.JsopReader)9 JsopTokenizer (org.apache.jackrabbit.oak.commons.json.JsopTokenizer)9 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)3 Blob (org.apache.jackrabbit.oak.api.Blob)2 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)2 Base64BlobSerializer (org.apache.jackrabbit.oak.json.Base64BlobSerializer)1 JsonDeserializer (org.apache.jackrabbit.oak.json.JsonDeserializer)1 ReferencedBlob (org.apache.jackrabbit.oak.plugins.blob.ReferencedBlob)1 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)1 Test (org.junit.Test)1