| 1 | /** |
| 2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
| 3 | */ |
| 4 | package org.sourceforge.xradar; |
| 5 | |
| 6 | import java.io.File; |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.Arrays; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Iterator; |
| 11 | import java.util.List; |
| 12 | import java.util.Map; |
| 13 | |
| 14 | import org.sourceforge.xradar.config.ReleaseData; |
| 15 | import org.sourceforge.xradar.config.ReleaseDataLoader; |
| 16 | import org.sourceforge.xradar.statics.MergeProduct; |
| 17 | import org.sourceforge.xradar.statics.Param; |
| 18 | import org.sourceforge.xradar.statics.Report; |
| 19 | import org.sourceforge.xradar.statics.Statics; |
| 20 | import org.sourceforge.xradar.statics.renderers.XslHtmlWebsite; |
| 21 | |
| 22 | /** |
| 23 | * |
| 24 | * @author Romain PELISSE, belaran@gmail.com |
| 25 | * @author Zahir Nuckchady, zahir.nu@gmail.com |
| 26 | */ |
| 27 | public class XRadarStaticsCmdLine { |
| 28 | |
| 29 | /** |
| 30 | * Command line to use xradar: |
| 31 | * $ java -cp org.sourceforge.xradar.Statics --pmd pmd.xml --jpdepend ... --target --offline --config etc/radar-config |
| 32 | * |
| 33 | * @param args |
| 34 | * @throws XRadarException |
| 35 | */ |
| 36 | public static void runXRadarStatics(String[] args) throws XRadarException { |
| 37 | // Dealing with the arguments |
| 38 | List<Report> reports = new ArrayList<Report>(); |
| 39 | XRadarParameters parameters = XRadarStaticsCmdLine.extractArguments(reports,args); |
| 40 | |
| 41 | // Is target a directory and an absolute path ? Target MUST BE an absolute path... |
| 42 | parameters.setTarget(new File(parameters.getTarget()).getAbsolutePath()); |
| 43 | |
| 44 | // Settings... |
| 45 | Param radarConfig = new Param(Statics.XRADAR_CONFIG_FILE,parameters.getXradarConfig()); |
| 46 | ReleaseData releaseData = new ReleaseDataLoader("testproject/etc/release1.properties").load(); |
| 47 | |
| 48 | XslHtmlWebsite staticEngine = new XslHtmlWebsite(); |
| 49 | // If Offline is specified in the arguments, the xml catalog will be used |
| 50 | staticEngine.initializedXmlCatalog(parameters.isOffline()); |
| 51 | staticEngine.setDocsHome(parameters.getTarget()); |
| 52 | staticEngine.setDebug(parameters.isDebug()); |
| 53 | staticEngine.setClassesDirectory(parameters.getClassesDirectory()); |
| 54 | |
| 55 | // Add reports to the statics instance |
| 56 | for ( Report report : reports ) { |
| 57 | staticEngine.addReport(report); |
| 58 | } |
| 59 | |
| 60 | // Executing the merging and then the PostProcessing |
| 61 | Map<String,Param> params = new HashMap<String,Param>(); |
| 62 | params.put(Statics.JAVADOC_ENABLED,new Param(Statics.JAVADOC_ENABLED,String.valueOf(parameters.isJavadocEnabled()))); |
| 63 | params.put(Statics.JAVADOC_ROOT,new Param(Statics.JAVADOC_ROOT,parameters.getJavadocRoot())); |
| 64 | params.put(Statics.JAVA2HTML_ROOT,new Param(Statics.JAVA2HTML_ROOT,parameters.getJava2htmlRoot())); |
| 65 | params.put(Statics.XRADAR_CONFIG_FILE,radarConfig); |
| 66 | // Merge and the post process |
| 67 | MergeProduct products = staticEngine.executePostProcessing(staticEngine.executeMerge(),releaseData,radarConfig); |
| 68 | staticEngine.generateHtmlReport(products, params); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * <p>Checks if all the necessary arguments required to generate the XRadar |
| 74 | * report are present.</p> |
| 75 | * @param list, contains all the arguments in the command line |
| 76 | * @throws IllegalArgumentException if a required argument is missing |
| 77 | **/ |
| 78 | private static void checkCompulsoryArgs(List<String> list) { |
| 79 | String [] compulsoryArgs = new String [] {"--target","--properties","--xradarConfig"}; |
| 80 | //String [] classesGiven = new String [] {"--classes","--target","--properties","--xradarConfig"}; |
| 81 | for(String argument :compulsoryArgs){ |
| 82 | if( ! list.contains(argument)) |
| 83 | throw new IllegalArgumentException(argument + " is a compulsory argument in order to generate the XRadar report"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private static void checkJdependClasses(List<String> list) { |
| 88 | boolean classes = false, jdepend = false; |
| 89 | Iterator <String> iterator = list.iterator(); |
| 90 | while ( iterator.hasNext() ) { |
| 91 | String arg = iterator.next(); |
| 92 | if (arg.contains("--classes")){ |
| 93 | classes = true; |
| 94 | } |
| 95 | if ( arg.contains("--jdepend")){ |
| 96 | jdepend = true; |
| 97 | } |
| 98 | } |
| 99 | if (!( classes || jdepend ) ){ |
| 100 | throw new IllegalArgumentException("If you do not have a jdepend report already generated, XRadar can run JDepend for you if you provide it the directory where your project directory is."); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * <p>Extract all the arguments provided in the command line. |
| 106 | * Before generating the reports, we verify if the arguments and the files exist.</p> |
| 107 | * @param report, which contains all the arguments provided |
| 108 | * @param args, which represents the arguments provided in the command line |
| 109 | * @throws IllegalArgumentException when the file specified does not exist, a wrong file is specified or the command does not exist |
| 110 | **/ |
| 111 | private static XRadarParameters extractArguments(List<Report> reports,String[] args) { |
| 112 | |
| 113 | List<String> list = Arrays.asList( args ); |
| 114 | XRadarParameters xradarParameters = new XRadarParameters(); |
| 115 | checkCompulsoryArgs(list); |
| 116 | checkJdependClasses(list); |
| 117 | Iterator <String> iterator = list.iterator(); |
| 118 | List<CommandLineArgument> cmdArguments = createArgsReferences() ; |
| 119 | |
| 120 | while ( iterator.hasNext() ) { |
| 121 | String arg = iterator.next(); |
| 122 | |
| 123 | if ( !arg.startsWith("--") ) { // If no command is specified (--pmd, --jdepend), reports cannot be generated |
| 124 | throw new IllegalArgumentException(arg+ " is not a valid command. \nCommand should always start with --"); |
| 125 | } |
| 126 | // Test if the arguments exist. E.x: pmd, checkstyle ... |
| 127 | // FIXME: this is ugly ! |
| 128 | CommandLineArgument cmd = null; |
| 129 | for (CommandLineArgument tempCmd : cmdArguments ) { |
| 130 | if ( tempCmd.getArgument().equals(arg) ) { |
| 131 | cmd = tempCmd; |
| 132 | } |
| 133 | } |
| 134 | // If the command does not exist, throws an exception |
| 135 | if (cmd == null) |
| 136 | throw new IllegalArgumentException("The " +arg+ " command is unknown to Xradar"); |
| 137 | // Command found, test if value for argument is provided |
| 138 | if ( cmd.getNbValue() == 0 ) { |
| 139 | if ( arg.contains("offline") ){ |
| 140 | xradarParameters.setOffline(true); |
| 141 | } |
| 142 | if ( arg.contains("online") ){ |
| 143 | xradarParameters.setOffline(false); |
| 144 | } |
| 145 | if ( arg.contains("debug") ){ |
| 146 | xradarParameters.setDebug(true); |
| 147 | } |
| 148 | if ( arg.contains("javadocEnabled") ){ |
| 149 | xradarParameters.setJavadocEnabled(true); |
| 150 | } |
| 151 | } |
| 152 | else if ( (cmd.getNbValue() == 1) && (iterator.hasNext()) ) { |
| 153 | String value = iterator.next(); |
| 154 | // If value for the argument is absent, throws an Exception |
| 155 | if ( value.startsWith("--") ) { |
| 156 | throw new IllegalArgumentException ("Missing " + arg + " value for " + arg +" command"); |
| 157 | } |
| 158 | // Verify if the value is a file path and if the file exists |
| 159 | fileExists(value); |
| 160 | if ( cmd.isValueAFile(0) ) { |
| 161 | File file = new File(value); |
| 162 | if ( ! file.exists() && |
| 163 | ( "--target".equals(arg) && ! file.mkdir() ) ) { // No target directory ? let's try to create it |
| 164 | throw new IllegalArgumentException("File " + value + " does not exist."); |
| 165 | } |
| 166 | } |
| 167 | if ( arg.contains("javadocRoot") ){ |
| 168 | xradarParameters.setJavadocRoot(value); |
| 169 | } |
| 170 | else if ( arg.contains("java2htmlRoot") ){ |
| 171 | xradarParameters.setJava2htmlRoot(value); |
| 172 | } |
| 173 | else if ( arg.contains("properties") ){ |
| 174 | xradarParameters.setProperties(value); |
| 175 | } |
| 176 | else if ( arg.contains("xradarConfig") ){ |
| 177 | xradarParameters.setXradarConfig(value); |
| 178 | } |
| 179 | else if ( arg.contains("target") ){ |
| 180 | xradarParameters.setTarget(value); |
| 181 | } |
| 182 | else if ( arg.contains("classes") ){ |
| 183 | xradarParameters.setClassesDirectory(value); |
| 184 | } |
| 185 | else {// Create report if arguments are valid |
| 186 | reports.add(new Report(arg.substring(2, arg.length()),value)); |
| 187 | } |
| 188 | } |
| 189 | else { |
| 190 | throw new IllegalArgumentException ("Missing value for argument " + arg + "\nRight statement should be: \"" + arg + " value\""); |
| 191 | } |
| 192 | } |
| 193 | return xradarParameters; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | *Verifies if the file exists |
| 198 | *@param fileName and Command |
| 199 | */ |
| 200 | private static void fileExists (String fileName) { |
| 201 | File file = new File(fileName); |
| 202 | if ( ! file.exists()){ |
| 203 | throw new IllegalArgumentException("File " + file + " does not exist"); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * <p>Creates a List with all the possible arguments provided in the command line.</p> |
| 209 | **/ |
| 210 | private static List<CommandLineArgument> createArgsReferences() { |
| 211 | List<CommandLineArgument> cmdArguments = new ArrayList<CommandLineArgument>(); |
| 212 | cmdArguments.add(new CommandLineArgument("--pmd",1,true)); |
| 213 | cmdArguments.add(new CommandLineArgument("--checkstyle",1,true)); |
| 214 | cmdArguments.add(new CommandLineArgument("--jdepend",1,true)); |
| 215 | cmdArguments.add(new CommandLineArgument("--cpd",1,true)); |
| 216 | cmdArguments.add(new CommandLineArgument("--ckjm",1,true)); |
| 217 | cmdArguments.add(new CommandLineArgument("--javancss",1,true)); |
| 218 | cmdArguments.add(new CommandLineArgument("--findbugs",1,true)); |
| 219 | cmdArguments.add(new CommandLineArgument("--target",1,true)); |
| 220 | cmdArguments.add(new CommandLineArgument("--properties",1,true)); |
| 221 | cmdArguments.add(new CommandLineArgument("--xradarConfig",1,true)); |
| 222 | cmdArguments.add(new CommandLineArgument("--classes",1,true)); |
| 223 | cmdArguments.add(new CommandLineArgument("--javadocRoot",1,true)); |
| 224 | cmdArguments.add(new CommandLineArgument("--java2htmlRoot",1,true)); |
| 225 | cmdArguments.add(new CommandLineArgument("--javadocEnabled",0,false)); |
| 226 | cmdArguments.add(new CommandLineArgument("--offline",0,false)); |
| 227 | cmdArguments.add(new CommandLineArgument("--online",0,false)); |
| 228 | cmdArguments.add(new CommandLineArgument("--debug",0,false)); |
| 229 | |
| 230 | return cmdArguments; |
| 231 | } |
| 232 | } |