/* Derby - Class TreeWalker Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.io.*; import java.sql.*; import java.util.*; import org.apache.derby.iapi.error.StandardException; import org.apache.derby.iapi.reference.ContextId; import org.apache.derby.iapi.services.context.ContextManager; import org.apache.derby.iapi.services.sanity.SanityManager; import org.apache.derby.iapi.sql.compile.Visitable; import org.apache.derby.iapi.sql.compile.Visitor; import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; import org.apache.derby.impl.jdbc.EmbedConnection; import org.apache.derby.impl.sql.compile.QueryTreeNode; /** * Little program to run the Derby parser on a text string and to print * the resulting abstract syntax tree produced by the parser. */ public class TreeWalker implements Visitor { /////////////////////////////////////////////////////////////////////////////////// // // CONSTANTS // /////////////////////////////////////////////////////////////////////////////////// private static final String DRIVER_NAME = "org.apache.derby.jdbc.EmbeddedDriver"; private static final String CONNECTION_URL = "jdbc:derby:dummy;create=true"; private static final String DERBY_DEBUG_SETTING = "derby.debug.true"; private static final String STOP_AFTER_PARSING = "StopAfterParsing"; private static final String STOPPED_AFTER_PARSING = "42Z55"; private static final String SHUTDOWN_URL = "jdbc:derby:;shutdown=true"; private static final String INDENTATION = " "; /////////////////////////////////////////////////////////////////////////////////// // // STATE // /////////////////////////////////////////////////////////////////////////////////// private PrintStream _printStream = System.out; private int _depth; private HashSet _visitedNodes; /////////////////////////////////////////////////////////////////////////////////// // // ENTRY POINT // /////////////////////////////////////////////////////////////////////////////////// /** * Entry point. Takes one argument: the string to be parsed. Along * the way, this program will create a dummy Derby database if it * doesn't already exist. */ public static void main( String[] args ) throws Exception { TreeWalker astParser = new TreeWalker(); astParser.execute( args ); } private void execute( String[] args ) throws Exception { System.setProperty( DERBY_DEBUG_SETTING, STOP_AFTER_PARSING ); Class.forName( DRIVER_NAME ); String text = args[ 0 ]; Connection conn = DriverManager.getConnection( CONNECTION_URL ); PreparedStatement ps = null; println( "Parsing:\n" + text ); try { ps = conn.prepareStatement( text ); } catch (SQLException se) { String sqlState = se.getSQLState(); if ( !STOPPED_AFTER_PARSING.equals( sqlState ) ) { throw se; } } ContextManager contextManager = ((EmbedConnection) conn).getContextManager(); LanguageConnectionContext lcc = (LanguageConnectionContext) contextManager.getContext( ContextId.LANG_CONNECTION); QueryTreeNode queryTree = (QueryTreeNode) lcc.getLastQueryTree(); _depth = 0; _visitedNodes = new HashSet(); visit( queryTree ); try { DriverManager.getConnection( SHUTDOWN_URL ); } catch (Exception e) {} } /////////////////////////////////////////////////////////////////////////////////// // // Visitor BEHAVIOR // /////////////////////////////////////////////////////////////////////////////////// public Visitable visit(Visitable node) throws StandardException { if ( !_visitedNodes.contains( node ) ) { _depth++; _visitedNodes.add( node ); StringBuffer buffer = new StringBuffer(); for ( int i = 0; i < _depth; i++ ) { buffer.append( INDENTATION ); } buffer.append( node.getClass().getName() ); println( buffer.toString() ); node.accept( this ); _depth--; } return node; } public boolean stopTraversal() { return false; } public boolean skipChildren(Visitable node) throws StandardException { return false; } /////////////////////////////////////////////////////////////////////////////////// // // MINIONS // /////////////////////////////////////////////////////////////////////////////////// private void println( String text ) { _printStream.println( text ); } }