Package org.apache.jackrabbit.mk.json

Examples of org.apache.jackrabbit.mk.json.JsopBuilder


        jsop.newline();
        indexer.buffer(jsop.toString());
    }

    void bufferDelete(String path) {
        JsopBuilder jsop = new JsopBuilder();
        jsop.tag('-').value(PathUtils.concat(name, path));
        jsop.newline();
        indexer.buffer(jsop.toString());
    }
View Full Code Here


    @Override
    void writeCreate() {
        verify();
        tree.modified(this);
        JsopBuilder jsop = new JsopBuilder();
        jsop.tag('+').key(PathUtils.concat(tree.getName(), getPath())).object();
        jsop.key("keys").array();
        for (String k : keys) {
            jsop.value(k);
        }
        jsop.endArray();
        jsop.key("values").array();
        for (String v : values) {
            jsop.value(v);
        }
        jsop.endArray();
        jsop.endObject();
        jsop.newline();
        tree.buffer(jsop.toString());
    }
View Full Code Here

    }

    public String asString() {
        // TODO ALLOW_UNQUOTED_FIELD_NAMES to safe space
        // (check what Javascript supports and what are the keywords)
        JsopWriter json = new JsopBuilder();
        json.setLineLength(120);
        boolean inline = true;
        if (id != null && !id.isInline()) {
            String nodeId = map.formatId(id);
            if (nodeId != null) {
                inline = false;
                json.encodedValue(nodeId).tag('=');
            }
        }
        json.object();
        String[] pv = propertyValuePairs;
        if (pv != null) {
            for (int i = 0, size = pv.length; i < size; i += 2) {
                json.key(pv[i]).encodedValue(pv[i + 1]);
            }
        }
        if (map.getHash() && id != null) {
            byte[] hash = getHash();
            json.key(HASH).value(StringUtils.convertBytesToHex(hash));
        }
        if (childNodes != null && childNodes.size() > 0) {
            if (map.getDescendantCount()) {
                if (descendantCount > childNodes.size()) {
                    json.key(DESCENDANT_COUNT).value(descendantCount);
                }
            }
            childNodes.append(json, map);
        }
        json.endObject();
        if (!inline) {
            json.tag(';');
        }
        return json.toString();
    }
View Full Code Here

                }
            } while (t.matches(','));
            t.read('}');
        }
        if (!names.isEmpty()) {
            JsopBuilder buff = new JsopBuilder();
            for (String name : names) {
                buff.tag('-').value(name).newline();
            }
            mk.commit("/", buff.toString(), mk.getHeadRevision(), null);
        }
        if (!properties.isEmpty()) {
            JsopBuilder buff = new JsopBuilder();
            for (String property : properties) {
                buff.tag('^').key(property).value(null).newline();
            }
            mk.commit("/", buff.toString(), mk.getHeadRevision(), null);
        }
    }
View Full Code Here

            switch (r) {
            case '+':
                t.read(':');
                if (t.matches('{')) {
                    NodeImpl n = NodeImpl.parse(map, t, 0);
                    JsopWriter diff = new JsopBuilder();
                    diff.tag('+').key(path);
                    n.append(diff, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, false);
                    buffer(path, diff);
                } else {
                    String value = t.readRawValue().trim();
                    JsopWriter diff = new JsopBuilder();
                    diff.tag('+').key(path);
                    diff.encodedValue(value);
                    buffer(path, diff);
                }
                break;
            case '-': {
                JsopWriter diff = new JsopBuilder();
                diff.tag('-').value(path);
                buffer(path, diff);
                break;
            }
            case '^':
                t.read(':');
                String value;
                if (t.matches(JsopReader.NULL)) {
                    JsopWriter diff = new JsopBuilder();
                    diff.tag('^').key(path).value(null);
                    buffer(path, diff);
                } else {
                    value = t.readRawValue().trim();
                    JsopWriter diff = new JsopBuilder();
                    diff.tag('^').key(path).encodedValue(value);
                    buffer(path, diff);
                }
                break;
            case '>': {
                t.read(':');
                JsopWriter diff = new JsopBuilder();
                if (t.matches('{')) {
                    String position = t.readString();
                    t.read(':');
                    String to = t.readString();
                    t.read('}');
                    if (!PathUtils.isAbsolute(to)) {
                        to = PathUtils.concat(rootPath, to);
                    }
                    diff.tag('>').key(path);
                    diff.object().key(position);
                    diff.value(to).endObject();
                } else {
                    String to = t.readString();
                    if (!PathUtils.isAbsolute(to)) {
                        to = PathUtils.concat(rootPath, to);
                    }
                    diff.tag('>').key(path);
                    diff.value(to);
                }
                buffer(path, diff);
                break;
            }
            default:
View Full Code Here

    }

    private JsopWriter getBuilder(String mount) {
        JsopWriter builder = builders.get(mount);
        if (builder == null) {
            builder = new JsopBuilder();
            builders.put(mount, builder);
        }
        return builder;
    }
