Path.java 1.97 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.jackrabbit.oak.api.PropertyState
 *  org.apache.jackrabbit.oak.api.Tree
 */
package com.adobe.aemfd.formsfoundation.oak.restrictions.impl;

import com.adobe.aemfd.formsfoundation.oak.restrictions.impl.Value;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Tree;

public class Path {
    private final Tree tree;
    private final PropertyState propertyState;
    private final String path;
    private Tree root;

    public Path(Tree tree, PropertyState propertyState, String pathStr) {
        this.tree = tree;
        this.propertyState = propertyState;
        this.path = pathStr;
    }

    private Tree getRoot() {
        if (this.root != null) {
            return this.root;
        }
        this.root = this.tree;
        while (!this.root.isRoot()) {
            this.root = this.root.getParent();
        }
        return this.root;
    }

    public Value evaluate() {
        String[] pathTokens = this.path.split("/");
        Tree currentTree = this.tree;
        for (int i = 0; i < pathTokens.length; ++i) {
            String token = pathTokens[i];
            if (token.length() == 0) {
                currentTree = this.getRoot();
                continue;
            }
            if ("..".equals(token)) {
                currentTree = currentTree.getParent();
                continue;
            }
            if (".".equals(token)) continue;
            if (i == pathTokens.length - 1) {
                if (currentTree == this.tree && this.propertyState != null && this.propertyState.getName().equals(token)) {
                    return Value.fromPropertyState(this.propertyState);
                }
                PropertyState propertyState = currentTree.getProperty(token);
                return Value.fromPropertyState(propertyState);
            }
            currentTree = currentTree.getChild(token);
        }
        return null;
    }
}