KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > FindBadConstructions


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.*;
23 import java.nio.ByteBuffer JavaDoc;
24 import java.nio.CharBuffer JavaDoc;
25 import java.nio.channels.FileChannel JavaDoc;
26 import java.nio.charset.Charset JavaDoc;
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 /** Task to search some files for bad constructions.
34  * @author Jesse Glick
35  */

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     /** Add a set of files to scan. */
44     public void addFileset(FileSet fs) {
45         filesets.add(fs);
46     }
47     /** Add a set of files to scan, according to CVS status. */
48     public void addCvsFileset(CvsFileSet fs) {
49         filesets.add(fs);
50     }
51     /** Add a construction that is bad. */
52     public Construction createConstruction() {
53         Construction c = new Construction();
54         bad.add(c);
55         return c;
56     }
57     /** One bad construction. */
58     public class Construction {
59         Pattern regexp;
60         String JavaDoc message = null;
61         int show = -1;
62         public Construction() {}
63         /**
64          * Set the bad regular expression to search for.
65          * Use embedded flags to set any desired pattern behaviors like case insensitivity;
66          * multiline mode is always on.
67          */

68         public void setRegexp(String JavaDoc 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         /** Set an optional message to display as output. */
76         public void setMessage(String JavaDoc m) {
77             message = m;
78         }
79         /** Set whether to display the matching text (by default no), and if so which part.
80          * @param s 0 means complete match; 1 or higher means that-numbered parenthesis
81          */

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 JavaDoc[] files = scanner.getIncludedFiles();
94             log("Scanning " + files.length + " files in " + dir);
95             for (String JavaDoc name : files) {
96                 File f = new File(dir, name);
97                 //System.err.println("working on " + f);
98
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 JavaDoc fc = fis.getChannel();
103                         try {
104                             ByteBuffer JavaDoc bb = fc.map(FileChannel.MapMode.READ_ONLY, 0L, fc.size());
105                             Charset JavaDoc cs = Charset.forName("UTF-8");
106                             CharBuffer JavaDoc content = cs.decode(bb);
107                             Matcher m = c.regexp.matcher(content);
108                             while (m.find()) {
109                                 StringBuffer JavaDoc message = new StringBuffer JavaDoc(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