public Object visit(QueryRootNode node, Object data) throws RepositoryException {
        StringBuilder sb = (StringBuilder) data;
        try {
            sb.append("SELECT");

            InternalQName[] selectProps = node.getSelectProperties();
            if (selectProps.length == 0) {
                sb.append(" *");
            } else {
                String comma = "";
                for (int i = 0; i < selectProps.length; i++) {
                    sb.append(comma).append(" ");
                    appendName(selectProps[i], resolver, sb);
                    comma = ",";
                }
            }

            sb.append(" FROM");

            // node type restrictions are within predicates of location nodes
            // therefore we write the where clause first to a temp string to
            // collect the node types.
            StringBuilder tmp = new StringBuilder();
            LocationStepQueryNode[] steps = node.getLocationNode().getPathSteps();
            QueryNode[] predicates = steps[steps.length - 1].getPredicates();
            // are there any relevant predicates?
            for (int i = 0; i < predicates.length; i++) {
                if (predicates[i].getType() != QueryNode.TYPE_NODETYPE) {
                    tmp.append(" WHERE ");
                }
            }
            String and = "";
            for (int i = 0; i < predicates.length; i++) {
                if (predicates[i].getType() != QueryNode.TYPE_NODETYPE) {
                    tmp.append(and);
                    and = " AND ";
                }
                predicates[i].accept(this, tmp);
            }

            // node types have been collected by now
            String comma = "";
            int ntCount = 0;
            for (Iterator<InternalQName> it = nodeTypes.iterator(); it.hasNext(); ntCount++) {
               InternalQName nt = it.next();
                sb.append(comma).append(" ");
                appendName(nt, resolver, sb);
                comma = ",";
            }

            if (ntCount == 0) {
                sb.append(" ");
                sb.append(resolver.createJCRName(Constants.NT_BASE).getAsString());
            }

            // append WHERE clause
            sb.append(tmp.toString());

            if (steps.length == 2
                    && steps[1].getIncludeDescendants()
                    && steps[1].getNameTest() == null) {
                // then this query selects all paths
            } else if (steps.length == 1
                    && steps[0].getIncludeDescendants()
                    && steps[0].getNameTest() == null) {
                // then this query selects all paths
            } else {
                if (predicates.length > 0) {
                    sb.append(" AND ");
                } else {
                    sb.append(" WHERE ");
                }
                node.getLocationNode().accept(this, sb);
            }
        } catch (NamespaceException e) {
            exceptions.add(e);
        }

        if (node.getOrderNode() != null) {
            node.getOrderNode().accept(this, sb);
        }

        return sb;
    }
public Object visit ( QueryRootNode node , Object data ) throws RepositoryException { StringBuilder sb = ( StringBuilder ) data ; try { sb . append ( "SELECT" ) ; InternalQName [ ] selectProps = node . getSelectProperties ( ) ; if ( selectProps . length == 0 ) { sb . append ( " *" ) ; } else { String comma = "" ; for ( int i = 0 ; i < selectProps . length ; i ++ ) { sb . append ( comma ) . append ( " " ) ; appendName ( selectProps [ i ] , resolver , sb ) ; comma = "," ; } } sb . append ( " FROM" ) ; StringBuilder tmp = new StringBuilder ( ) ; LocationStepQueryNode [ ] steps = node . getLocationNode ( ) . getPathSteps ( ) ; QueryNode [ ] predicates = steps [ steps . length - 1 ] . getPredicates ( ) ; for ( int i = 0 ; i < predicates . length ; i ++ ) { if ( predicates [ i ] . getType ( ) != QueryNode . TYPE_NODETYPE ) { tmp . append ( " WHERE " ) ; } } String and = "" ; for ( int i = 0 ; i < predicates . length ; i ++ ) { if ( predicates [ i ] . getType ( ) != QueryNode . TYPE_NODETYPE ) { tmp . append ( and ) ; and = " AND " ; } predicates [ i ] . accept ( this , tmp ) ; } String comma = "" ; int ntCount = 0 ; for ( Iterator < InternalQName > it = nodeTypes . iterator ( ) ; it . hasNext ( ) ; ntCount ++ ) { InternalQName nt = it . next ( ) ; sb . append ( comma ) . append ( " " ) ; appendName ( nt , resolver , sb ) ; comma = "," ; } if ( ntCount == 0 ) { sb . append ( " " ) ; sb . append ( resolver . createJCRName ( Constants . NT_BASE ) . getAsString ( ) ) ; } sb . append ( tmp . toString ( ) ) ; if ( steps . length == 2 && steps [ 1 ] . getIncludeDescendants ( ) && steps [ 1 ] . getNameTest ( ) == null ) { } else if ( steps . length == 1 && steps [ 0 ] . getIncludeDescendants ( ) && steps [ 0 ] . getNameTest ( ) == null ) { } else { if ( predicates . length > 0 ) { sb . append ( " AND " ) ; } else { sb . append ( " WHERE " ) ; } node . getLocationNode ( ) . accept ( this , sb ) ; } } catch ( NamespaceException e ) { exceptions . add ( e ) ; } if ( node . getOrderNode ( ) != null ) { node . getOrderNode ( ) . accept ( this , sb ) ; } return sb ; }
-------------< QueryNodeVisitor interface >-------------------------------