KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.debug.ui.console.FileLink;
19 import org.eclipse.debug.ui.console.IConsole;
20 import org.eclipse.debug.ui.console.IConsoleLineTracker;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.ui.externaltools.internal.model.StringMatcher;
24
25 /**
26  * Generates hyperlinks for javac output
27  */

28 public class JavacLineTracker implements IConsoleLineTracker {
29     
30     private IConsole fConsole;
31     private IFile fLastFile;
32     private StringMatcher fEclipseCompilerMatcher;
33     private StringMatcher fJavacMatcher;
34     private StringMatcher fJikesMatcher;
35     
36     // trolling for errors after a Jikes error header was found
37
private boolean fTrolling = false;
38
39     /**
40      * Constructor for JavacLineTracker.
41      */

42     public JavacLineTracker() {
43         super();
44     }
45
46     /**
47      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#init(org.eclipse.debug.ui.console.IConsole)
48      */

49     public void init(IConsole console) {
50         fConsole = console;
51         fEclipseCompilerMatcher = new StringMatcher("*[javac]*ERROR in*.java*(at line*)*",false, false); //$NON-NLS-1$
52
fJavacMatcher = new StringMatcher("*[javac] *.java:*:*",false, false); //$NON-NLS-1$
53
fJikesMatcher = new StringMatcher("*[javac] *\"*.java\":", false, false); //$NON-NLS-1$
54
}
55
56     /**
57      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#lineAppended(org.eclipse.jface.text.IRegion)
58      */

59     public void lineAppended(IRegion line) {
60         try {
61             int lineOffset = line.getOffset();
62             int lineLength = line.getLength();
63             String JavaDoc text = fConsole.getDocument().get(lineOffset, lineLength);
64             String JavaDoc fileName = null;
65             String JavaDoc lineNumber = ""; //$NON-NLS-1$
66
int fileStart = -1;
67             if (fEclipseCompilerMatcher.match(text)) {
68                 fTrolling = false;
69                 int index = text.indexOf("ERROR in"); //$NON-NLS-1$
70
if (index > 0) {
71                     fileStart = index + 9;
72                     index = text.lastIndexOf("(at line "); //$NON-NLS-1$
73
if (index > 0) {
74                         int fileEnd = index - 1;
75                         int numberStart = index + 9;
76                         index = text.lastIndexOf(')');
77                         if (index > 0) {
78                             int numberEnd = index;
79                             fileName = text.substring(fileStart, fileEnd).trim();
80                             lineNumber = text.substring(numberStart, numberEnd).trim();
81                         }
82                     }
83                 }
84             } else if (fJavacMatcher.match(text)) {
85                 fTrolling = false;
86                 fileStart = text.indexOf("[javac] "); //$NON-NLS-1$
87
fileStart += 8;
88                 int index = text.indexOf(".java:", fileStart); //$NON-NLS-1$
89
if (index > 0) {
90                     int numberStart = index + 6;
91                     fileName = text.substring(fileStart, numberStart - 1).trim();
92                     index = text.indexOf(":", numberStart); //$NON-NLS-1$
93
if (index > numberStart) {
94                         lineNumber = text.substring(numberStart, index);
95                     }
96                 }
97             } else if (fJikesMatcher.match(text)) {
98                 fileStart = text.indexOf('"');
99                 fileStart++;
100                 int index = text.indexOf(".java\"", fileStart); //$NON-NLS-1$
101
if (index > 0) {
102                     index += 5;
103                     fileName = text.substring(fileStart, index).trim();
104                     fTrolling = true;
105                 }
106             } else if (fTrolling) {
107                 int index = text.indexOf("[javac]"); //$NON-NLS-1$
108
if (index > 0) {
109                     // look for a line number
110
index+=7;
111                     int numEnd = text.indexOf(".", index); //$NON-NLS-1$
112
if (numEnd > 0) {
113                         String JavaDoc number = text.substring(index, numEnd).trim();
114                         try {
115                             int num = Integer.parseInt(number);
116                             int numStart = text.indexOf(number, index);
117                             if (fLastFile != null && fLastFile.exists()) {
118                                 FileLink link = new FileLink(fLastFile, null, -1, -1, num);
119                                 fConsole.addLink(link, lineOffset + numStart, lineLength - numStart);
120                             }
121                         } catch (NumberFormatException JavaDoc e) {
122                             // not a line number
123
}
124                     }
125                 } else {
126                     fTrolling = false;
127                 }
128             }
129             if (fileName != null) {
130                 int num = -1;
131                 try {
132                     num = Integer.parseInt(lineNumber);
133                 } catch (NumberFormatException JavaDoc e) {
134                 }
135                 IFile file= null;
136                 IPath filePath= new Path(fileName);
137                 //first check if in the same file...faster
138
if (fLastFile != null && fLastFile.getLocation().equals(filePath)) {
139                     file= fLastFile;
140                 } else {
141                     IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(filePath);
142                     if (files.length != 0) {
143                         file= files[0];
144                     }
145                 }
146                 fLastFile = file;
147                 if (file != null && file.exists()) {
148                     FileLink link = new FileLink(file, null, -1, -1, num);
149                     fConsole.addLink(link, lineOffset + fileStart, lineLength - fileStart);
150                 }
151             }
152         } catch (BadLocationException e) {
153         }
154     }
155
156     /**
157      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#dispose()
158      */

159     public void dispose() {
160         fConsole = null;
161     }
162
163 }
164
Popular Tags