Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZPA Toolkit export XML in CLI Mode #202

Open
ailtonbsj opened this issue Nov 18, 2024 · 1 comment
Open

ZPA Toolkit export XML in CLI Mode #202

ailtonbsj opened this issue Nov 18, 2024 · 1 comment

Comments

@ailtonbsj
Copy link

Hi, first of all congratulations for this awesome project.

My question is if there is any way to use toolkit in CLI mode? Passing a sql file as input and getting a AST as output in XML or JSON. Something like ZAP-CLI do, but just for exporting plsql.

@felipebz
Copy link
Owner

Thank you so much for your kind words and for appreciating the project! It really means a lot. 😊

Regarding your question, unfortunately, the functionality you mentioned is not currently available either in the toolkit or the CLI tool. That said, it's definitely possible to achieve this with some custom code.

For instance, in a Maven project, you can add the following dependency:

<dependency>
    <groupId>com.felipebz.zpa</groupId>
    <artifactId>zpa-core</artifactId>
    <version>3.6.0</version>
</dependency>

And here's a basic Java class that can be used to export the AST to XML:

import com.felipebz.flr.api.AstNode;
import com.felipebz.flr.api.Grammar;
import com.felipebz.flr.impl.Parser;
import com.felipebz.flr.impl.ast.AstXmlPrinter;
import org.sonar.plsqlopen.parser.PlSqlParser;
import org.sonar.plsqlopen.squid.PlSqlConfiguration;

import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        Parser<Grammar> parser = PlSqlParser.INSTANCE.create(new PlSqlConfiguration(StandardCharsets.UTF_8, false));
        AstNode astNode = parser.parse("begin null; end;");
        String xml = AstXmlPrinter.print(astNode);
        System.out.println(xml);
    }
}

(the parser.parse method accepts the code as a String or as a java.io.File instance)

Running this example will output the following XML:

<FILE_INPUT tokenValue="BEGIN" tokenLine="1" tokenColumn="0">
  <COMPILATION_UNIT tokenValue="BEGIN" tokenLine="1" tokenColumn="0">
    <ANONYMOUS_BLOCK tokenValue="BEGIN" tokenLine="1" tokenColumn="0">
      <BLOCK_STATEMENT tokenValue="BEGIN" tokenLine="1" tokenColumn="0">
        <STATEMENTS_SECTION tokenValue="BEGIN" tokenLine="1" tokenColumn="0">
          <BEGIN tokenValue="BEGIN" tokenLine="1" tokenColumn="0"/>
          <STATEMENTS tokenValue="NULL" tokenLine="1" tokenColumn="6">
            <STATEMENT tokenValue="NULL" tokenLine="1" tokenColumn="6">
              <NULL_STATEMENT tokenValue="NULL" tokenLine="1" tokenColumn="6">
                <NULL tokenValue="NULL" tokenLine="1" tokenColumn="6"/>
                <SEMICOLON tokenValue=";" tokenLine="1" tokenColumn="10"/>
              </NULL_STATEMENT>
            </STATEMENT>
          </STATEMENTS>
          <END tokenValue="END" tokenLine="1" tokenColumn="12"/>
          <SEMICOLON tokenValue=";" tokenLine="1" tokenColumn="15"/>
        </STATEMENTS_SECTION>
      </BLOCK_STATEMENT>
    </ANONYMOUS_BLOCK>
  </COMPILATION_UNIT>
  <EOF tokenValue="EOF" tokenLine="1" tokenColumn="16"/>
</FILE_INPUT>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants