1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.*; 23 import java.nio.ByteBuffer ; 24 import java.nio.CharBuffer ; 25 import java.nio.channels.FileChannel ; 26 import java.nio.charset.Charset ; 27 import java.util.*; 28 import java.util.regex.*; 29 30 import org.apache.tools.ant.*; 31 import org.apache.tools.ant.types.FileSet; 32 33 36 public class FindBadConstructions extends Task { 37 38 private static final Pattern lineBreak = Pattern.compile("^", Pattern.MULTILINE); 39 40 private List<FileSet> filesets = new LinkedList<FileSet>(); 41 private List<Construction> bad = new LinkedList<Construction>(); 42 43 44 public void addFileset(FileSet fs) { 45 filesets.add(fs); 46 } 47 48 public void addCvsFileset(CvsFileSet fs) { 49 filesets.add(fs); 50 } 51 52 public Construction createConstruction() { 53 Construction c = new Construction(); 54 bad.add(c); 55 return c; 56 } 57 58 public class Construction { 59 Pattern regexp; 60 String message = null; 61 int show = -1; 62 public Construction() {} 63 68 public void setRegexp(String r) throws BuildException { 69 try { 70 regexp = Pattern.compile(r, Pattern.MULTILINE); 71 } catch (PatternSyntaxException rese) { 72 throw new BuildException(rese, getLocation()); 73 } 74 } 75 76 public void setMessage(String m) { 77 message = m; 78 } 79 82 public void setShowMatch(int s) { 83 show = s; 84 } 85 } 86 87 public void execute() throws BuildException { 88 if (filesets.isEmpty()) throw new BuildException("Must give at least one fileset", getLocation()); 89 if (bad.isEmpty()) throw new BuildException("Must give at least one construction", getLocation()); 90 for (FileSet fs : filesets) { 91 FileScanner scanner = fs.getDirectoryScanner(getProject()); 92 File dir = scanner.getBasedir(); 93 String [] files = scanner.getIncludedFiles(); 94 log("Scanning " + files.length + " files in " + dir); 95 for (String name : files) { 96 File f = new File(dir, name); 97 try { 99 for (Construction c : bad) { 100 if (c.regexp == null) throw new BuildException("Must specify regexp on a construction", getLocation()); 101 FileInputStream fis = new FileInputStream(f); 102 FileChannel fc = fis.getChannel(); 103 try { 104 ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0L, fc.size()); 105 Charset cs = Charset.forName("UTF-8"); 106 CharBuffer content = cs.decode(bb); 107 Matcher m = c.regexp.matcher(content); 108 while (m.find()) { 109 StringBuffer message = new StringBuffer (1000); 110 message.append(f.getAbsolutePath()); 111 message.append(':'); 112 Matcher lbm = lineBreak.matcher(content); 113 int line = 0; 114 int col = 1; 115 while (lbm.find()) { 116 if (lbm.start() <= m.start()) { 117 line++; 118 col = m.start() - lbm.start() + 1; 119 } else { 120 break; 121 } 122 } 123 message.append(line); 124 message.append(":"); 125 message.append(col); 126 message.append(": "); 127 if (c.message != null) { 128 message.append(c.message); 129 } 130 if (c.show != -1) { 131 if (c.message != null) { 132 message.append(": "); 133 } 134 message.append(m.group(c.show)); 135 } 136 if (c.show == -1 && c.message == null) { 137 message.append("bad construction found"); 138 } 139 log(message.toString(), Project.MSG_WARN); 140 } 141 } finally { 142 fc.close(); 143 fis.close(); 144 } 145 } 146 } catch (IOException ioe) { 147 throw new BuildException("Error reading " + f, ioe, getLocation()); 148 } 149 } 150 } 151 } 152 153 } 154 | Popular Tags |