alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package sample.tools;

/**
 * A custom Ant task that finds compile logs containing compile
 * errors.  The compile logs with errors are sent as email attachments using
 * information in monitor.properties.
 */

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.util.Vector;
import java.util.Enumeration;
import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class CompileCheck extends Task {

	//directory containing of build source, parent of features and plugins
	private String dir = "";
	private String output = "compileerrorlist.txt";

	//keep track of compile logs containing errors
	private Vector logsWithErrors;

	public CompileCheck() {
		logsWithErrors = new Vector();
	}

	public void execute() throws BuildException {
		findLogs(dir);
		printErrorLog();
	}

	// test
	public static void main(String[] args) {
		CompileCheck checker = new CompileCheck();
		checker.dir="D:\\workspace\\jdt-builder-sample\\build\\I.TestBuild\\compilelogs";
		checker.output="d:\\workspace\\jdt-builder-sample\\build\\I.TestBuild\\log.txt";
		checker.execute();
	}

	private void findLogs(String file) {
		File aFile = new File(file);

		// basis case
		if (aFile.isFile()) {
			if (aFile.getAbsolutePath().endsWith(".jar.bin.log")||aFile.getAbsolutePath().endsWith("dot.bin.log")){
				read(aFile);
			}else if(aFile.getAbsolutePath().endsWith(".xml")){
				parse(aFile);
			}
		} else {
			//recurse into directories looking for and reading compile logs
			File files[] = aFile.listFiles();

			for (int i = 0; i < files.length; i++) {
				findLogs(files[i].getAbsolutePath());
			}
		}
	}

	private void read(File file) {
		//read the contents of the log file, and return contents as a String
		if (file.length()==0)
			return;
		
		BufferedReader in = null;
		String aLine;

		try {
			in = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		try {
			while ((aLine = in.readLine()) != null) {
				int statusSummaryIndex=aLine.indexOf("problem (");
				if (statusSummaryIndex==-1)
					statusSummaryIndex=aLine.indexOf("problems (");
				
				if (statusSummaryIndex!=-1&&(aLine.indexOf("error", statusSummaryIndex) != -1)){
					logsWithErrors.add(file);
					return;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in!=null)
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

	private void parse(File file) {
		Document aDocument=null;
			BufferedReader reader = null;
			try {
				reader = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

			InputSource inputSource = new InputSource(reader);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = null;

			try {
				builder = factory.newDocumentBuilder();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}

			try {
				aDocument = builder.parse(inputSource);
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			// Get summary of problems
			NodeList nodeList=aDocument.getElementsByTagName("problem_summary");
			if (nodeList==null)
				return;
			
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node problemSummaryNode = nodeList.item(i);
				NamedNodeMap aNamedNodeMap = problemSummaryNode.getAttributes();
				Node errorNode = aNamedNodeMap.getNamedItem("errors");
				if (errorNode!= null&&!errorNode.getNodeValue().equals("0")) {
					logsWithErrors.add(new File(file.getParentFile(),file.getName().replaceAll(".xml", ".html")));
					return;
				}
			}
	}
	
	private void printErrorLog() {
		// print log with names of plug-ins containing compile errors
		Enumeration enumeration = logsWithErrors.elements();

		if (logsWithErrors.size() > 0) {
			PrintWriter out = null;
			try {
				out = new PrintWriter(new FileWriter(new File(output)));
				out.println("<h3>Compile Errors");
				while (enumeration.hasMoreElements()) {
					File compileLog = (File) enumeration.nextElement();
					String path = compileLog.getAbsolutePath();
					String relativePath = (path.substring(path.indexOf("plugins"))).replace('\\', '/');
					out.println("<a href=\"@compilelogdir@/"+relativePath+"\">"+relativePath+"
"); } } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) out.close(); } } } /** * Gets the directory containing compile logs * @return Returns a String */ public String getDir() { return dir; } /** * Sets the directory containing compile logs. * @param install The install to set */ public void setDir(String dir) { this.dir = dir; } public String getOutput() { return output; } public void setOutput(String output) { this.output = output; } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.