Package org.apache.jackrabbit.mk.json

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


            stagedTree.move(srcPath, destPath);
        }

        @Override
        String asDiff() {
            JsopBuilder diff = new JsopBuilder();
            diff.tag('>').key(srcPath).value(destPath);
            return diff.toString();
        }
View Full Code Here


            stagedTree.copy(srcPath, destPath);
        }

        @Override
        String asDiff() {
            JsopBuilder diff = new JsopBuilder();
            diff.tag('*').key(srcPath).value(destPath);
            return diff.toString();
        }
View Full Code Here

            stagedTree.setProperty(nodePath, propName, propValue);
        }

        @Override
        String asDiff() {
            JsopBuilder diff = new JsopBuilder();
            diff.tag('^').key(PathUtils.concat(nodePath, propName));
            if (propValue != null) {
                diff.encodedValue(propValue);
            } else {
                diff.value(null);
            }
            return diff.toString();
        }
View Full Code Here

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

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

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

        final HashMap<Id, ArrayList<String>> addedNodes = new HashMap<Id, ArrayList<String>>();
        final HashMap<Id, ArrayList<String>> removedNodes = new HashMap<Id, ArrayList<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(rp) {
            int levels = depth < 0 ? Integer.MAX_VALUE : depth;
            @Override
            public void propAdded(String propName, String value) {
                String p = PathUtils.concat(getCurrentPath(), propName);
                if (p.startsWith(pathFilter)) {
                    buff.tag('^').
                            key(p).
                            encodedValue(value).
                            newline();
                }
            }

            @Override
            public void propChanged(String propName, String oldValue, String newValue) {
                String p = PathUtils.concat(getCurrentPath(), propName);
                if (p.startsWith(pathFilter)) {
                    buff.tag('^').
                            key(p).
                            encodedValue(newValue).
                            newline();
                }
            }

            @Override
            public void propDeleted(String propName, String value) {
                String p = PathUtils.concat(getCurrentPath(), propName);
                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(ChildNodeEntry added) {
                String p = PathUtils.concat(getCurrentPath(), added.getName());
                if (p.startsWith(pathFilter)) {
                    ArrayList<String> removedPaths = removedNodes.get(added.getId());
                    if (removedPaths != null) {
                        // move detected
                        String removedPath = removedPaths.remove(0);
                        if (removedPaths.isEmpty()) {
                            removedNodes.remove(added.getId());
                        }
                        buff.tag('>').
                                // path/to/deleted/node
                                key(removedPath).
                                // path/to/added/node
                                value(p).
                                newline();
                    } else {
                        ArrayList<String> addedPaths = addedNodes.get(added.getId());
                        if (addedPaths == null) {
                            addedPaths = new ArrayList<String>();
                            addedNodes.put(added.getId(), addedPaths);
                        }
                        addedPaths.add(p);
                    }
                }
            }

            @Override
            public void childNodeDeleted(ChildNodeEntry deleted) {
                String p = PathUtils.concat(getCurrentPath(), deleted.getName());
                if (p.startsWith(pathFilter)) {
                    ArrayList<String> addedPaths = addedNodes.get(deleted.getId());
                    if (addedPaths != null) {
                        // move detected
                        String addedPath = addedPaths.remove(0);
                        if (addedPaths.isEmpty()) {
                            addedNodes.remove(deleted.getId());
                        }
                        buff.tag('>').
                                // path/to/deleted/node
                                key(p).
                                // path/to/added/node
                                value(addedPath).
                                newline();
                    } else {
                        ArrayList<String> removedPaths = removedNodes.get(deleted.getId());
                        if (removedPaths == null) {
                            removedPaths = new ArrayList<String>();
                            removedNodes.put(deleted.getId(), removedPaths);
                        }
                        removedPaths.add(p);
                    }
                }
            }

            @Override
            public void childNodeChanged(ChildNodeEntry changed, Id newId) {
                String p = PathUtils.concat(getCurrentPath(), changed.getName());
                if (PathUtils.isAncestor(p, pathFilter)
                        || p.startsWith(pathFilter)) {
                    --levels;
                    if (levels >= 0) {
                        // recurse
                        super.childNodeChanged(changed, newId);
                    } else {
                        buff.tag('^');
                        buff.key(p);
                        buff.object().endObject();
                        buff.newline();
                    }
                    ++levels;
                }
            }
        };
        diffHandler.start(before, after, path);

        // finally process remaining added nodes ...
        for (Map.Entry<Id, ArrayList<String>> entry : addedNodes.entrySet()) {
            for (String p : entry.getValue()) {
                buff.tag('+').
                        key(p).object();
                toJson(buff, rp.getNode(entry.getKey()), depth);
                buff.endObject().newline();
            }
        }
        //  ... and removed nodes
        for (Map.Entry<Id, ArrayList<String>> entry : removedNodes.entrySet()) {
            for (String p : entry.getValue()) {
                buff.tag('-');
                buff.value(p);
                buff.newline();
            }
        }
        return buff.toString();
    }
