JeSSVisitor

 

package edu.colby.JeSS.util;

 

import org.eclipse.jdt.core.dom.*;

 

import edu.colby.JeSS.scanner.VisitorManager;

 

 

/**

 * This is the super class for all Visitors in the JeSS scanner.

 * This is created to allow easy extensibility to the JeSS plugin.

 * Simply create a sub-type of JeSSVisitor to find patterns in an

 * AST and then use the reportProblem() method of JeSSVisitor to

 * create a security marker.

 * @author Russell Spitler

 * Mar 24, 2005

 */

public class JeSSVisitor extends ASTVisitor {

 

            private VisitorManager vManager;

            //store a reference to the Visitor manager for error reporting

            public JeSSVisitor(VisitorManager vManager){

                        this.vManager = vManager;

            }

            /**

             * Used to report a problem and create a security

             * marker for the security bug.  This method uses

             * the reportProblem() method of the VisitorManager.

             * @param node - the root of the problem

             * @param errorMessage - the message associated with the error

             */

            public void reportProblem(ASTNode node, String errorMessage){

 

                        Location loc = new Location();

 

                        CompilationUnit compUnit = (CompilationUnit) node.getRoot();

 

                        loc.setLineNumber(compUnit.lineNumber(node.getStartPosition()));

                        loc.setCharEnd(node.getStartPosition()+node.getLength());

                        loc.setCharStart(node.getStartPosition());

                        loc.setFile(vManager.getResource());

 

                        vManager.reportProblem(errorMessage, loc, true);

            }

            /**

             * This helper method parses a class name from the output of

             * the standard toString() method in the TypeDeclaration

             * AST node. This method relies upon the standard format of

             * TypeDeclaration[class CLASSNAME DECLARATIONS].  The name

             * is converted to user readable form "class CLASSNAME"

             * @param string -  toString() from a TypeDeclaration AST node

             * @return the name in user readable form

             */

            protected String parseClassName(String string){

 

                //start after the first [

                int startIndex = string.indexOf("[")+1;

 

                //end after the first space following "class "

                int endIndex = string.indexOf(" ", startIndex+7);

                string = string.substring(startIndex, endIndex);

                return string;

            }

            /**

             * This method parses a user readable name from the

             * toString() output of FieldDeclaration and MethodDeclaration.

             * This method relies on the standard format

             *

             * @param string

             * @return

             */

            protected String parseStandardName(String string){

 

                string = string.substring(string.indexOf(" "), (string.length()-1));

                return string;

            }

           

}