KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ml > GenerateUIDs


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

19
20 package edu.umd.cs.findbugs.ml;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.zip.GZIPInputStream JavaDoc;
31
32 import org.dom4j.Attribute;
33 import org.dom4j.Document;
34 import org.dom4j.DocumentException;
35 import org.dom4j.DocumentFactory;
36 import org.dom4j.Element;
37 import org.dom4j.io.OutputFormat;
38 import org.dom4j.io.XMLWriter;
39
40 import edu.umd.cs.findbugs.BugCollection;
41 import edu.umd.cs.findbugs.Project;
42 import edu.umd.cs.findbugs.SortedBugCollection;
43 import edu.umd.cs.findbugs.xml.Dom4JXMLOutput;
44
45 /**
46  * Add uid attributes to BugInstances in a BugCollection.
47  * A uid is an integer that uniquely identifies a BugInstance
48  * in a BugCollection.
49  * Right now this is only used in machine learning experiments.
50  *
51  * @author David Hovemeyer
52  */

53 public class GenerateUIDs {
54     private BugCollection bugCollection;
55     private Project project;
56     private String JavaDoc inputFilename;
57     private String JavaDoc outputFilename;
58     
59     public GenerateUIDs(String JavaDoc inputFilename, String JavaDoc outputFilename) {
60         this.bugCollection = new SortedBugCollection();
61         this.project = new Project();
62         this.inputFilename = inputFilename;
63         this.outputFilename = outputFilename;
64     }
65     
66     public void execute() throws IOException JavaDoc, DocumentException {
67         InputStream JavaDoc in;
68         if (inputFilename.equals("-")) {
69             in = System.in;
70         } else {
71             in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(inputFilename));
72             if (inputFilename.endsWith(".gz"))
73                 in = new GZIPInputStream JavaDoc(in);
74         }
75         
76         bugCollection.readXML(in, project);
77
78         Document document = DocumentFactory.getInstance().createDocument();
79         Dom4JXMLOutput xmlOutput = new Dom4JXMLOutput(document);
80         bugCollection.writeXML(xmlOutput, project);
81         
82         int count = 0;
83         
84         List JavaDoc<Element> bugInstanceList = document.selectNodes("/BugCollection/BugInstance");
85         for (Element element : bugInstanceList) {
86             Attribute uidAttr = element.attribute("uid");
87             if (uidAttr == null) {
88                 element.addAttribute("uid", Integer.toString(count++));
89             }
90         }
91         
92         OutputStream JavaDoc out;
93         if (outputFilename.equals("-")) {
94             out = System.out;
95         } else {
96             out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outputFilename));
97         }
98         XMLWriter xmlWriter = new XMLWriter(out, OutputFormat.createPrettyPrint());
99         xmlWriter.write(document);
100     }
101     
102     public static void main(String JavaDoc[] args) throws IOException JavaDoc, DocumentException {
103         if (args.length != 2) {
104             System.err.println("Usage: " + GenerateUIDs.class.getName() +
105                     " <input file> <output file>");
106             System.exit(1);
107         }
108         
109         String JavaDoc inputFilename = args[0];
110         String JavaDoc outputFilename = args[1];
111         
112         GenerateUIDs generateUIDs = new GenerateUIDs(inputFilename, outputFilename);
113         generateUIDs.execute();
114     }
115 }
116
Popular Tags