[metaas-dev] Some code to contribute - Actionscript comment header generator
Acevedo, Alberto Mr CIV USA AMC
Alberto.Acevedo at us.Army.Mil
Wed Apr 9 14:52:53 UTC 2008
I implemented this Java utility to autogenerate comment headers for
actionscript source code. You specify the root path of your Flex project
and the utility will add comment headers to all the files (class, and
functions) found in the project. The utility by default do not overwrite
existing comments. By using the MetaAS ActonScript code generator it
made the implementation of this utility very easy. You can modify the
format of the comment headers based on your own requirements. I hope you
find this utility useful.
I found a problem with MettaAS and the super keyword. Hopefully this bug
will get fix in the next release of MetaAS.
Thanks for the great work on the MetaAS ActonScript code generator.
Alberto Acevedo
Computer Engineer
US Army CERDEC SED/ABSD
Fort Monmouth , New Jersey
package ascommentgenerator;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import uk.co.badgersinfoil.metaas.ActionScriptFactory;
import uk.co.badgersinfoil.metaas.ActionScriptParser;
import uk.co.badgersinfoil.metaas.ActionScriptProject;
import uk.co.badgersinfoil.metaas.dom.ASClassType;
import uk.co.badgersinfoil.metaas.dom.ASCompilationUnit;
import uk.co.badgersinfoil.metaas.dom.ASMethod;
import uk.co.badgersinfoil.metaas.dom.ASPackage;
import uk.co.badgersinfoil.metaas.dom.ASType;
/**
*
* @author Alberto.Acevedo
* @date 04/08/2008
* @Version 0.1
* @ revisions
* @log 04/08/2008 versionn 0.1 - when it was initially created.
*
* @description This utility add "MCS like" comment headers to
actionscript source code.
* Adding comments headers is tedious and this utility will make the
adding of comments painless
* because the developer only need to worry about adding the description
of the functions and parameters
* This utility is able to recursevely traverse the directory hierarchy
and add comments to all
* As files under the specified root Flex project folder or a specific
package. This utility is best used when
* implementing new AS classes. The developer write the signatures of
the functions and then use
* this utility to add all the comment headers. Be careful when setting
the forceOverwrite to true because
* it will overwrite or reset any existing comments. My advice is to
keep the default of not overwriting.
* @issues This utility is based on the metaas open source library. This
library complaints when
* it finds the super keyword. The workaround is to comment out the line
where the keyword is located
* and then run the utility.
* @usage
* 1) Open project in Netbeans
* 1) Change the root path according to your Flex project location
or package
* 2) Change the author according to the developer
* 3) to overwrite any existing comments set forceOverwrite to true.
Be careful!!
* 4) Always keep the latest AS files source code in SS. Checkout
only the files you want
* to add comment headers. If you by mistake overwrite any
existing comments the solution is
* to get the latest from SS.
* 5) run the project by clicking the Netbeans toolbar/Run/Run
main.
* 6) All AS files now have comments headers. The developer is
responsible
* for the filling out of the descriptions for each comment
header generated.
*
*/
public class Main {
// Change the root path according to your Flex project location
// rootpath can also be a spacific package.
static String rootPath = "D:/Flex/projects/checktree";
// Change the author according to the developer
static String author = "Alberto Acevedo";
// set to true to overwrite any existing comments. Default is false.
static boolean forceOverwrite = false;
static ActionScriptFactory fact = new ActionScriptFactory();
static ActionScriptProject proj = fact.newEmptyASProject(rootPath);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
File projectRootDir = new File(rootPath);
visitAllDirsAndFiles(projectRootDir);
proj.writeAll();
} catch (Exception ex) {
}
}// public static void main(String[] args)
/*
* Generates the header comment for the method.
*/
public static String generateMethodDescription(String methodName,
List paramList, String returnDescription) {
String description = "";
String parameterComment = "";
String methodParameters = "";
String parameter = null;
int len = paramList.size();
for (int index = 0; index < len; index++) {
parameter = (String) paramList.get(index).toString();
if (index == len - 1) {
methodParameters += parameter;
} else {
methodParameters += parameter + ",";
}
parameterComment += "<param name=\"" + parameter + "\">\nadd
description\n" +
"</param>\n";
}
description =
"Name: " + methodName + "(" + methodParameters + ")\n" +
"\n" +
"<summary>\n" +
"It performs the following steps:\n" +
"1) add step 1 if any\n" +
"2) add step 2 if any\n" +
"3) add step 3 if any\n" +
"</summary>\n" +
"\n" +
"Parameters:\n";
description += parameterComment;
description += "\n" +
"Returns:\n" +
"<returns>" + returnDescription + "</returns>\n";
return description;
}
/*
* Generates the header comment for the class.
*/
public static String generateClassDescription(String className,
String author) {
String description = "";
description =
"<summary>\n" +
"Name:" + className + "\n\n" +
"Description:\n" +
"Author:" + author + "\n" +
"Date:" + new Date().toString() + "\n" +
"Notes:\n" +
"</summary>\n";
return description;
}
/*
* Locates all the AS files under the specified root path.
*/
public static void visitAllDirsAndFiles(File dir) {
if (dir.isFile() && dir.getName().contains(".as")) {
processFile(dir);
}
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
visitAllDirsAndFiles(new File(dir, children[i]));
}
}
}
/*
// Process only directories under dir
public static void visitAllDirs(File dir) {
if (dir.isDirectory()) {
processFile(dir);
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllDirs(new File(dir, children[i]));
}
}
}
// Process only files under dir
public static void visitAllFiles(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllFiles(new File(dir, children[i]));
}
} else {
processFile(dir);
}
}
* */
/*
* for each AS file found process it and add comment headers.
*/
public static void processFile(File file) {
try {
FileInputStream in = new FileInputStream(file);
//FileInputStream in = new
FileInputStream("D:/Flex/projects/checktree/com/pricklythistle/common/re
nderers/TreeCheckBoxRenderer.as");
//FileInputStream in = new
FileInputStream("C:/test/TreeCheckBoxRenderer.as");
InputStreamReader reader = new InputStreamReader(in);
ActionScriptParser parser = fact.newParser();
ASCompilationUnit unit = parser.parse(reader);
ASClassType clazz = (ASClassType) unit.getType();
if (clazz.getDescriptionString() == null || forceOverwrite)
{
clazz.setDescription(generateClassDescription(clazz.getName(), "Alberto
Acevedo"));
}
proj.addCompilationUnit(unit);
ASPackage pkg = unit.getPackage();
ASType type = pkg.getType();
List methods = type.getMethods();
for (Iterator i = methods.iterator(); i.hasNext();) {
ASMethod meth = (ASMethod) i.next();
System.out.println(meth.getName());
List methodArg = meth.getArgs();
if (meth.getDescriptionString() == null ||
forceOverwrite) {
meth.setDescription(generateMethodDescription(meth.getName(), methodArg,
meth.getType()));
}
//for (int methodArgIndex
=0;methodArgIndex<methodArg.size();methodArgIndex++)
//{
//
System.out.println(methodArg.get(methodArgIndex));
// }
//meth.setDescription("testting");
//meth.addComment("hhhhh");
}
//proj.writeAll();
}//try
catch (Exception e) {
System.out.println(e.getMessage());
}
}// public static processFile(File file)
}
More information about the metaas-dev
mailing list