KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > console > BuildFailedTracker


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.console;
12
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.runtime.Path;
17 import org.eclipse.debug.ui.console.FileLink;
18 import org.eclipse.debug.ui.console.IConsole;
19 import org.eclipse.debug.ui.console.IConsoleLineTracker;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.ui.externaltools.internal.model.StringMatcher;
23
24 /**
25  * Generates hyperlinks for build failures
26  */

27 public class BuildFailedTracker implements IConsoleLineTracker {
28     
29     private IConsole fConsole;
30     private StringMatcher fErrorMatcher;
31     private StringMatcher fErrorMatcher2;
32     private boolean fBuildFailed= false;
33     
34     /* (non-Javadoc)
35      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#init(org.eclipse.debug.ui.console.IConsole)
36      */

37     public void init(IConsole console) {
38         fConsole = console;
39         //BUILD FAILED: file:c:/1115/test/buildFiles/23638.xml:12:
40
fErrorMatcher = new StringMatcher("*BUILD FAILED: *.xml*", false, false); //$NON-NLS-1$
41
fErrorMatcher2= new StringMatcher("*.xml*", false, false); //$NON-NLS-1$
42
}
43
44     /* (non-Javadoc)
45      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#lineAppended(org.eclipse.jface.text.IRegion)
46      */

47     public void lineAppended(IRegion line) {
48         try {
49             int lineOffset = line.getOffset();
50             int lineLength = line.getLength();
51             String JavaDoc text = fConsole.getDocument().get(lineOffset, lineLength);
52             String JavaDoc fileName = null;
53             String JavaDoc lineNumber = ""; //$NON-NLS-1$
54
int fileStart = -1;
55             int index= -1;
56             if (fErrorMatcher.match(text)) {
57                 fBuildFailed= true;
58                 index = text.indexOf("file:"); //$NON-NLS-1$
59
if (index > 0) {
60                     fileStart = index + 5;
61                 } else {
62                     fileStart = text.indexOf("BUILD FAILED:") + 14; //$NON-NLS-1$
63
index= fileStart;
64                 }
65             } else if (fBuildFailed && fErrorMatcher2.match(text)) {
66                 //output resulting from failures which occurred in nested build from using the ant task:
67
//BUILD FAILED: C:\Darins\Debugger\20021213\eclipse\runtime-workspace\Mine\build.xml:4: Following error occured while executing this line
68
//C:\Darins\Debugger\20021213\eclipse\runtime-workspace\Mine\subbuild.xml:4: srcdir attribute must be set!
69
index= 0;
70                 fileStart= 0;
71             }
72             if (index > -1) {
73                 index = text.indexOf("xml", index); //$NON-NLS-1$
74
if (index > 0) {
75                     int numberStart= index + 4;
76                     int numberEnd= text.indexOf(':', numberStart);
77                     int fileEnd = index + 3;
78                     if (numberStart > 0 && fileEnd > 0) {
79                         fileName = text.substring(fileStart, fileEnd).trim();
80                         if (numberEnd > 0) {
81                             lineNumber = text.substring(numberStart, numberEnd).trim();
82                         }
83                     }
84                 }
85             }
86             if (fileName != null) {
87                 int num = -1;
88                 try {
89                     num = Integer.parseInt(lineNumber);
90                 } catch (NumberFormatException JavaDoc e) {
91                 }
92                 IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(fileName));
93                 IFile file= null;
94                 if (files.length > 0) {
95                     file= files[0];
96                 }
97                 if (file != null && file.exists()) {
98                     FileLink link = new FileLink(file, null, -1, -1, num);
99                     fConsole.addLink(link, lineOffset + fileStart, lineLength - fileStart);
100                 }
101             }
102         } catch (BadLocationException e) {
103         }
104     }
105
106     /* (non-Javadoc)
107      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#dispose()
108      */

109     public void dispose() {
110         fConsole = null;
111     }
112 }
Popular Tags