KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.ant.internal.ui.console;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.regex.Matcher JavaDoc;
19 import java.util.regex.Pattern JavaDoc;
20
21 import org.eclipse.ant.internal.ui.AntUIPlugin;
22 import org.eclipse.ant.internal.ui.IAntUIPreferenceConstants;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IMarker;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.debug.core.model.IProcess;
28 import org.eclipse.debug.ui.console.FileLink;
29 import org.eclipse.jface.preference.IPreferenceStore;
30 import org.eclipse.jface.text.BadLocationException;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.jface.text.IRegion;
33 import org.eclipse.ui.console.IPatternMatchListenerDelegate;
34 import org.eclipse.ui.console.PatternMatchEvent;
35 import org.eclipse.ui.console.TextConsole;
36
37 public abstract class AbstractJavacPatternMatcher implements IPatternMatchListenerDelegate {
38
39     protected TextConsole fConsole;
40     private static Pattern JavaDoc fgLineNumberPattern = Pattern.compile("\\d+"); //$NON-NLS-1$
41
private static List JavaDoc fgPatternMatchers= new ArrayList JavaDoc();
42     private Map JavaDoc fFileNameToIFile= new HashMap JavaDoc();
43     
44     private JavacMarkerCreator fMarkerCreator;
45     protected static final Integer JavaDoc fgWarningType= new Integer JavaDoc(IMarker.SEVERITY_WARNING);
46     protected static final Integer JavaDoc fgErrorType= new Integer JavaDoc(IMarker.SEVERITY_ERROR);
47
48     /* (non-Javadoc)
49      * @see org.eclipse.ui.console.IPatternMatchListenerDelegate#connect(org.eclipse.ui.console.TextConsole)
50      */

51     public void connect(TextConsole console) {
52         fConsole= console;
53         IPreferenceStore store= AntUIPlugin.getDefault().getPreferenceStore();
54         if (store.getBoolean(IAntUIPreferenceConstants.ANT_CREATE_MARKERS)) {
55             fMarkerCreator= new JavacMarkerCreator(fConsole, this instanceof EclipseJavacPatternMatcher);
56             fgPatternMatchers.add(this);
57         }
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.ui.console.IPatternMatchListenerDelegate#disconnect()
62      */

63     public void disconnect() {
64         fConsole = null;
65         fFileNameToIFile.clear();
66     }
67     
68     protected IFile getIFile(String JavaDoc filePath) {
69         if (filePath == null) {
70             return null;
71         }
72         IFile file= (IFile) fFileNameToIFile.get(filePath);
73         if (file == null) {
74             IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(filePath));
75             if (files.length > 0) {
76                 file = files[0];
77                 fFileNameToIFile.put(filePath, file);
78             }
79         }
80         return file;
81     }
82     
83     protected String JavaDoc getMatchedText(PatternMatchEvent event) {
84         int eventOffset= event.getOffset();
85         int eventLength= event.getLength();
86         IDocument document= fConsole.getDocument();
87         String JavaDoc matchedText= null;
88         try {
89             matchedText= document.get(eventOffset, eventLength);
90         } catch (BadLocationException e) {
91             AntUIPlugin.log(e);
92         }
93         return matchedText;
94     }
95     
96     protected int getLineNumber(int eventOffset, boolean sameLine) {
97         IDocument document= fConsole.getDocument();
98         try {
99             int fileLine = document.getLineOfOffset(eventOffset);
100             if (!sameLine) {
101                 fileLine += 1;
102             }
103             IRegion region = document.getLineInformation(fileLine);
104             int regionLength = region.getLength();
105             if (region.getOffset() != eventOffset) {
106                  regionLength = regionLength - (eventOffset - region.getOffset());
107             }
108             
109             String JavaDoc lineLine = document.get(eventOffset, regionLength);
110             Matcher JavaDoc matcher = null;
111             synchronized (fgLineNumberPattern) {
112                 matcher = fgLineNumberPattern.matcher(lineLine);
113             }
114             if (matcher.find()) {
115                 String JavaDoc lineString = matcher.group();
116                 return Integer.parseInt(lineString);
117             }
118             
119         } catch (BadLocationException e) {
120             AntUIPlugin.log(e);
121         } catch (NumberFormatException JavaDoc e) {
122             AntUIPlugin.log(e);
123         }
124         return -1;
125     }
126     
127     protected void addLink(String JavaDoc filePath, int lineNumber, int offset, int length, Integer JavaDoc type) {
128         IFile file= getIFile(filePath);
129         if (file == null) {
130             return;
131         }
132         
133         if (fMarkerCreator != null) {
134             if (type == null) { //match for listfiles
135
fMarkerCreator.addFileToBeCleaned(file);
136             } else { //match for error or warning
137
fMarkerCreator.addMarker(file, lineNumber, offset, length, type);
138             }
139         }
140         
141         FileLink link = new FileLink(file, null, -1, -1, lineNumber);
142         try {
143             fConsole.addHyperlink(link, offset, length);
144         } catch (BadLocationException e) {
145             AntUIPlugin.log(e);
146         }
147     }
148
149     public static void consoleClosed(IProcess process) {
150         Iterator JavaDoc iter= new ArrayList JavaDoc(fgPatternMatchers).iterator();
151         while (iter.hasNext()) {
152             AbstractJavacPatternMatcher matcher = (AbstractJavacPatternMatcher) iter.next();
153             matcher.finished(process);
154         }
155     }
156
157     protected void finished(IProcess process) {
158         if (fMarkerCreator != null) {
159             fMarkerCreator.finished(process);
160             fgPatternMatchers.remove(this);
161         }
162     }
163 }
Popular Tags