View Full Code Here

        }
        indexRootNodeDepth = PathUtils.getDepth(indexRootNode);
        revision = mk.getHeadRevision();
        readRevision = revision;
        if (!mk.nodeExists(indexRootNode, revision)) {
            JsopBuilder jsop = new JsopBuilder();
            String p = "/";
            for (String e : PathUtils.elements(indexRootNode)) {
                p = PathUtils.concat(p, e);
                if (!mk.nodeExists(p, revision)) {
                    jsop.tag('+').key(PathUtils.relativize("/", p)).object().endObject().newline();
                }
            }
            revision = mk.commit("/", jsop.toString(), revision, null);
        } else {
            String node = mk.getNodes(indexRootNode, revision, 0, 0, Integer.MAX_VALUE, null);
            JsopTokenizer t = new JsopTokenizer(node);
            t.read('{');
            HashMap<String, String> map = new HashMap<String, String>();
View Full Code Here

     * @param toRevision the new index revision
     * @return the new head revision
     */
    public String updateEnd(String toRevision) {
        readRevision = toRevision;
        JsopBuilder jsop = new JsopBuilder();
        jsop.tag('^').key("rev").value(readRevision);
        buffer(jsop.toString());
        try {
            commitChanges();
        } catch (MicroKernelException e) {
            if (!mk.nodeExists(indexRootNode, revision)) {
                // the index node itself was removed, which is
View Full Code Here

        this.pathFilter = (pathFilter == null || "".equals(pathFilter)) ? "/" : pathFilter;

    }

    public String build() throws Exception {
        final JsopBuilder buff = new JsopBuilder();
        // maps (key: id of target node, value: path/to/target)
        // for tracking added/removed nodes; this allows us
        // to detect 'move' operations
        final HashMap<NodeState, String> addedNodes = new HashMap<NodeState, String>();
        final HashMap<NodeState, String> removedNodes = new HashMap<NodeState, String>();

        if (!PathUtils.isAncestor(path, pathFilter)
                && !path.startsWith(pathFilter)) {
            return "";
        }

        if (before == null) {
            if (after != null) {
                buff.tag('+').key(path).object();
                toJson(buff, after);
                return buff.endObject().newline().toString();
            } else {
                // path doesn't exist in the specified revisions
                return "";
            }
        } else if (after == null) {
            buff.tag('-');
            buff.value(path);
            return buff.newline().toString();
        }

        TraversingNodeDiffHandler diffHandler = new TraversingNodeDiffHandler(store) {
            @Override
            public void propertyAdded(PropertyState after) {
                String p = PathUtils.concat(getCurrentPath(), after.getName());
                if (p.startsWith(pathFilter)) {
                    buff.tag('+').
                            key(p).
                            encodedValue(after.getEncodedValue()).
                            newline();
                }
            }

            @Override
            public void propertyChanged(PropertyState before, PropertyState after) {
                String p = PathUtils.concat(getCurrentPath(), after.getName());
                if (p.startsWith(pathFilter)) {
                    buff.tag('^').
                            key(p).
                            encodedValue(after.getEncodedValue()).
                            newline();
                }
            }

            @Override
            public void propertyDeleted(PropertyState before) {
                String p = PathUtils.concat(getCurrentPath(), before.getName());
                if (p.startsWith(pathFilter)) {
                    // since property and node deletions can't be distinguished
                    // using the "- <path>" notation we're representing
                    // property deletions as "^ <path>:null"
                    buff.tag('^').
                            key(p).
                            value(null).
                            newline();
                }
            }

            @Override
            public void childNodeAdded(String name, NodeState after) {
                String p = PathUtils.concat(getCurrentPath(), name);
                if (p.startsWith(pathFilter)) {
                    addedNodes.put(after, p);
                    buff.tag('+').
                            key(p).object();
                    toJson(buff, after);
                    buff.endObject().newline();
                }
            }

            @Override
            public void childNodeDeleted(String name, NodeState before) {
                String p = PathUtils.concat(getCurrentPath(), name);
                if (p.startsWith(pathFilter)) {
                    removedNodes.put(before, p);
                    buff.tag('-');
                    buff.value(p);
                    buff.newline();
                }
            }

            @Override
            public void childNodeChanged(String name, NodeState before, NodeState after) {
                String p = PathUtils.concat(getCurrentPath(), name);
                if (PathUtils.isAncestor(p, pathFilter)
                        || p.startsWith(pathFilter)) {
                    super.childNodeChanged(name, before, after);
                }
            }
        };
        diffHandler.start(before, after, path);

        // check if this commit includes 'move' operations
        // by building intersection of added and removed nodes
        addedNodes.keySet().retainAll(removedNodes.keySet());
        if (!addedNodes.isEmpty()) {
            // this commit includes 'move' operations
            removedNodes.keySet().retainAll(addedNodes.keySet());
            // addedNodes & removedNodes now only contain information about moved nodes

            // re-build the diff in a 2nd pass, this time representing moves correctly
            buff.resetWriter();

            // TODO refactor code, avoid duplication

            diffHandler = new TraversingNodeDiffHandler(store) {
                @Override
                public void propertyAdded(PropertyState after) {
                    String p = PathUtils.concat(getCurrentPath(), after.getName());
                    if (p.startsWith(pathFilter)) {
                        buff.tag('+').
                                key(p).
                                encodedValue(after.getEncodedValue()).
                                newline();
                    }
                }

                @Override
                public void propertyChanged(PropertyState before, PropertyState after) {
                    String p = PathUtils.concat(getCurrentPath(), after.getName());
                    if (p.startsWith(pathFilter)) {
                        buff.tag('^').
                                key(p).
                                encodedValue(after.getEncodedValue()).
                                newline();
                    }
                }

                @Override
                public void propertyDeleted(PropertyState before) {
                    String p = PathUtils.concat(getCurrentPath(), before.getName());
                    if (p.startsWith(pathFilter)) {
                        // since property and node deletions can't be distinguished
                        // using the "- <path>" notation we're representing
                        // property deletions as "^ <path>:null"
                        buff.tag('^').
                                key(p).
                                value(null).
                                newline();
                    }
                }

                @Override
                public void childNodeAdded(String name, NodeState after) {
                    if (addedNodes.containsKey(after)) {
                        // moved node, will be processed separately
                        return;
                    }
                    String p = PathUtils.concat(getCurrentPath(), name);
                    if (p.startsWith(pathFilter)) {
                        buff.tag('+').
                                key(p).object();
                        toJson(buff, after);
                        buff.endObject().newline();
                    }
                }

                @Override
                public void childNodeDeleted(String name, NodeState before) {
                    if (addedNodes.containsKey(before)) {
                        // moved node, will be processed separately
                        return;
                    }
                    String p = PathUtils.concat(getCurrentPath(), name);
                    if (p.startsWith(pathFilter)) {
                        buff.tag('-');
                        buff.value(p);
                        buff.newline();
                    }
                }

                @Override
                public void childNodeChanged(String name, NodeState before, NodeState after) {
                    String p = PathUtils.concat(getCurrentPath(), name);
                    if (PathUtils.isAncestor(p, pathFilter)
                            || p.startsWith(pathFilter)) {
                        super.childNodeChanged(name, before, after);
                    }
                }
            };
            diffHandler.start(before, after, path);

            // finally process moved nodes
            for (Map.Entry<NodeState, String> entry : addedNodes.entrySet()) {
                buff.tag('>').
                        // path/to/deleted/node
                        key(removedNodes.get(entry.getKey())).
                        // path/to/added/node
                        value(entry.getValue()).
                        newline();
            }
        }
        return buff.toString();
    }
View Full Code Here

            }
        } catch (Exception e) {
            throw new MicroKernelException(e);
        }

        JsopBuilder buff = new JsopBuilder().array();
        for (int i = history.size() - 1; i >= 0; i--) {
            StoredCommit commit = history.get(i);
            buff.object().
                    key("id").value(commit.getId().toString()).
                    key("ts").value(commit.getCommitTS()).
                    key("msg").value(commit.getMsg()).
                    endObject();
        }
        return buff.endArray().toString();
    }
View Full Code Here

TOP

Related Classes of org.apache.jackrabbit.mk.json.JsopBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.