📄 linecounter.java
字号:
* @param files The list of files to run on. * @param verbose Whether or not to print verbose information. * @return An array of 5 longs: the total number of files, the total * number of lines, the number of lines of code, the number of * lines of comment and the number of blank lines. * @throws IOException If an error occurs during the reading of a file. */ public long[] run(List files, boolean verbose) throws IOException { for (int i = 0; i < files.size(); i++) { if (verbose) { System.out.println("Reading file " + (String)files.get(i)); } countLines((String)files.get(i)); } return new long[] { this.files, lines, linesOfCode, linesOfComment,blankLines }; } /** * @param classElement An XML <tt>class</tt> node. * @return A set of extensions to descriptions. */ private static Set getExtensions(Element classElement) { Set extensions = new HashSet(); List exts = classElement.getChild("extensions").getChildren(); for (int i = 0; i < exts.size(); i++) { extensions.add( ((Element)exts.get(i)).getAttributeValue("value") ); } return extensions; } /** * @param classElement An XML <tt>class</tt> node. * @return An array with single line comment start strings. */ private static String[] getSingleLineComments(Element classElement) { List singles = classElement.getChild("comments") .getChildren("single"); String[] comments = new String[singles.size()]; for (int i = 0; i < singles.size(); i++) { comments[i] = ((Element)singles.get(i)) .getAttributeValue("start"); } return comments; } /** * @param classElement An XML <tt>class</tt> node. * @return An arrau with multi line comments. */ private static Comment[] getMultiLineComments(Element classElement) { List multis = classElement.getChild("comments").getChildren("multi"); Comment[] comments = new Comment[multis.size()]; for (int i = 0; i < multis.size(); i++) { Element el = (Element)multis.get(i); comments[i] = new Comment( el.getAttributeValue("start"), el.getAttributeValue("end") ); } return comments; } /** * Reads the command line arguments. * * @param args The command line arguments. * @return A map of parameters. * @see ParameterReader */ private static Map readParameters(String[] args) { ParameterReader paramreader = new ParameterReader(); paramreader.addKeys( new String[] {"-h", "--help", "-v", "--version", "--verbose"}, ParameterReader.KEY ); paramreader.addKeys( new String[] {"--class", "--config"}, ParameterReader.KEY_EQUALS_VALUE ); paramreader.addKeys( new String[] {"-f", "-d", "-r"}, ParameterReader.KEY_VALUES ); try { return paramreader.readParameters(args); } catch (IllegalArgumentException e) { printErrorMessage("illegal parameter: " + e.getMessage()); } /* * will never reach this statement, since either * paramreader.readParameters() is returned, or printErrorMessage() * has called System.exit(1). */ return null; } /** * @param classname The name of the class to read for. * @return The element for class <tt>classs</tt>, or <tt>null</tt> * if there is no such element. * @param parameters A map with parameters. */ private static Element getClassElement(String classname, Map parameters) { try { File config = null; if (parameters.containsKey("--config")) { config = new File((String)parameters.get("--config")); } else { config = getDefaultFile(); if (config == null) { printErrorMessage("could not find a configuration file"); } } // Parse the config file List classes = new SAXBuilder(true).build(config) .getRootElement().getChildren("class"); for (int i = 0; i < classes.size(); i++) { Element el = (Element)classes.get(i); if (classname.equals(el.getAttributeValue("name") )) { return el; } } return null; } catch (IOException e) { printErrorMessage("could not read configuration file"); } catch (JDOMException e) { printErrorMessage("could not parse configuration file"); } return null; } /** * Looks in the directory <tt>.linecounter</tt> in the user's home * directory and the local directory for a file called * <tt>linecounter.xml</tt>. * * @return The default configuration filename if it can be found, or * <tt>null</tt> otherwise. */ private static File getDefaultFile() { File homefile = new File( new StringBuffer(System.getProperty("user.home")) .append(File.separator) .append(".linecounter") .append(File.separator) .append("linecounter.xml") .toString() ); if (homefile.exists()) { return homefile; } File localfile = new File("linecounter.xml"); if (localfile.exists()) { return localfile; } return null; } /** * @param parameters A map with parameters. * @param filter The file filters to use. * @return A list of filenames, denoted by the <tt>-f</tt>, * <tt>-d</tt> and <tt>-r</tt> parameters in * <tt>parameters</tt>. */ private static List getFiles(Map parameters, FileFilter filter) throws IOException { List files = new ArrayList(); if (parameters.containsKey("-f")) { files.addAll((List)parameters.get("-f")); } if (parameters.containsKey("-d")) { List dirs = (List)parameters.get("-d"); for (int i = 0; i < dirs.size(); i++) { files.addAll(FileUtils.getFiles( new File((String)dirs.get(i)), filter, false )); } } if (parameters.containsKey("-r")) { List dirs = (List)parameters.get("-r"); for (int i = 0; i < dirs.size(); i++) { files.addAll(FileUtils.getFiles( new File((String)dirs.get(i)), filter, true )); } } return files; } /** * This method executes the program. * * @param args An array with the command line arguments. */ public static void main(String[] args) { // retrieve parameters Map parameters = readParameters(args); // check for help / version if (parameters.containsKey("-h") || parameters.containsKey("--help")) { printHelpMessage(); } if (parameters.containsKey("-v") || parameters.containsKey("--version")) { printVersionMessage(); } if (!parameters.containsKey("--class")) { printErrorMessage("necessary parameter --class missing"); } try { // create line counter LineCounter counter = new LineCounter( getClassElement((String)parameters.get("--class"), parameters) ); // get all files List files = getFiles(parameters, counter.getFileFilter()); // let the reader read printResults( counter.run(files, parameters.containsKey("--verbose")) ); } catch (Exception e) { printErrorMessage(e.getMessage()); } } /** * Prints a help message to standard out. */ private static void printHelpMessage() { System.out.println("Usage: LineCounter [OPTIONS]"); System.out.println("Counts the lines of code and comment and blank " + "lines from files."); System.out.println(); System.out.println(" --class=CLASS for which class of files to " + "read. It MUST be given"); System.out.println(" --config=FILE use FILE as a configuration " + "file"); System.out.println(" -h, --help displays this help message and" + " exits"); System.out.println(" -v, --version output version information and" + " exits"); System.out.println(" --verbose display the names of the files" + " read"); System.out.println(" -f takes files as its input"); System.out.println(" -d takes directories as root " + "directories for the files"); System.out.println(" -r takes directories as root " + "directories for the files,"); System.out.println(" recursing through sub " + "directories"); System.out.println("The -r, -d and -f tags can be combined."); System.exit(0); } /** * Prints a version message to standard out. */ private static void printVersionMessage() { System.out.println("LineCounter version " + version); System.out.println("Written by Rob Spoor"); System.out.println(); System.out.println("Copyright 2003-2004 Rob Spoor"); System.exit(0); } /** * Prints an error message to standard out when some exception has * occured. */ private static void printErrorMessage(String message) { System.err.println("LineCounter: " + message); System.err.println("Try LineCounter --help for more information"); System.exit(1); } /** * Prints the results of a {@link #run(List, boolean)} call. * * @param results An array with the results. */ private static void printResults(long[] results) { System.out.println("Number of files read: " + results[0]); System.out.println("Total number of lines: " + results[1]); System.out.println("Number of code lines: " + results[2]); System.out.println("Number of comment lines: " + results[3]); System.out.println("Number of blank lines: " + results[4]); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -