• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 
12 package org.eclipse.releng.generators;
13 
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.util.StringTokenizer;
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.Task;
21 
22 /**
23  * This task will count the number of fils in a given directory
24  * that match a given filter.  The number of fils will be output
25  * to a given output file.  The output file will be overwritten
26  * if it already exists.
27  *
28  * Note: Filter comparison is NOT case sensitive.  Do not use wild cards.
29  * ie .zip counts all files with .zip anywere in the name.
30  */
31 public class FileCounter extends Task {
32 
33 	private String sourceDirectory = "";
34 	private String filterString = ".zip";
35 	private String outputFile = "";
36 
main(String args[])37 	public static void main(String args[]) {
38 		// For testing only.
39 		FileCounter aFileCounter = new FileCounter();
40 		aFileCounter.setSourceDirectory("c:\\RelEng\\dean");
41 		aFileCounter.setOutputFile("c:\\RelEng\\dean\\files.count");
42 		aFileCounter.setFilterString(".zip");
43 		aFileCounter.execute();
44 	}
45 
execute()46 	public void execute() throws BuildException {
47 		// Do the work.
48 
49 		int count = 0;
50 
51 		System.out.println("Source Directory: " + this.getSourceDirectory());
52 		System.out.println("Output File: " + this.getOutputFile());
53 		System.out.println("Filter String: " + this.getFilterString());
54 
55 		File aDirectory = new File(this.getSourceDirectory());
56 		if (aDirectory == null) {
57 			throw new BuildException("Directory " + this.getSourceDirectory() + " not found.");
58 		}
59 
60 		String[] names = aDirectory.list();
61 		if (names == null) {
62 			throw new BuildException("Directory " + this.getSourceDirectory() + " not found.");
63 		}
64 
65 		System.out.println("List size: " + names.length);
66 
67 		for (int i = 0; i < names.length; i++) {
68 			System.out.println("Name: " + names[i]);
69 
70 			int index = -1;
71 			StringTokenizer types = getFileTypes();
72 
73 			while (types.hasMoreTokens()){
74 				index = names[i].toLowerCase().indexOf(types.nextToken().toLowerCase());
75 				if (index != -1) {
76 					count++;
77 				}
78 			}
79 
80 		}
81 
82 		try {
83 			FileOutputStream anOutputStream = new FileOutputStream(this.getOutputFile());
84 			anOutputStream.write(String.valueOf(count).getBytes());
85 			anOutputStream.close();
86 		} catch (FileNotFoundException e) {
87 			throw new BuildException("Can not create file.count file");
88 		} catch (IOException e) {
89 			throw new BuildException("Can not create file.count file");
90 		}
91 
92 	}
93 
getFileTypes()94 	private StringTokenizer getFileTypes(){
95 		return new StringTokenizer(getFilterString(),",");
96 	}
97 
98 	/**
99 	 * Gets the sourceDirectory.
100 	 * @return Returns a String
101 	 */
getSourceDirectory()102 	public String getSourceDirectory() {
103 		return sourceDirectory;
104 	}
105 
106 	/**
107 	 * Sets the sourceDirectory.
108 	 * @param sourceDirectory The sourceDirectory to set
109 	 */
setSourceDirectory(String sourceDirectory)110 	public void setSourceDirectory(String sourceDirectory) {
111 		this.sourceDirectory = sourceDirectory;
112 	}
113 
114 	/**
115 	 * Gets the filterString.
116 	 * @return Returns a String
117 	 */
getFilterString()118 	public String getFilterString() {
119 		return filterString;
120 	}
121 
122 	/**
123 	 * Sets the filterString.
124 	 * @param filterString The filterString to set
125 	 */
setFilterString(String filterString)126 	public void setFilterString(String filterString) {
127 		this.filterString = filterString;
128 	}
129 
130 	/**
131 	 * Gets the outputFile.
132 	 * @return Returns a String
133 	 */
getOutputFile()134 	public String getOutputFile() {
135 		return outputFile;
136 	}
137 
138 	/**
139 	 * Sets the outputFile.
140 	 * @param outputFile The outputFile to set
141 	 */
setOutputFile(String outputFile)142 	public void setOutputFile(String outputFile) {
143 		this.outputFile = outputFile;
144 	}
145 
146 }
147