ICU-8114 checkin NAFD improvements

X-SVN-Rev: 28990
This commit is contained in:
Steven R. Loomis 2010-11-02 21:44:10 +00:00
parent 1abfa1c39c
commit adf994a3fd

View File

@ -7,19 +7,37 @@
package com.ibm.icu.dev.tools.misc;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.Vector;
public class NonAsciiFileDetector
{
private static boolean VERBOSE = false;
public static class ICUSourceFileFilter implements FilenameFilter
{
int matched = 0;
int skipped = 0;
public boolean accept(File dir, String name)
{
return name.endsWith(".cpp") || name.endsWith(".c") || name.endsWith(".h") || name.endsWith(".java");
boolean doAccept = name.endsWith(".cpp") || name.endsWith(".c") || name.endsWith(".h") || name.endsWith(".java");
if(doAccept) {
matched++;
} else {
skipped++;
}
return doAccept;
}
public String stats() {
return "Checked " + matched + " files and skipped " + skipped + " files";
}
}
@ -49,13 +67,22 @@ public class NonAsciiFileDetector
public static void listFiles(File file,
FilenameFilter filter,
Vector list) throws IOException
Vector<File> list) throws IOException
{
if(VERBOSE) System.err.println(" .. checking " + file.getPath() );
File files[] = file.listFiles();
if (files != null && files.length > 0) {
for (int i = 0; i < files.length; i ++) {
if (files[i].isDirectory()) {
listFiles(files[i], filter, list);
if(files[i].getAbsolutePath().equals(files[i].getCanonicalPath())) {
if(!files[i].getName().equals(".svn")) { // skip .svn dirs
listFiles(files[i], filter, list);
}
} else {
if(VERBOSE) {
System.err.println(".. skipping symlink " + files[i].getPath());
}
}
}
else {
if (filter.accept(file, files[i].getName())) {
@ -66,22 +93,21 @@ public class NonAsciiFileDetector
}
}
public static Vector getNonAsciiFiles(String directory,
public static Map<String, Integer> getNonAsciiFiles(File directory,
FilenameFilter filter)
throws IOException
{
Vector files = new Vector();
Vector result = new Vector();
listFiles(new File(directory), filter, files);
Vector<File> files = new Vector<File>();
Map<String,Integer> result = new TreeMap<String,Integer>();
listFiles(directory, filter, files);
int filecount = files.size();
if (filecount == 0) {
return null;
}
for (int i = 0; i < filecount; i ++) {
int isnonascii = isNonAscii((File)files.elementAt(i));
int isnonascii = isNonAscii(files.elementAt(i));
if (isnonascii != -1) {
result.add(((File)files.elementAt(i)).getAbsolutePath());
result.add(new Integer(isnonascii));
result.put(files.elementAt(i).getAbsolutePath(),new Integer(isnonascii));
}
}
return result;
@ -89,20 +115,54 @@ public class NonAsciiFileDetector
public static void main(String arg[])
{
try {
Vector nonascii = getNonAsciiFiles(arg[0], new ICUSourceFileFilter());
System.out.println();
if (nonascii != null && nonascii.size() > 0) {
for (int i = 0; i < nonascii.size(); i += 2) {
System.out.println("Non ascii files "
+ (String)nonascii.elementAt(i) + " "
+ ((Integer)nonascii.elementAt(i + 1)).intValue());
}
System.exit(1);
}
} catch (IOException e) {
e.printStackTrace();
}
int nextArg = 0;
for(nextArg = 0;nextArg<arg.length && arg[nextArg].startsWith("-");nextArg++) {
if(arg[nextArg].equals("-v")) {
VERBOSE=true;
System.err.println(" .. verbose mode.");
}
}
if(nextArg == arg.length) {
System.err.println(NonAsciiFileDetector.class.getSimpleName()+": error, no directories specified!");
}
int bad=0;
for(;nextArg<arg.length;nextArg++){
try {
ICUSourceFileFilter isff = new ICUSourceFileFilter();
File dir = new File(arg[nextArg]);
if(!dir.isDirectory()) {
throw new FileNotFoundException("Not a directory: " + dir.getAbsolutePath());
}
Map<String, Integer> nonascii = getNonAsciiFiles(dir, isff);
boolean noised = false;
System.out.println();
if (nonascii != null && nonascii.size() > 0) {
for (Entry<String, Integer> e : nonascii.entrySet()) {
if(!noised) {
System.out.println("Non ascii files in " + arg[nextArg] + ": ");
noised = true;
}
System.out.println(""
+e.getKey() + ":"
+e.getValue());
bad++;
}
} else {
// if (VERBOSE==true) {
System.out.println("No non ascii files in " + arg[nextArg]);
// }
}
// if(VERBOSE==true) {
System.out.println( isff.stats());
// }
} catch (IOException e) {
System.err.println("Error processing " + arg[nextArg]);
e.printStackTrace();
}
}
if(bad>0) {
System.err.println(bad+" non-ascii files found in total.");
}
}
}