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

package edu.umd.cs.findbugs.ml;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import edu.umd.cs.findbugs.BugCollection;
import edu.umd.cs.findbugs.Project;
import edu.umd.cs.findbugs.SortedBugCollection;
import edu.umd.cs.findbugs.xml.Dom4JXMLOutput;

/**
 * Add uid attributes to BugInstances in a BugCollection.
 * A uid is an integer that uniquely identifies a BugInstance
 * in a BugCollection.
 * Right now this is only used in machine learning experiments.
 * 
 * @author David Hovemeyer
 */
public class GenerateUIDs {
	private BugCollection bugCollection;
	private Project project;
	private String inputFilename;
	private String outputFilename;
	
	public GenerateUIDs(String inputFilename, String outputFilename) {
		this.bugCollection = new SortedBugCollection();
		this.project = new Project();
		this.inputFilename = inputFilename;
		this.outputFilename = outputFilename;
	}
	
	public void execute() throws IOException, DocumentException {
		bugCollection.readXML(inputFilename, project);

		Document document = DocumentFactory.getInstance().createDocument();
		Dom4JXMLOutput xmlOutput = new Dom4JXMLOutput(document);
		bugCollection.writeXML(xmlOutput, project);
		
		int count = 0;
		
		List bugInstanceList = document.selectNodes("/BugCollection/BugInstance");
		for (Iterator i = bugInstanceList.iterator(); i.hasNext(); ) {
			Element element = (Element) i.next();
			Attribute uidAttr = element.attribute("uid");
			if (uidAttr == null) {
				element.addAttribute("uid", new Integer(count++).toString());
			}
		}
		
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFilename));
		XMLWriter xmlWriter = new XMLWriter(out, OutputFormat.createPrettyPrint());
		xmlWriter.write(document);
	}
	
	public static void main(String[] args) throws IOException, DocumentException {
		if (args.length != 2) {
			System.err.println("Usage: " + GenerateUIDs.class.getName() +
					" <input file> <output file>");
			System.exit(1);
		}
		
		String inputFilename = args[0];
		String outputFilename = args[1];
		
		GenerateUIDs generateUIDs = new GenerateUIDs(inputFilename, outputFilename);
		generateUIDs.execute();
		System.out.println("Done!");
	}
}

@@ -878,35 +878,103 @@
 java.io.Buffered
-Out
+InputStream;%0Aimport java.io.BufferedOutputStream;%0Aimport java.io.FileIn
 putStream;%0Aimpor
@@ -1025,24 +1025,81 @@
 OException;%0A
+import java.io.InputStream;%0Aimport java.io.OutputStream;%0A
 import java.
@@ -2229,16 +2229,175 @@
 ion %7B%0A%09%09
+InputStream in;%0A%09%09if (inputFilename.equals(%22-%22)) %7B%0A%09%09%09in = System.in;%0A%09%09%7D else %7B%0A%09%09%09in = new BufferedInputStream(new FileInputStream(inputFilename));%0A%09%09%7D%0A%09%09%0A%09%09
 bugColle
@@ -2412,27 +2412,16 @@
 adXML(in
-putFilename
 , projec
@@ -2961,24 +2961,16 @@
 %09%7D%0A%09%09%0A%09%09
-Buffered
 OutputSt
@@ -2974,16 +2974,92 @@
 tStream 
+out;%0A%09%09if (outputFilename.equals(%22-%22)) %7B%0A%09%09%09out = System.out;%0A%09%09%7D else %7B%0A%09%09%09
 out = ne
@@ -3120,16 +3120,20 @@
 name));%0A
+%09%09%7D%0A
 %09%09XMLWri
@@ -3621,16 +3621,16 @@
 ename);%0A
+
 %09%09genera
@@ -3651,39 +3651,8 @@
 ();%0A
-%09%09System.out.println(%22Done!%22);%0A
 %09%7D%0A%7D

Allow reading from stdin and writing to stdout.