queryformat.java

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

JAVA
496
字号
        for (int i = 0; i < steps.length; i++) {            sb.append(slash);            steps[i].accept(this, sb);            slash = "/";        }        return sb;    }    public Object visit(LocationStepQueryNode node, Object data) {        StringBuffer sb = (StringBuffer) data;        if (node.getIncludeDescendants()) {            sb.append('/');        }        final QName[] nodeType = new QName[1];        node.acceptOperands(new DefaultQueryNodeVisitor() {            public Object visit(NodeTypeQueryNode node, Object data) {                nodeType[0] = node.getValue();                return data;            }        }, null);        if (nodeType[0] != null) {            sb.append("element(");        }        if (node.getNameTest() == null) {            sb.append("*");        } else {            try {                if (node.getNameTest().getLocalName().length() == 0) {                    NameFormat.format(XPathQueryBuilder.JCR_ROOT, resolver, sb);                } else {                    NameFormat.format(ISO9075.encode(node.getNameTest()), resolver, sb);                }            } catch (NoPrefixDeclaredException e) {                exceptions.add(e);            }        }        if (nodeType[0] != null) {            sb.append(", ");            try {                NameFormat.format(ISO9075.encode(nodeType[0]), resolver, sb);            } catch (NoPrefixDeclaredException e) {                exceptions.add(e);            }            sb.append(")");        }        if (node.getIndex() != LocationStepQueryNode.NONE) {            sb.append('[').append(node.getIndex()).append(']');        }        QueryNode[] predicates = node.getPredicates();        for (int i = 0; i < predicates.length; i++) {            // ignore node type query nodes            if (predicates[i].getType() == QueryNode.TYPE_NODETYPE) {                continue;            }            sb.append('[');            predicates[i].accept(this, sb);            sb.append(']');        }        return sb;    }    public Object visit(DerefQueryNode node, Object data) {        StringBuffer sb = (StringBuffer) data;        try {            NameFormat.format(XPathQueryBuilder.JCR_DEREF, resolver, sb);            sb.append("(@");            NameFormat.format(ISO9075.encode(node.getRefProperty()), resolver, sb);            sb.append(", '");            if (node.getNameTest() == null) {                sb.append("*");            } else {                NameFormat.format(ISO9075.encode(node.getNameTest()), resolver, sb);            }            sb.append("')");        } catch (NoPrefixDeclaredException e) {            exceptions.add(e);        }        return sb;    }    public Object visit(RelationQueryNode node, Object data) {        StringBuffer sb = (StringBuffer) data;        try {            StringBuffer propPath = new StringBuffer();            // only encode if not position function            Path relPath = node.getRelativePath();            if (relPath.getNameElement().getName().equals(XPathQueryBuilder.FN_POSITION_FULL)) {                NameFormat.format(XPathQueryBuilder.FN_POSITION_FULL, resolver, propPath);            } else {                Path.PathElement[] elements = relPath.getElements();                String slash = "";                for (int i = 0; i < elements.length; i++) {                    propPath.append(slash);                    slash = "/";                    if (i == elements.length - 1) {                        propPath.append("@");                    }                    if (elements[i].getName().equals(RelationQueryNode.STAR_NAME_TEST)) {                        propPath.append("*");                    } else {                        NameFormat.format(ISO9075.encode(elements[i].getName()), resolver, propPath);                    }                    if (elements[i].getIndex() != 0) {                        propPath.append("[").append(elements[i].getIndex()).append("]");                    }                }            }            // surround name with property function            node.acceptOperands(this, propPath);            if (node.getOperation() == OPERATION_EQ_VALUE) {                sb.append(propPath).append(" eq ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_EQ_GENERAL) {                sb.append(propPath).append(" = ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_GE_GENERAL) {                sb.append(propPath).append(" >= ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_GE_VALUE) {                sb.append(propPath).append(" ge ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_GT_GENERAL) {                sb.append(propPath).append(" > ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_GT_VALUE) {                sb.append(propPath).append(" gt ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_LE_GENERAL) {                sb.append(propPath).append(" <= ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_LE_VALUE) {                sb.append(propPath).append(" le ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_LIKE) {                NameFormat.format(XPathQueryBuilder.JCR_LIKE, resolver, sb);                sb.append("(").append(propPath).append(", ");                appendValue(node, sb);                sb.append(")");            } else if (node.getOperation() == OPERATION_LT_GENERAL) {                sb.append(propPath).append(" < ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_LT_VALUE) {                sb.append(propPath).append(" lt ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_NE_GENERAL) {                sb.append(propPath).append(" != ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_NE_VALUE) {                sb.append(propPath).append(" ne ");                appendValue(node, sb);            } else if (node.getOperation() == OPERATION_NULL) {                NameFormat.format(XPathQueryBuilder.FN_NOT, resolver, sb);                sb.append("(").append(propPath).append(")");            } else if (node.getOperation() == OPERATION_NOT_NULL) {                sb.append(propPath);            } else {                exceptions.add(new InvalidQueryException("Invalid operation: " + node.getOperation()));            }        } catch (NoPrefixDeclaredException e) {            exceptions.add(e);        }        return sb;    }    public Object visit(OrderQueryNode node, Object data) {        StringBuffer sb = (StringBuffer) data;        sb.append(" order by");        OrderQueryNode.OrderSpec[] specs = node.getOrderSpecs();        String comma = "";        try {            for (int i = 0; i < specs.length; i++) {                sb.append(comma);                QName prop = ISO9075.encode(specs[i].getProperty());                sb.append(" @");                NameFormat.format(prop, resolver, sb);                if (!specs[i].isAscending()) {                    sb.append(" descending");                }                comma = ",";            }        } catch (NoPrefixDeclaredException e) {            exceptions.add(e);        }        return data;    }    public Object visit(PropertyFunctionQueryNode node, Object data) {        StringBuffer sb = (StringBuffer) data;        String functionName = node.getFunctionName();        try {            if (functionName.equals(PropertyFunctionQueryNode.LOWER_CASE)) {                sb.insert(0, NameFormat.format(XPathQueryBuilder.FN_LOWER_CASE, resolver) + "(");                sb.append(")");            } else if (functionName.equals(PropertyFunctionQueryNode.UPPER_CASE)) {                sb.insert(0, NameFormat.format(XPathQueryBuilder.FN_UPPER_CASE, resolver) + "(");                sb.append(")");            } else {                exceptions.add(new InvalidQueryException("Unsupported function: " + functionName));            }        } catch (NoPrefixDeclaredException e) {            exceptions.add(e);        }        return sb;    }    //----------------------------< internal >----------------------------------    /**     * Appends the value of a relation node to the <code>StringBuffer</code>     * <code>sb</code>.     *     * @param node the relation node.     * @param b    where to append the value.     * @throws NoPrefixDeclaredException if a prefix declaration is missing for     *                                   a namespace URI.     */    private void appendValue(RelationQueryNode node, StringBuffer b)            throws NoPrefixDeclaredException {        if (node.getValueType() == TYPE_LONG) {            b.append(node.getLongValue());        } else if (node.getValueType() == TYPE_DOUBLE) {            b.append(node.getDoubleValue());        } else if (node.getValueType() == TYPE_STRING) {            b.append("'").append(node.getStringValue().replaceAll("'", "''")).append("'");        } else if (node.getValueType() == TYPE_DATE || node.getValueType() == TYPE_TIMESTAMP) {            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));            cal.setTime(node.getDateValue());            NameFormat.format(XPathQueryBuilder.XS_DATETIME, resolver, b);            b.append("('").append(ISO8601.format(cal)).append("')");        } else if (node.getValueType() == TYPE_POSITION) {            if (node.getPositionValue() == LocationStepQueryNode.LAST) {                b.append("last()");            } else {                b.append(node.getPositionValue());            }        } else {            exceptions.add(new InvalidQueryException("Invalid type: " + node.getValueType()));        }    }}

⌨️ 快捷键说明

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