KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > eclipse > debug > FreemarkerConsoleLineTracker


1 package freemarker.eclipse.debug;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.LinkedList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.regex.Matcher JavaDoc;
7 import java.util.regex.Pattern JavaDoc;
8
9 import org.eclipse.core.resources.IContainer;
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.core.resources.IProject;
12 import org.eclipse.core.resources.IResource;
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
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.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IRegion;
25
26 /**
27  * @version $Id: FreemarkerConsoleLineTracker.java,v 1.1 2005/01/25 18:59:29 stephanmueller Exp $
28  * @author <a HREF="mailto:stephan&#64;chaquotay.net">Stephan Mueller </a>
29  */

30 public class FreemarkerConsoleLineTracker implements IConsoleLineTracker {
31
32     private IConsole console;
33     private static final Pattern JavaDoc pattern = Pattern.compile("^.*\\[on line (\\d+), column (\\d+) in (\\S+)\\]$");
34
35     /*
36      * (non-Javadoc)
37      *
38      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#init(org.eclipse.debug.ui.console.IConsole)
39      */

40     public void init(IConsole console) {
41         this.console = console;
42     }
43
44     /*
45      * (non-Javadoc)
46      *
47      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#lineAppended(org.eclipse.jface.text.IRegion)
48      */

49     public void lineAppended(IRegion line) {
50         try {
51             String JavaDoc text = console.getDocument().get(line.getOffset(), line.getLength());
52             Matcher JavaDoc m = pattern.matcher(text);
53             if (m.matches()) {
54                 String JavaDoc res = m.group(3);
55                 int l = Integer.parseInt(m.group(1));
56                 int c = Integer.parseInt(m.group(2));
57
58                 int linkOffset = line.getOffset() + m.start(1) - 9;
59                 int linkLength = line.getLength() - m.start(1) + 9;
60
61                 IPath path = new Path(res);
62
63                 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
64                 
65                 List JavaDoc files = new ArrayList JavaDoc();
66                 for (int i = 0; i < projects.length; i++) {
67                     IProject project = projects[i];
68                     IJavaProject jproject = JavaCore.create(project);
69                     
70                     IFile[] foundFiles = retrieveFiles(project, res);
71
72                     for (int j = 0; j < foundFiles.length; j++) {
73                         IFile file = foundFiles[j];
74                         
75                         boolean isInsideOutputLocation = false;
76                         
77                         try {
78                             // check whether the file is located inside a Java project's output location
79
isInsideOutputLocation = jproject.getOutputLocation().isPrefixOf(file.getFullPath());
80                         } catch (JavaModelException e1) {
81                             // noop; isInsideOutputLocation = false
82
}
83                         
84                         if(!isInsideOutputLocation) {
85                             // FTL files inside a project's output location are probably copies
86
files.add(file);
87                         }
88                     }
89                 }
90
91                 if (files.size() != 0) {
92                     List JavaDoc filteredFiles = new ArrayList JavaDoc();
93
94                     nextfile : for (int j = 0; j < files.size(); j++) {
95                         IFile aFile = (IFile) files.get(j);
96                         IContainer parent = aFile.getParent();
97                         while (parent != null) {
98                             parent = parent.getParent();
99                         }
100                         filteredFiles.add(aFile);
101                     }
102
103                     if (filteredFiles.size() != 0) {
104                         IFile file = (IFile) filteredFiles.get(0);
105                         if (file != null && file.exists()) {
106                             FileLink link = new FileLink(file, null, -1, -1, l);
107                             console.addLink(link, linkOffset, linkLength);
108                         }
109                     }
110                 }
111             }
112         } catch (BadLocationException e) {
113         }
114     }
115
116     public IFile[] retrieveFiles(IContainer container, String JavaDoc filename) {
117         String JavaDoc[] seqs = filename.split("/|\\\\");
118
119         List JavaDoc l = new LinkedList JavaDoc();
120         l.add(container);
121         List JavaDoc files = new ArrayList JavaDoc();
122         while (!l.isEmpty()) {
123             IContainer c = (IContainer) l.get(0);
124             l.remove(0);
125             try {
126                 IResource[] res = c.members();
127                 for (int i = 0; i < res.length; i++) {
128                     IResource resource = res[i];
129                     if (resource instanceof IContainer) {
130                         l.add(resource);
131                     } else if (resource instanceof IFile) {
132                         if (isCorrectFile((IFile) resource, seqs)) {
133                             IPath path = ((IFile) resource).getProjectRelativePath();
134                             files.add(resource);
135                         }
136                     }
137                 }
138             } catch (CoreException ce) {
139             }
140         }
141         IFile[] f = new IFile[files.size()];
142         files.toArray(f);
143         return f;
144     }
145
146     private boolean isCorrectFile(IFile file, String JavaDoc[] filenameSeqs) {
147         IResource temp = file;
148         for (int i = filenameSeqs.length - 1; i >= 0; i--) {
149             String JavaDoc seq = filenameSeqs[i];
150             if (!seq.equals(temp.getName())) {
151                 return false;
152             }
153             temp = temp.getParent();
154         }
155         return true;
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see org.eclipse.debug.ui.console.IConsoleLineTracker#dispose()
162      */

163     public void dispose() {
164         // do nothing here
165
}
166
167 }
Popular Tags