View Full Code Here

    public static String addFiles(AbstractBlobStore store, String dir) throws Exception {
        ArrayList<String> list = new ArrayList<String>();
        String root = new File(dir).getAbsolutePath();
        String parent = new File(dir).getParentFile().getAbsolutePath();
        addFiles(list, new File(root));
        JsopBuilder listing = new JsopBuilder();
        listing.object();
        for (String f : list) {
            FileInputStream in = new FileInputStream(f);
            String id = store.writeBlob(in);
            in.close();
            String name = f.substring(parent.length());
            listing.key(name).value(id);
            listing.newline();
        }
        listing.endObject();
        String l = listing.toString();
        String id = store.writeBlob(new ByteArrayInputStream(l.getBytes("UTF-8")));
        return id;
    }
View Full Code Here

                assertEquals(s, s2);
            }
            if (s.indexOf('/') >= 0) {
                continue;
            }
            JsopBuilder jsop = new JsopBuilder();
            jsop.tag('+').key(s).object().key(s).value("x").endObject();
            rev = mk.commit("/", jsop.toString(),
                    null, null);
            nodes = mk.getNodes("/" + s, rev, 0, 0, 100, null);
            jsop = new JsopBuilder();
            jsop.object().key(s).value("x").
                    key(":childNodeCount").value(0).endObject();
            String n = jsop.toString();
            assertEquals(n, nodes);
            nodes = mk.getNodes("/", rev, 0, 0, 100, null);
            jsop = new JsopBuilder();
            jsop.object().key(s).object().endObject().
            key(":childNodeCount").value(1).endObject();
            n = jsop.toString();
            assertEquals(n, nodes);
            jsop = new JsopBuilder();
            jsop.tag('-').value(s);
            rev = mk.commit("/", jsop.toString(), rev, null);
           
        }
    }
View Full Code Here

            String x2 = Utils.escapePropertyName(s);
            String s2 = Utils.unescapePropertyName(x2);
            if (!s.equals(s2)) {
                assertEquals(s, s2);
            }
            JsopBuilder jsop = new JsopBuilder();
            jsop.tag('+').key(s).object().key(s).value("x").endObject();
            rev = mk.commit("/", jsop.toString(),
                    null, null);
            nodes = mk.getNodes("/" + s, rev, 0, 0, 10, null);
            jsop = new JsopBuilder();
            jsop.object().key(s).value("x").
                    key(":childNodeCount").value(0).endObject();
            String n = jsop.toString();
            assertEquals(n, nodes);
            nodes = mk.getNodes("/", rev, 0, 0, 10, null);
            jsop = new JsopBuilder();
            jsop.object().key(s).object().endObject().
            key(":childNodeCount").value(1).endObject();
            n = jsop.toString();
            assertEquals(n, nodes);
            jsop = new JsopBuilder();
            jsop.tag('-').value(s);
            rev = mk.commit("/", jsop.toString(), rev, null);
        }
    }
View Full Code Here

        this.jsop = jsop;
        this.path = path;
    }

    JsopDiff(KernelNodeStore store) {
        this(store, new JsopBuilder(), "/");
    }
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

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