Package org.apache.jackrabbit.mk.json

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


            }
            if (nodeState == null) {
                return null;
            }

            JsopBuilder buf = new JsopBuilder().object();
            toJson(buf, nodeState, depth, (int) offset, maxChildNodes, true, nodeFilter);
            return buf.endObject().toString();
        } catch (Exception e) {
            throw new MicroKernelException(e);
        }
    }
View Full Code Here


   
    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();
    }
View Full Code Here

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

    public String build() throws Exception {
        final JsopBuilder buff = new JsopBuilder();

        // maps (key: the target node, value: the path to the target)
        // for tracking added/removed nodes; this allows us
        // to detect 'move' operations

        // TODO performance problem: this class uses NodeState as a hash key,
        // which is not recommended because the hashCode and equals methods
        // of those classes are slow

        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, depth);
                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) {
            int levels = depth < 0 ? Integer.MAX_VALUE : depth;
            @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, depth);
                    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)) {
                    --levels;
                    if (levels >= 0) {
                        // recurse
                        super.childNodeChanged(name, before, after);
                    } else {
                        buff.tag('^');
                        buff.key(p);
                        buff.object().endObject();
                        buff.newline();
                    }
                    ++levels;
                }
            }
        };
        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) {
                int levels = depth < 0 ? Integer.MAX_VALUE : depth;
                @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, depth);
                        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)) {
                        --levels;
                        if (levels >= 0) {
                            // recurse
                            super.childNodeChanged(name, before, after);
                        } else {
                            buff.tag('^');
                            buff.value(p);
                            buff.newline();
                        }
                        ++levels;
                    }
                }
            };
            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

                }
            } 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

        return list.size();
    }

    public String toString() {
        if (jsop == null) {
            JsopBuilder w = new JsopBuilder();
            w.array();
            for (String e : list) {
                w.encodedValue(e);
            }
            w.endArray();
            jsop = w.toString();
            start = 0;
        }
        return jsop.substring(start);
    }
View Full Code Here

        jsop = null;
    }

    public String toString() {
        if (jsop == null) {
            JsopBuilder w = new JsopBuilder();
            w.object();
            if (lengthIndex) {
                if (map.containsKey(LENGTHS_KEY)) {
                    throw new IllegalStateException("Object already contains the key " + LENGTHS_KEY);
                }
                StringBuilder buff = new StringBuilder();
                for (Entry<String, String> e : map.entrySet()) {
                    if (buff.length() > 0) {
                        buff.append(',');
                    }
                    buff.append(e.getValue().length());
                }
                w.key(LENGTHS_KEY).value(buff.toString());
            }
            for (Entry<String, String> e : map.entrySet()) {
                w.key(e.getKey()).encodedValue(e.getValue());
            }
            w.endObject();
            jsop = w.toString();
            start = 0;
        }
        return jsop.substring(start);
    }
View Full Code Here

        return result;
    }

    @Override
    public String toString() {
        JsopWriter json = new JsopBuilder();
        json.object();
        for (int i = 0; i < size; i++) {
            json.key(names[i]).value(children[i].toString());
        }
        json.endObject();
        return json.toString();
    }
View Full Code Here

    }

    @Override
    void writeCreate() {
        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();
        // could just use child node list, but then
        // new children need to be ordered at the right position,
        // and we would need a way to distinguish empty lists
        // from a leaf
        jsop.key("children").array();
        for (String d : children) {
            jsop.value(d);
        }
        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

        return list.size();
    }

    public String toString() {
        if (jsop == null) {
            JsopBuilder w = new JsopBuilder();
            w.array();
            for (String e : list) {
                w.encodedValue(e);
            }
            w.endArray();
            jsop = w.toString();
            start = 0;
        }
        return jsop.substring(start);
    }
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.