MyTree.java 1.95 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 */
package com.day.cq.mcm.core.servlets;

import com.day.cq.mcm.core.servlets.MyNode;
import com.day.cq.mcm.core.servlets.TreeVisitor;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

class MyTree {
    private TreeMap<String, MyNode> tree = new TreeMap();

    public MyNode addGetNode(Resource resource) {
        MyNode current = this.tree.get(resource.getPath());
        if (current == null) {
            current = new MyNode();
            current.initFrom(resource);
            this.addNode(current);
        }
        return current;
    }

    public void addNode(MyNode node) {
        if (!"/".equals(node.resource.getPath())) {
            MyNode parentNode;
            Resource parent = node.resource.getResourceResolver().getResource(node.resource, "../");
            if (parent == null) {
                throw new RuntimeException("Could not find parent for '" + node.resource.getPath() + "'!");
            }
            node.parent = parentNode = this.addGetNode(parent);
            parentNode.children.add(node.resource.getPath());
        }
        this.tree.put(node.resource.getPath(), node);
    }

    public boolean hasNode(String path) {
        return this.tree.containsKey(path);
    }

    public MyNode getNode(String path) {
        return this.tree.get(path);
    }

    public void depthFirstTraversal(TreeVisitor tv) {
        MyNode root = this.getNode("/");
        if (root != null) {
            this.depthFirstTraversal(tv, root);
        }
    }

    public void depthFirstTraversal(TreeVisitor tv, MyNode n) {
        tv.enter(n);
        for (String path : n.children) {
            this.depthFirstTraversal(tv, this.getNode(path));
        }
        tv.leave(n);
    }
}