RootEvaluator.java 5.28 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.day.cq.search.impl.builder;

import com.day.cq.search.Predicate;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.eval.EvaluationContext;
import com.day.cq.search.eval.PathPredicateEvaluator;
import com.day.cq.search.eval.PredicateGroupEvaluator;
import com.day.cq.search.eval.TypePredicateEvaluator;
import com.day.cq.search.eval.XPath;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RootEvaluator
extends PredicateGroupEvaluator {
    private Predicate pathPredicate;
    private Predicate typePredicate;
    private List<OrderBySpec> orderBySpecs = new ArrayList<OrderBySpec>();

    @Override
    protected String getOpeningBracket() {
        return "[";
    }

    @Override
    protected String getClosingBracket() {
        return "]";
    }

    public static boolean isExcerpt(Predicate rootPredicate) {
        return rootPredicate.getBool("excerpt");
    }

    public static void setExcerpt(Predicate rootPredicate, boolean excerpt) {
        rootPredicate.set("excerpt", excerpt ? "true" : "false");
    }

    public void addOrderBySpec(String property, boolean ascending, boolean ignoreCase) {
        this.orderBySpecs.add(new OrderBySpec(property, ascending, ignoreCase));
    }

    @Override
    public String getXPathExpression(Predicate predicate, EvaluationContext context) {
        String type;
        PredicateGroup pg;
        Predicate p;
        Iterator<Predicate> i$;
        StringBuffer xpath = new StringBuffer();
        if (!(predicate instanceof PredicateGroup)) {
            return "";
        }
        PredicateGroup group = (PredicateGroup)predicate;
        if (group.size() == 1 || group.allRequired()) {
            i$ = group.iterator();
            while (i$.hasNext()) {
                p = i$.next();
                if (p instanceof PredicateGroup && (pg = (PredicateGroup)p).size() == 1) {
                    p = pg.get(0);
                }
                if (!"path".equals(p.getType()) || !p.hasNonEmptyValue("path") || !p.get("path").startsWith("/") || p.getBool("self")) continue;
                this.pathPredicate = p;
                this.pathPredicate.setIgnored(true);
                break;
            }
        }
        if (group.size() == 1 || group.allRequired()) {
            i$ = group.iterator();
            while (i$.hasNext()) {
                p = i$.next();
                if (p instanceof PredicateGroup && (pg = (PredicateGroup)p).size() == 1) {
                    p = pg.get(0);
                }
                if (!"type".equals(p.getType()) || !p.hasNonEmptyValue("type")) continue;
                this.typePredicate = p;
                this.typePredicate.setIgnored(true);
                break;
            }
        }
        if (this.pathPredicate != null) {
            xpath.append("/jcr:root").append(PathPredicateEvaluator.encodePath(this.pathPredicate));
            if (!this.pathPredicate.getBool("exact")) {
                if (this.pathPredicate.getBool("flat")) {
                    if (xpath.charAt(xpath.length() - 1) == '/') {
                        xpath.append("*");
                    } else {
                        xpath.append("/*");
                    }
                } else if (xpath.charAt(xpath.length() - 1) == '/') {
                    xpath.append("/*");
                } else {
                    xpath.append("//*");
                }
            }
        } else {
            xpath.append("//*");
        }
        if (this.typePredicate != null && (type = TypePredicateEvaluator.getType(this.typePredicate, context)) != null) {
            String typeConstraint = "element(*, " + type + ")";
            int end = xpath.length() - 1;
            if (xpath.charAt(end) == '*') {
                xpath.replace(end, end + 1, typeConstraint);
            } else if (xpath.charAt(end) != '/') {
                xpath.append("/").append(typeConstraint);
            } else {
                xpath.append(typeConstraint);
            }
        }
        xpath.append(super.getXPathExpression(predicate, context));
        if (RootEvaluator.isExcerpt(group)) {
            xpath.append("/rep:excerpt(.)");
        }
        if (this.orderBySpecs.size() > 0) {
            xpath.append(" order by ");
            int count = 0;
            for (OrderBySpec obs : this.orderBySpecs) {
                if (count > 0) {
                    xpath.append(", ");
                }
                xpath.append(obs.getOrderByForXPath());
                ++count;
            }
        }
        return xpath.toString();
    }

    public void cleanup() {
        if (this.pathPredicate != null) {
            this.pathPredicate.setIgnored(false);
        }
        if (this.typePredicate != null) {
            this.typePredicate.setIgnored(false);
        }
    }

    private static class OrderBySpec {
        public String property;
        public boolean ascending;
        public boolean ignoreCase;

        public OrderBySpec(String property, boolean ascending, boolean ignoreCase) {
            this.property = property;
            this.ascending = ascending;
            this.ignoreCase = ignoreCase;
        }

        public String getOrderByForXPath() {
            return XPath.getXPathOrderBy(this.property, this.ascending, this.ignoreCase);
        }
    }

}