jcrsqlquerybuilder.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 947 行 · 第 1/3 页

JAVA
947 行
字号
            if (names[i].length() == 0) {                if (i == 0) {                    // root                    pathNode.addPathStep(new LocationStepQueryNode(pathNode));                } else {                    // descendant '//' -> invalid path                    // todo throw or ignore?                    // we currently do not throw and add location step for an                    // empty name (which is basically the root node)                    pathNode.addPathStep(new LocationStepQueryNode(pathNode));                }            } else {                int idx = names[i].indexOf('[');                String name;                int index = LocationStepQueryNode.NONE;                if (idx > -1) {                    // contains index                    name = names[i].substring(0, idx);                    String suffix = names[i].substring(idx);                    String indexStr = suffix.substring(1, suffix.length() - 1);                    if (indexStr.equals("%")) {                        // select all same name siblings                        index = LocationStepQueryNode.NONE;                    } else {                        try {                            index = Integer.parseInt(indexStr);                        } catch (NumberFormatException e) {                            log.warn("Unable to parse index for path element: " + names[i]);                        }                    }                    if (name.equals("%")) {                        name = null;                    }                } else {                    // no index specified                    // - index defaults to 1 if there is an explicit name test                    // - index defaults to NONE if name test is %                    name = names[i];                    if (name.equals("%")) {                        name = null;                    } else {                        index = 1;                    }                }                QName qName = null;                if (name != null) {                    try {                        qName = NameFormat.parse(name, resolver);                    } catch (NameException e) {                        throw new IllegalArgumentException("Illegal name: " + name);                    }                }                // if name test is % this means also search descendants                boolean descendant = name == null;                LocationStepQueryNode step = new LocationStepQueryNode(pathNode, qName, descendant);                if (index > 0) {                    step.setIndex(index);                }                pathNode.addPathStep(step);            }        }        pathConstraints.add(pathNode);    }    /**     * Translates a pattern using the escape character <code>from</code> into     * a pattern using the escape character <code>to</code>.     *     * @param pattern the pattern to translate     * @param from    the currently used escape character.     * @param to      the new escape character to use.     * @return the new pattern using the escape character <code>to</code>.     */    private static String translateEscaping(String pattern, char from, char to) {        // if escape characters are the same OR pattern does not contain any        // escape characters -> simply return pattern as is.        if (from == to || (pattern.indexOf(from) < 0 && pattern.indexOf(to) < 0)) {            return pattern;        }        StringBuffer translated = new StringBuffer(pattern.length());        boolean escaped = false;        for (int i = 0; i < pattern.length(); i++) {            if (pattern.charAt(i) == from) {                if (escaped) {                    translated.append(from);                    escaped = false;                } else {                    escaped = true;                }            } else if (pattern.charAt(i) == to) {                if (escaped) {                    translated.append(to).append(to);                    escaped = false;                } else {                    translated.append(to).append(to);                }            } else {                if (escaped) {                    translated.append(to);                    escaped = false;                }                translated.append(pattern.charAt(i));            }        }        return translated.toString();    }    /**     * Extends the <code>PathQueryNode</code> with merging capability. A     * <code>PathQueryNode</code> <code>n1</code> can be merged with another     * node <code>n2</code> in the following case:     * <p/>     * <code>n1</code> contains a location step at position <code>X</code> with     * a name test that matches any node and has the descending flag set. Where     * <code>X</code> &lt; number of location steps.     * <code>n2</code> contains no location step to match any node name and     * the sequence of name tests is the same as the sequence of name tests     * of <code>n1</code>.     * The merged node then contains a location step at position <code>X</code>     * with the name test of the location step at position <code>X+1</code> and     * the descending flag set.     * <p/>     * The following path patterns:<br/>     * <code>/foo/%/bar</code> OR <code>/foo/bar</code><br/>     * are merged into:<br/>     * <code>/foo//bar</code>.     * <p/>     * The path patterns:<br/>     * <code>/foo/%</code> AND NOT <code>/foo/%/%</code><br/>     * are merged into:<br/>     * <code>/foo/*</code>     */    private static class MergingPathQueryNode extends PathQueryNode {        /**         * The operation type of the parent node         */        private int operation;        /**         * Creates a new <code>MergingPathQueryNode</code> with the operation         * tpye of a parent node. <code>operation</code> must be one of:         * {@link org.apache.jackrabbit.core.query.QueryNode#TYPE_OR},         * {@link org.apache.jackrabbit.core.query.QueryNode#TYPE_AND} or         * {@link org.apache.jackrabbit.core.query.QueryNode#TYPE_NOT}.         *         * @param operation the operation type of the parent node.         */        MergingPathQueryNode(int operation) {            super(null);            if (operation != QueryNode.TYPE_OR && operation != QueryNode.TYPE_AND && operation != QueryNode.TYPE_NOT) {                throw new IllegalArgumentException("operation");            }            this.operation = operation;        }        /**         * Merges this node with a node from <code>nodes</code>. If a merge         * is not possible an NoSuchElementException is thrown.         *         * @param nodes the nodes to try to merge with.         * @return the merged array containing a merged version of this node.         */        MergingPathQueryNode[] doMerge(MergingPathQueryNode[] nodes) {            if (operation == QueryNode.TYPE_OR) {                return doOrMerge(nodes);            } else {                return doAndMerge(nodes);            }        }        /**         * Merges two nodes into a node which selects any child nodes of a         * given node.         * <p/>         * Example:<br/>         * The path patterns:<br/>         * <code>/foo/%</code> AND NOT <code>/foo/%/%</code><br/>         * are merged into:<br/>         * <code>/foo/*</code>         *         * @param nodes the nodes to merge with.         * @return the merged nodes.         */        private MergingPathQueryNode[] doAndMerge(MergingPathQueryNode[] nodes) {            if (operation == QueryNode.TYPE_AND) {                // check if there is an node with operation OP_AND_NOT                MergingPathQueryNode n = null;                for (int i = 0; i < nodes.length; i++) {                    if (nodes[i].operation == QueryNode.TYPE_NOT) {                        n = nodes[i];                        nodes[i] = this;                    }                }                if (n == null) {                    throw new NoSuchElementException("Merging not possible with any node");                } else {                    return n.doAndMerge(nodes);                }            }            // check if this node is valid as an operand            if (operands.size() < 3) {                throw new NoSuchElementException("Merging not possible");            }            int size = operands.size();            LocationStepQueryNode n1 = (LocationStepQueryNode) operands.get(size - 1);            LocationStepQueryNode n2 = (LocationStepQueryNode) operands.get(size - 2);            if (n1.getNameTest() != null || n2.getNameTest() != null                    || !n1.getIncludeDescendants() || !n2.getIncludeDescendants()) {                throw new NoSuchElementException("Merging not possible");            }            // find a node to merge with            MergingPathQueryNode matchedNode = null;            for (int i = 0; i < nodes.length; i++) {                if (nodes[i].operands.size() == operands.size() - 1) {                    boolean match = true;                    for (int j = 0; j < operands.size() - 1 && match; j++) {                        LocationStepQueryNode step = (LocationStepQueryNode) operands.get(j);                        LocationStepQueryNode other = (LocationStepQueryNode) nodes[i].operands.get(j);                        match &= (step.getNameTest() == null) ? other.getNameTest() == null : step.getNameTest().equals(other.getNameTest());                    }                    if (match) {                        matchedNode = nodes[i];                        break;                    }                }            }            if (matchedNode == null) {                throw new NoSuchElementException("Merging not possible with any node");            }            // change descendants flag to only match child nodes            // that's the result of the merge.            ((LocationStepQueryNode) matchedNode.operands.get(matchedNode.operands.size() - 1)).setIncludeDescendants(false);            return nodes;        }        /**         * Merges two nodes into one node selecting a node on the         * descendant-or-self axis.         * <p/>         * Example:<br/>         * The following path patterns:<br/>         * <code>/foo/%/bar</code> OR <code>/foo/bar</code><br/>         * are merged into:<br/>         * <code>/foo//bar</code>.         *         * @param nodes the node to merge.         * @return the merged nodes.         */        private MergingPathQueryNode[] doOrMerge(MergingPathQueryNode[] nodes) {            // compact this            MergingPathQueryNode compacted = new MergingPathQueryNode(QueryNode.TYPE_OR);            for (Iterator it = operands.iterator(); it.hasNext();) {                LocationStepQueryNode step = (LocationStepQueryNode) it.next();                if (step.getIncludeDescendants() && step.getNameTest() == null) {                    // check if has next                    if (it.hasNext()) {                        LocationStepQueryNode next = (LocationStepQueryNode) it.next();                        next.setIncludeDescendants(true);                        compacted.addPathStep(next);                    } else {                        compacted.addPathStep(step);                    }                } else {                    compacted.addPathStep(step);                }            }            MergingPathQueryNode matchedNode = null;            for (int i = 0; i < nodes.length; i++) {                // loop over the steps and compare the names                if (nodes[i].operands.size() == compacted.operands.size()) {                    boolean match = true;                    Iterator compactedSteps = compacted.operands.iterator();                    Iterator otherSteps = nodes[i].operands.iterator();                    while (match && compactedSteps.hasNext()) {                        LocationStepQueryNode n1 = (LocationStepQueryNode) compactedSteps.next();                        LocationStepQueryNode n2 = (LocationStepQueryNode) otherSteps.next();                        match &= (n1.getNameTest() == null) ? n2.getNameTest() == null : n1.getNameTest().equals(n2.getNameTest());                    }                    if (match) {                        matchedNode = nodes[i];                        break;                    }                }            }            if (matchedNode == null) {                throw new NoSuchElementException("Merging not possible with any node.");            }            // construct new list            List mergedList = new ArrayList(Arrays.asList(nodes));            mergedList.remove(matchedNode);            mergedList.add(compacted);            return (MergingPathQueryNode[]) mergedList.toArray(new MergingPathQueryNode[mergedList.size()]);        }        /**         * Returns <code>true</code> if this node needs merging; <code>false</code>         * otherwise.         *         * @return <code>true</code> if this node needs merging; <code>false</code>         *         otherwise.         */        boolean needsMerge() {            for (Iterator it = operands.iterator(); it.hasNext();) {                LocationStepQueryNode step = (LocationStepQueryNode) it.next();                if (step.getIncludeDescendants() && step.getNameTest() == null) {                    return true;                }            }            return false;        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?