Fixed bug preventing the source from being displayed. Basic code clean-up as well.

git-svn-id: http://webgoat.googlecode.com/svn/trunk@77 4033779f-a91e-0410-96ef-6bf7bf53c507
This commit is contained in:
esheri3 2007-01-23 18:56:42 +00:00
parent 19a0566c47
commit 1ad2fd45d3

View File

@ -73,6 +73,14 @@ public class Course
} }
} }
/**
* Take an absolute file and return the filename.
*
* Ex. /etc/password becomes password
*
* @param s
* @return the file name
*/
private static String getFileName(String s) private static String getFileName(String s)
{ {
String fileName = new File(s).getName(); String fileName = new File(s).getName();
@ -90,27 +98,42 @@ public class Course
return fileName; return fileName;
} }
/**
* Take a class name and return the equivalent file name
*
* Ex. org.owasp.webgoat becomes org/owasp/webgoat.java
*
* @param className
* @return
*/
private static String getSourceFile(String className)
{
StringBuffer sb = new StringBuffer();
sb.append(className.replace(".", "/"));
sb.append(".java");
return sb.toString();
}
/** /**
* Description of the Method * Takes a file name and builds the class file name
* *
* @param fileName Description of the Parameter * @param fileName Description of the Parameter
* @param path Description of the Parameter * @param path Description of the Parameter
* @param ext Description of the Parameter
* @return Description of the Return Value * @return Description of the Return Value
*/ */
private static String clean(String fileName, String path, String ext) private static String getClassFile(String fileName, String path)
{ {
String ext = ".class";
fileName = fileName.trim(); fileName = fileName.trim();
// check if file is a directory /**
if (fileName.endsWith("/")) * We do not handle directories.
{ * We do not handle files with different extensions
return fileName; */
} if(fileName.endsWith("/") || !fileName.endsWith(ext))
// check if file is a class or java file
if (!fileName.endsWith(ext))
{ {
return null; return null;
} }
@ -119,16 +142,14 @@ public class Course
int index = fileName.indexOf("/WEB-INF/classes/"); int index = fileName.indexOf("/WEB-INF/classes/");
if (index != -1) if (index != -1)
{ {
fileName = fileName.substring(index + "/WEB-INF/classes/".length(), fileName = fileName.substring(index + "/WEB-INF/classes/".length(), fileName.length() - ext.length());
fileName.length() - ext.length());
fileName = fileName.replace('/', '.'); fileName = fileName.replace('/', '.');
fileName = fileName.replace('\\', '.'); fileName = fileName.replace('\\', '.');
} }
else else
{ {
// Strip off the leading path info // Strip off the leading path info
fileName = fileName.substring(path.length(), fileName.length() fileName = fileName.substring(path.length(), fileName.length() - ext.length());
- ext.length());
} }
return fileName; return fileName;
@ -294,6 +315,12 @@ public class Course
return getLessons(category, roles); return getLessons(category, roles);
} }
/**
* Load all of the filenames into a temporary cache
*
* @param context
* @param path
*/
private void loadFiles(ServletContext context, String path) private void loadFiles(ServletContext context, String path)
{ {
Set resourcePaths = context.getResourcePaths(path); Set resourcePaths = context.getResourcePaths(path);
@ -314,6 +341,11 @@ public class Course
} }
} }
/**
* Instantiate all the lesson objects into a cache
*
* @param path
*/
private void loadLessons(String path) private void loadLessons(String path)
{ {
Iterator itr = files.iterator(); Iterator itr = files.iterator();
@ -321,7 +353,7 @@ public class Course
while(itr.hasNext()) while(itr.hasNext())
{ {
String file = (String)itr.next(); String file = (String)itr.next();
String className = clean(file, path, ".class"); String className = getClassFile(file, path);
if(className != null) if(className != null)
{ {
@ -344,12 +376,15 @@ public class Course
} }
catch (Exception e) catch (Exception e)
{ {
System.out.println("Warning: " + e.getMessage()); //System.out.println("Warning: " + e.getMessage());
} }
} }
} }
} }
/**
* For each lesson, set the source file and lesson file
*/
private void loadResources() private void loadResources()
{ {
Iterator lessonItr = lessons.iterator(); Iterator lessonItr = lessons.iterator();
@ -358,27 +393,25 @@ public class Course
{ {
AbstractLesson lesson = (AbstractLesson)lessonItr.next(); AbstractLesson lesson = (AbstractLesson)lessonItr.next();
String className = lesson.getClass().getName(); String className = lesson.getClass().getName();
String classFile = getSourceFile(className);
Iterator fileItr = files.iterator(); Iterator fileItr = files.iterator();
while(fileItr.hasNext()) while(fileItr.hasNext())
{ {
String absoluteFile = (String)fileItr.next(); String absoluteFile = (String)fileItr.next();
String fileName = getFileName(absoluteFile); String fileName = getFileName(absoluteFile);
String javaName = clean(absoluteFile, "/", ".java");
String lessonName = clean(absoluteFile, "/", ".html");
if(javaName != null && className.endsWith(fileName)) if(absoluteFile.endsWith(classFile))
{ {
System.out.println("DEBUG: setting source file " + absoluteFile + " for lesson " + lesson.getClass().getName()); //System.out.println("Set source file for " + classFile);
lesson.setSourceFileName(absoluteFile); lesson.setSourceFileName(absoluteFile);
break;
} }
if(lessonName != null && className.endsWith(fileName)) if(absoluteFile.endsWith(".html") && className.endsWith(fileName))
{ {
System.out.println("DEBUG: setting lesson plan file " + absoluteFile + " for lesson " + lesson.getClass().getName()); //System.out.println("DEBUG: setting lesson plan file " + absoluteFile + " for lesson " + lesson.getClass().getName());
lesson.setLessonPlanFileName(absoluteFile); lesson.setLessonPlanFileName(absoluteFile);
break;
} }
} }
} }