kaz         02/04/13 23:52:28

  Modified:    src/java/org/apache/maven/java/parser JavaParser.jjt
  Added:       src/java/org/apache/maven/java NodeViewVisitor.java
               src/test/org/apache/maven/java NodeViewVisitorTest.java
  Log:
  Added a variation of NodeViewMode visitor (pretty prints the AST) from
  Velocity to assist in debugging the AST.  Also added a unit test that
  invokes the parser which generates output that is captured in the unit
  test report.
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-maven/src/java/org/apache/maven/java/NodeViewVisitor.java
  
  Index: NodeViewVisitor.java
  ===================================================================
  package org.apache.maven.java;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.PrintWriter;
  
  import org.apache.maven.java.parser.Token;
  import org.apache.maven.java.parser.SimpleNode;
  
  /**
   * A visitor implementation that traverses the AST produced by the
   * JavaParser used in Maven and creates a visual structure of the AST.
   * This is primarily used for debugging, but it useful for documentation
   * as well.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Pete Kazmier</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: NodeViewVisitor.java,v 1.1 2002/04/14 06:52:28 kaz Exp $
   */
  public class NodeViewVisitor extends BaseVisitor
  {
      /** 
       * Specifies the current indent level of the tree.
       */
      private int indent = 0;
  
      /** 
       * Specifies the width of each indent level.
       */
      private int indentWidth = 2;
  
      /** 
       * Flag to indicate if the first token should be displayed.
       */
      private boolean showTokens = true;
  
      /** 
       * Specifies the location of the output.
       */
      private PrintWriter writer = new PrintWriter(System.out, true);
  
      /**
        * Display the type of node and optionally the first token.
        */
      public Object defaultVisit(SimpleNode node, Object data)
      {
          Token t;
          String tokens = "";
          
          if (showTokens)
          {
              t = node.getFirstToken();
              tokens = " -> " + t.image;
          }            
          
          writer.println(indentString() + node + tokens);
          ++indent;
          data = node.childrenAccept(this, data);
          --indent;
          return data;
      }
  
      /** 
       * Gets the indent width used when pretty printing the AST.
       * 
       * @return The current indent width.
       */
      public int getIndentWidth()
      {
          return indentWidth;
      }
  
      /** 
       * Sets the indent width used when pretty printing the AST.  The
       * default value for this property is <code>2</code>.
       * 
       * @param width The width of each level of indentation.
       */
      public void setIndentWidth(int indentWidth)
      {
          this.indentWidth = indentWidth;
      }
  
      /** 
       * Test to determine wheter or not tokens are to be displayed.
       * 
       * @return <code>true</code> if the first token is to be displayed,
       * otherise <code>false</code>.
       */
      public boolean isShowTokens()
      {
          return showTokens;
      }
  
      /** 
       * Sets a flag indicating that the first token should be displayed
       * in the pretty format.  The default is <code>true</code>.
       * 
       * @param show <code>true</code> enables this feature, while
       * <code>false</code> disables it.
       */
      public void setShowTokens(boolean showTokens)
      {
          this.showTokens = showTokens;
      }
  
      /** 
       * Gets the <code>PrintWriter</code> used to display the output.  If
       * a writer has not been specified, <code>System.out</code> is
       * wrapped in a <code>PrintWriter</code> and used as a default.
       * 
       * @return The <code>PrintWriter</code> used for output.
       */
      public PrintWriter getWriter()
      {
          return writer;
      }
  
      /** 
       * Sets the <code>PrintWriter</code> used to display the output.  If
       * a writer is not specified, <code>System.out</code> is wrapped in
       * a <code>PrintWriter</code> and used as a default.
       * 
       * @param writer The <code>PrintWriter</code> used for output.
       */
      public void setWriter(PrintWriter writer)
      {
          this.writer = writer;
      }
  
      /** 
       * Generates the appropriate whitespace for the current indent
       * level of the AST.  This enables the tree to be displayed using
       * indentation to make it easier to read.
       * 
       * @return String containing the appropriate whitespace for the
       * current indent level.
       */
      private String indentString()
      {
          StringBuffer sb = new StringBuffer();
          for (int i = 0; i < indent * indentWidth; ++i)
          {
              sb.append(" ");
          }
          return sb.toString();
      }
  }
  
  
  
  1.3       +0 -4      
jakarta-turbine-maven/src/java/org/apache/maven/java/parser/JavaParser.jjt
  
  Index: JavaParser.jjt
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/java/parser/JavaParser.jjt,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JavaParser.jjt    3 Mar 2002 15:32:26 -0000       1.2
  +++ JavaParser.jjt    14 Apr 2002 06:52:28 -0000      1.3
  @@ -55,10 +55,6 @@
   import java.io.InputStream;
   import java.io.ByteArrayInputStream;
   
  -/**
  - * @todo Take the node view visitor from Velocity so that it is easy
  - *       to see the syntax tree when making alterations to the grammar.
  - */
   public class JavaParser
   {
       /** 
  
  
  
  1.1                  
jakarta-turbine-maven/src/test/org/apache/maven/java/NodeViewVisitorTest.java
  
  Index: NodeViewVisitorTest.java
  ===================================================================
  package org.apache.maven.java;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /** 
   * Unit test for <code>NodeViewVisitor</code>.  The output of this test
   * generates an indented AST to standard out for debugging purposes.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]";>Pete Kazmier</a>
   * @version $Id: NodeViewVisitorTest.java,v 1.1 2002/04/14 06:52:28 kaz Exp $
   */
  public class NodeViewVisitorTest extends TestCase 
  {
      /** 
       * The name of the Java source file to parse.
       */
      private String TEST_FILE = null;
      
      public NodeViewVisitorTest(String testName)
      {
          super(testName);
      }        
  
      public static Test suite() 
      {
          return new TestSuite(NodeViewVisitorTest.class);
      }
  
      public void setUp() throws Exception
      {
          super.setUp();
          String baseDir = System.getProperty("basedir");
          assertNotNull("The system property basedir was not defined.", baseDir);
          String fs = System.getProperty("file.separator");
          assertNotNull("The system property file.separator was not defined.", fs);
          TEST_FILE = baseDir + fs + "src/test-java-parser/Input.java";
      }
     
      public void tearDown() 
      {
      }
  
      /** 
       * Test the <code>NodeViewVisitor</code>.
       */
      public void testNodeViewVisitor()
      {
          NodeViewVisitor v = new NodeViewVisitor();
          v.setShowTokens(true);
  
          SourceTool st = new SourceTool();
          st.setVisitor(v);
          
          try
          {
              st.parse(TEST_FILE);
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
  }
  
  
  


Reply via email to