<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.query.xwql.internal.hql;

import org.xwiki.query.jpql.analysis.DepthFirstAdapter;
import org.xwiki.query.jpql.node.*;
import org.xwiki.query.xwql.internal.QueryContext;

public class TreePrinter extends DepthFirstAdapter
{
    protected StringBuilder builder = new StringBuilder();

    private Printer printer;

    public TreePrinter(Printer printer)
    {
        this.printer = printer;
    }

    protected Printer getPrinter()
    {
        return printer;
    }

    protected QueryContext getContext()
    {
        return getPrinter().context;
    }

    @Override
    public String toString()
    {
        return builder.toString();
    }

    @Override
    public void defaultCase(Node node)
    {
        if (node instanceof Token) {
            Token token = (Token) node;
            builder.append(' ')
                .append(token.getText());
        }
    }

    @Override
    public void inASelectStatement(ASelectStatement node)
    {
        super.inASelectStatement(node);
        if (node.getWhereClause() == null) {
            // needed for #outAWhereClause
            node.setWhereClause(new AWhereClause());
        } else if (printer.where.length() > 0) {
            AWhereClause where = (AWhereClause) node.getWhereClause();
            // where := ( where )
            where.setConditionalExpression(
                new ASingleConditionalExpression(
                    new ASingleConditionalTerm(
                        new AConditionalFactor(null,
                            new ABrConditionalPrimary(new TLbr(),
                                where.getConditionalExpression(),
                                new TRbr())))));
        }
    }

    @Override
    public void caseARangeVariableDeclaration(ARangeVariableDeclaration node)
    {
        String from = node.getAbstractSchemaName().toString().trim();
        String alias = node.getVariable().toString().trim();

        if (getContext().getDocument(alias) != null) {
            from = "XWikiDocument";
        } else if (getContext().getObject(alias) != null) {
            from = "BaseObject";
        }
        builder.append(' ').append(from)
            .append(" as ").append(alias);
    }

    @Override
    public void outAFromClause(AFromClause node)<commit_msg>XWIKI-13562: Add support for "Space" table name alias in XWQL to hql converter<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.query.xwql.internal.hql;

import org.xwiki.query.jpql.analysis.DepthFirstAdapter;
import org.xwiki.query.jpql.node.*;
import org.xwiki.query.xwql.internal.QueryContext;

public class TreePrinter extends DepthFirstAdapter
{
    protected StringBuilder builder = new StringBuilder();

    private Printer printer;

    public TreePrinter(Printer printer)
    {
        this.printer = printer;
    }

    protected Printer getPrinter()
    {
        return printer;
    }

    protected QueryContext getContext()
    {
        return getPrinter().context;
    }

    @Override
    public String toString()
    {
        return builder.toString();
    }

    @Override
    public void defaultCase(Node node)
    {
        if (node instanceof Token) {
            Token token = (Token) node;
            builder.append(' ')
                .append(token.getText());
        }
    }

    @Override
    public void inASelectStatement(ASelectStatement node)
    {
        super.inASelectStatement(node);
        if (node.getWhereClause() == null) {
            // needed for #outAWhereClause
            node.setWhereClause(new AWhereClause());
        } else if (printer.where.length() > 0) {
            AWhereClause where = (AWhereClause) node.getWhereClause();
            // where := ( where )
            where.setConditionalExpression(
                new ASingleConditionalExpression(
                    new ASingleConditionalTerm(
                        new AConditionalFactor(null,
                            new ABrConditionalPrimary(new TLbr(),
                                where.getConditionalExpression(),
                                new TRbr())))));
        }
    }

    @Override
    public void caseARangeVariableDeclaration(ARangeVariableDeclaration node)
    {
        String from = node.getAbstractSchemaName().toString().trim();
        String alias = node.getVariable().toString().trim();

        if (getContext().getDocument(alias) != null) {
            from = "XWikiDocument";
        } else if (getContext().getObject(alias) != null) {
            from = "BaseObject";
        } else if (from.equals("Space")) {
            from = "XWikiSpace";
        }
        builder.append(' ').append(from)
            .append(" as ").append(alias);
    }

    @Override
    public void outAFromClause(AFromClause node)
{'fraction_non_alphanumeric': 0.09035315700963155, 'fraction_numerical': 0.00504510013759364, 'mean_word_length': 2.370427614631633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4338727', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1676, 'n_words': 540}