package velocidoc; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Category; import org.apache.log4j.Priority; import java.io.File; import java.io.IOException; import java.io.LineNumberReader; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** A process to load the template-tree and detect particular patterns, currently * only the parameter redefinitions. Usage * java -classpath ... VelociAnalyzer --sourcepath _root-dir_ */ public class VelociAnalyzer { public Category log = Category.getInstance("velocidoc"); private static final ThreadLocal velociAnalyzers = new ThreadLocal(); boolean justticked = false; private String sourcePath = null; private String destinationPath = null; private String templatePath = null; private boolean debug = false; /** * default constructor, does nothing */ public VelociAnalyzer() { velociAnalyzers.set(this); log.setPriority(Priority.ERROR); } /** * Generates the documentation * */ public List execute() { velociAnalyzers.set(this); List errors = validateConfiguration(); tick(); if (errors.size() > 0) return errors; File sourceDirectory = new File(this.sourcePath); tick(); // parse the file RootDoc rootDoc = new RootDoc(sourceDirectory); try { for(MacroDoc macroDoc: rootDoc.getAllMacros()) { //System.out.println("MacroDoc: " + macroDoc); checkMacro(macroDoc); } System.err.flush(); } catch (Exception exception) { log.debug("validateConfiguration",exception); errors.add(exception.toString()); } return errors; } private void checkMacro(MacroDoc macroDoc) { for(String p: macroDoc.arguments) { if(p.startsWith("$")) p = p.substring(1); //System.out.println(" param: " + p); Pattern pattern = Pattern.compile("\\#set\\s*\\(\\s*\\$"+p+"\\s*=.*\\)"); LineNumberReader input =new LineNumberReader(new StringReader(macroDoc.getBody())); String line = null; int lineNum = 0; try { while((line=input.readLine())!=null) { Matcher matcher = pattern.matcher(line); if(matcher.find()) { System.err.print(macroDoc.getTemplate()); System.err.print(":" + (lineNum+macroDoc.getBodyBeginLine()) + ":"); System.err.println(" " + " parameter \""+p+"\" is set: " + line ); } lineNum++; } } catch (IOException e) { e.printStackTrace(); } } //System.out.println(" body: " + macroDoc.getBody().trim()); } private static final void parseArguments(String[] args, VelociAnalyzer compiler) { for (int i=0;i < args.length; i++) { String a = args[i]; if (a.equals("--sourcepath") || a.equals("-s")) { compiler.setSourcePath(args[i+1]); } else if (a.equals("-g") || a.equals("--debug")) { compiler.setDebug(true); } else if (a.equals("-h") || a.equals("--help")) { printUsage(); System.exit(0); // do nothing } } } public List validateConfiguration() { List errors = new ArrayList(); try { File src = new File(sourcePath); if (!src.exists()) errors.add("the source directory does not exist"); if (!src.canRead()) errors.add("the source directory is not writable"); } catch (Exception e) { log.debug("validateConfiguration",e); errors.add(e.toString()); } return errors; } public static void main(String[] args) { BasicConfigurator.configure(); VelociAnalyzer velociAnalyzer = new VelociAnalyzer(); parseArguments(args, velociAnalyzer); List errors = velociAnalyzer.execute(); if (errors.size() > 0) { printUsage(); System.err.println("errors"); System.err.println(errors); System.exit(1); } else { System.out.println("done"); System.exit(0); } } /** * prints the usage statement */ public static void printUsage() { StringBuffer sb = new StringBuffer(); sb.append("--------------------------------------------------"); sb.append("\nVelociAnalyzer - Based on Velocidoc v1.0 - http://velocidoc.sourceforge.net"); sb.append("\n By Trever M. Shick trevershick@yahoo.com"); sb.append("\n Other OS Projects:"); sb.append("\n http://xjr.sourceforge.net"); sb.append("\n http://jpilot-db.sourceforge.net"); sb.append("\n http://outline.sourceforge.net"); sb.append("\n--------------------------------------------------"); sb.append("\n\n"); sb.append("\n\nUsage:"); sb.append("\n --sourcepath [--debug] [--help]"); sb.append("\n\n -s [-g] [-h]"); sb.append("\n\n REQUIRED ARGUMENTS"); sb.append("\n --sourcepath|-s - the root directory of the templates"); sb.append("\n OPTIONAL ARGUMENTS"); sb.append("\n --debug|-g - print debug output"); sb.append("\n --help|-h - print this message\n\n"); System.err.println(sb); } public void tick() { System.out.print("."); justticked = true; } public void out(Object obj) { if (justticked) System.out.println("\n"); System.out.println(obj); justticked = false; } /** * */ public void setSourcePath(String newSourcePath) { sourcePath = newSourcePath; } /** * */ public String getSourcePath() { return sourcePath; } public void setDebug(boolean newDebug) { debug = newDebug; log.setPriority(Priority.DEBUG); } public static VelociAnalyzer get() { if (velociAnalyzers.get() == null) { velociAnalyzers.set(new VelociAnalyzer()); } return velociAnalyzers.get(); } public boolean isDebug() { return debug; } public static void debug(Object obj) { get().log.debug(obj); } public static void debug(Object o, Exception e) { get().log.debug(o,e); } public static boolean isDebugging() { return (get().log.getPriority() == Priority.DEBUG); } }