Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 483a5d3c2129953f350332a5913f54bbd7542b0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.externaltools.internal.ant.launchConfigurations;


import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.ui.console.FileLink;
import org.eclipse.debug.ui.console.IConsole;
import org.eclipse.debug.ui.console.IConsoleLineTracker;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.ui.externaltools.internal.model.StringMatcher;

/**
 * Generates hyperlinks for javac output
 */
public class JavacLineTracker implements IConsoleLineTracker {
	
	private IConsole fConsole;
	private IFile fLastFile;
	private StringMatcher fEclipseCompilerMatcher;
	private StringMatcher fJavacMatcher;
	private StringMatcher fJikesMatcher;
	
	// trolling for errors after a Jikes error header was found
	private boolean fTrolling = false;

	/**
	 * Constructor for JavacLineTracker.
	 */
	public JavacLineTracker() {
		super();
	}

	/**
	 * @see org.eclipse.debug.ui.console.IConsoleLineTracker#init(org.eclipse.debug.ui.console.IConsole)
	 */
	public void init(IConsole console) {
		fConsole = console;
		fEclipseCompilerMatcher = new StringMatcher("*[javac]*ERROR in*.java*(at line*)*",false, false); //$NON-NLS-1$
		fJavacMatcher = new StringMatcher("*[javac] *.java:*:*",false, false); //$NON-NLS-1$
		fJikesMatcher = new StringMatcher("*[javac] *\"*.java\":", false, false); //$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.debug.ui.console.IConsoleLineTracker#lineAppended(org.eclipse.jface.text.IRegion)
	 */
	public void lineAppended(IRegion line) {
		try {
			int lineOffset = line.getOffset();
			int lineLength = line.getLength();
			String text = fConsole.getDocument().get(lineOffset, lineLength);
			String fileName = null;
			String lineNumber = ""; //$NON-NLS-1$
			int fileStart = -1;
			if (fEclipseCompilerMatcher.match(text)) {
				fTrolling = false;
				int index = text.indexOf("ERROR in"); //$NON-NLS-1$
				if (index > 0) {
					fileStart = index + 9;
					index = text.lastIndexOf("(at line "); //$NON-NLS-1$
					if (index > 0) {
						int fileEnd = index - 1;
						int numberStart = index + 9;
						index = text.lastIndexOf(')');
						if (index > 0) {
							int numberEnd = index;
							fileName = text.substring(fileStart, fileEnd).trim();
							lineNumber = text.substring(numberStart, numberEnd).trim();
						}
					}
				}
			} else if (fJavacMatcher.match(text)) {
				fTrolling = false;
				fileStart = text.indexOf("[javac] "); //$NON-NLS-1$
				fileStart += 8;
				int index = text.indexOf(".java:", fileStart); //$NON-NLS-1$
				if (index > 0) {
					int numberStart = index + 6;
					fileName = text.substring(fileStart, numberStart - 1).trim();
					index = text.indexOf(":", numberStart); //$NON-NLS-1$
					if (index > numberStart) {
						lineNumber = text.substring(numberStart, index);
					}
				}
			} else if (fJikesMatcher.match(text)) {
				fileStart = text.indexOf('"');
				fileStart++;
				int index = text.indexOf(".java\"", fileStart); //$NON-NLS-1$
				if (index > 0) {
					index += 5;
					fileName = text.substring(fileStart, index).trim();
					fTrolling = true;
				}
			} else if (fTrolling) {
				int index = text.indexOf("[javac]"); //$NON-NLS-1$
				if (index > 0) {
					// look for a line number
					index+=7; 
					int numEnd = text.indexOf(".", index); //$NON-NLS-1$
					if (numEnd > 0) {
						String number = text.substring(index, numEnd).trim();
						try {
							int num = Integer.parseInt(number);
							int numStart = text.indexOf(number, index);
							if (fLastFile != null && fLastFile.exists()) {
								FileLink link = new FileLink(fLastFile, null, -1, -1, num);
								fConsole.addLink(link, lineOffset + numStart, lineLength - numStart);
							}
						} catch (NumberFormatException e) {
							// not a line number
						}
					}
				} else {
					fTrolling = false;
				}
			}
			if (fileName != null) {
				int num = -1;
				try {
					num = Integer.parseInt(lineNumber);
				} catch (NumberFormatException e) {
				}
				IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(fileName));
				fLastFile = file;
				if (file != null && file.exists()) {
					FileLink link = new FileLink(file, null, -1, -1, num);
					fConsole.addLink(link, lineOffset + fileStart, lineLength - fileStart);
				}
			}
		} catch (BadLocationException e) {
		}
	}

	/**
	 * @see org.eclipse.debug.ui.console.IConsoleLineTracker#dispose()
	 */
	public void dispose() {
		fConsole = null;
	}

}

Back to the top