KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > workflow > CopyBuggySource


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 William Pugh
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package edu.umd.cs.findbugs.workflow;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.apache.bcel.generic.GETSTATIC;
29 import org.dom4j.DocumentException;
30
31 import edu.umd.cs.findbugs.BugAnnotation;
32 import edu.umd.cs.findbugs.BugCollection;
33 import edu.umd.cs.findbugs.BugInstance;
34 import edu.umd.cs.findbugs.ClassAnnotation;
35 import edu.umd.cs.findbugs.DetectorFactoryCollection;
36 import edu.umd.cs.findbugs.PackageMemberAnnotation;
37 import edu.umd.cs.findbugs.Project;
38 import edu.umd.cs.findbugs.SortedBugCollection;
39 import edu.umd.cs.findbugs.SourceLineAnnotation;
40 import edu.umd.cs.findbugs.ba.SourceFinder;
41
42 /**
43  * Java main application to compute update a historical bug collection with
44  * results from another build/analysis.
45  *
46  * @author William Pugh
47  */

48
49 public class CopyBuggySource {
50
51     /**
52      *
53      */

54     private static final String JavaDoc USAGE = "Usage: <cmd> "
55             + " <bugs.xml> <destinationSrcDir>";
56
57     public static void main(String JavaDoc[] args) throws IOException JavaDoc,
58             DocumentException {
59
60         DetectorFactoryCollection.instance();
61         if (args.length != 2) {
62             System.out.println(USAGE);
63             return;
64         }
65
66         Project project = new Project();
67         BugCollection origCollection;
68         origCollection = new SortedBugCollection();
69         origCollection.readXML(args[0], project);
70         File JavaDoc src = new File JavaDoc(args[1]);
71         byte buf[] = new byte[4096];
72         if (!src.isDirectory())
73             throw new IllegalArgumentException JavaDoc(args[1]
74                     + " is not a source directory");
75         SourceFinder sourceFinder = new SourceFinder();
76         sourceFinder.setSourceBaseList(project.getSourceDirList());
77         HashSet JavaDoc<String JavaDoc> copied = new HashSet JavaDoc<String JavaDoc>();
78         for (BugInstance bug : origCollection.getCollection()) {
79             for (Iterator JavaDoc<BugAnnotation> i = bug.annotationIterator(); i
80                     .hasNext();) {
81                 BugAnnotation ann = i.next();
82                 SourceLineAnnotation sourceAnnotation;
83                 if (ann instanceof PackageMemberAnnotation)
84                     sourceAnnotation = ((PackageMemberAnnotation) ann)
85                             .getSourceLines();
86                 else if (ann instanceof SourceLineAnnotation)
87                     sourceAnnotation = (SourceLineAnnotation) ann;
88                 else
89                     continue;
90                 if (sourceAnnotation == null)
91                     continue;
92                 String JavaDoc fullName;
93
94                 String JavaDoc packageName = sourceAnnotation.getPackageName();
95                 String JavaDoc sourceFile = sourceAnnotation.getSourceFile();
96                 if (packageName == "")
97                     fullName = sourceFile;
98                 else
99                     fullName = packageName.replace('.', File.separatorChar)
100                             + File.separatorChar + sourceFile;
101                 if (copied.add(fullName)) {
102                     File JavaDoc file = new File JavaDoc(src, fullName);
103                     if (file.exists()) {
104                         System.out.println(file + " already exists");
105                         continue;
106                     }
107                     File JavaDoc parent = file.getParentFile();
108                     parent.mkdirs();
109                     InputStream JavaDoc in = null;
110                     OutputStream JavaDoc out = null;
111                     try {
112                         in = sourceFinder.openSource(packageName, sourceFile);
113                         out = new FileOutputStream JavaDoc(file);
114                         while (true) {
115                             int sz = in.read(buf);
116                             if (sz < 0)
117                                 break;
118                             out.write(buf, 0, sz);
119                         }
120                         System.out.println("Copied " + file);
121                     } catch (IOException JavaDoc e) {
122                         System.out.println("Problem copying " + file);
123                         e.printStackTrace(System.out);
124                     } finally {
125                         close(in);
126                         close(out);
127                     }
128                 }
129             }
130         }
131     }
132
133     public static void close(InputStream JavaDoc in) {
134         try {
135             if (in != null)
136                 in.close();
137         } catch (IOException JavaDoc e) {
138         }
139
140     }
141
142     public static void close(OutputStream JavaDoc out) {
143         try {
144             if (out != null)
145                 out.close();
146         } catch (IOException JavaDoc e) {
147         }
148
149     }
150 }
151
Popular Tags