1 11 package org.eclipse.ant.internal.ui.console; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.regex.Matcher ; 19 import java.util.regex.Pattern ; 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 fgLineNumberPattern = Pattern.compile("\\d+"); private static List fgPatternMatchers= new ArrayList (); 42 private Map fFileNameToIFile= new HashMap (); 43 44 private JavacMarkerCreator fMarkerCreator; 45 protected static final Integer fgWarningType= new Integer (IMarker.SEVERITY_WARNING); 46 protected static final Integer fgErrorType= new Integer (IMarker.SEVERITY_ERROR); 47 48 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 63 public void disconnect() { 64 fConsole = null; 65 fFileNameToIFile.clear(); 66 } 67 68 protected IFile getIFile(String 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 getMatchedText(PatternMatchEvent event) { 84 int eventOffset= event.getOffset(); 85 int eventLength= event.getLength(); 86 IDocument document= fConsole.getDocument(); 87 String 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 lineLine = document.get(eventOffset, regionLength); 110 Matcher matcher = null; 111 synchronized (fgLineNumberPattern) { 112 matcher = fgLineNumberPattern.matcher(lineLine); 113 } 114 if (matcher.find()) { 115 String lineString = matcher.group(); 116 return Integer.parseInt(lineString); 117 } 118 119 } catch (BadLocationException e) { 120 AntUIPlugin.log(e); 121 } catch (NumberFormatException e) { 122 AntUIPlugin.log(e); 123 } 124 return -1; 125 } 126 127 protected void addLink(String filePath, int lineNumber, int offset, int length, Integer type) { 128 IFile file= getIFile(filePath); 129 if (file == null) { 130 return; 131 } 132 133 if (fMarkerCreator != null) { 134 if (type == null) { fMarkerCreator.addFileToBeCleaned(file); 136 } else { 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 iter= new ArrayList (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 |