Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 David Green 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 |
* David Green - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
|
12 |
package org.eclipse.mylyn.internal.wikitext.tasks.ui; |
13 |
|
14 |
import java.io.BufferedInputStream; |
15 |
import java.io.InputStreamReader; |
16 |
import java.io.Reader; |
17 |
import java.io.StringWriter; |
18 |
import java.util.ArrayList; |
19 |
import java.util.Collections; |
20 |
import java.util.List; |
21 |
|
22 |
import org.eclipse.core.resources.IFile; |
23 |
import org.eclipse.core.resources.IMarker; |
24 |
import org.eclipse.core.resources.IResource; |
25 |
import org.eclipse.core.resources.ResourcesPlugin; |
26 |
import org.eclipse.core.runtime.CoreException; |
27 |
import org.eclipse.core.runtime.IPath; |
28 |
import org.eclipse.core.runtime.Path; |
29 |
import org.eclipse.core.runtime.content.IContentDescription; |
30 |
import org.eclipse.core.runtime.content.IContentType; |
31 |
import org.eclipse.mylyn.context.core.AbstractContextStructureBridge; |
32 |
import org.eclipse.mylyn.wikitext.core.WikiText; |
33 |
import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage; |
34 |
import org.eclipse.mylyn.wikitext.core.parser.outline.OutlineItem; |
35 |
import org.eclipse.mylyn.wikitext.core.parser.outline.OutlineParser; |
36 |
import org.eclipse.ui.IEditorPart; |
37 |
import org.eclipse.ui.PlatformUI; |
38 |
|
39 |
/** |
40 |
* A Mylyn {@link AbstractContextStructureBridge context structure bridge} for wikitext files. |
41 |
* |
42 |
* @author David Green |
43 |
*/ |
44 |
public class WikiTextContextStructureBridge extends AbstractContextStructureBridge { |
45 |
|
46 |
private static final char HANDLE_FILE_SEPARATOR = ';'; |
47 |
|
48 |
public static final String CONTENT_TYPE = WikiText.CONTENT_TYPE; |
49 |
|
50 |
public WikiTextContextStructureBridge() { |
51 |
} |
52 |
|
53 |
@Override |
54 |
public boolean acceptsObject(Object object) { |
55 |
if (object instanceof OutlineItem) { |
56 |
return true; |
57 |
} else if (object instanceof IFile) { |
58 |
IFile file = (IFile) object; |
59 |
|
60 |
try { |
61 |
IContentDescription description = file.getContentDescription(); |
62 |
if (description != null) { |
63 |
IContentType contentType = description.getContentType(); |
64 |
if (contentType != null) { |
65 |
if (isWikiText(contentType)) { |
66 |
return true; |
67 |
} |
68 |
} |
69 |
} |
70 |
} catch (CoreException e) { |
71 |
// ignore |
72 |
} |
73 |
|
74 |
String languageName = WikiText.getMarkupLanguageNameForFilename(file.getName()); |
75 |
return languageName != null; |
76 |
} |
77 |
return false; |
78 |
} |
79 |
|
80 |
private boolean isWikiText(IContentType contentType) { |
81 |
if (WikiText.CONTENT_TYPE.equals(contentType.getId())) { |
82 |
return true; |
83 |
} |
84 |
IContentType baseType = contentType.getBaseType(); |
85 |
if (baseType != null) { |
86 |
return isWikiText(baseType); |
87 |
} |
88 |
return false; |
89 |
} |
90 |
|
91 |
@Override |
92 |
public boolean canBeLandmark(String handle) { |
93 |
if (handle == null) { |
94 |
return false; |
95 |
} else { |
96 |
return handle.indexOf(HANDLE_FILE_SEPARATOR) != -1; |
97 |
} |
98 |
} |
99 |
|
100 |
@Override |
101 |
public boolean canFilter(Object element) { |
102 |
return true; |
103 |
} |
104 |
|
105 |
@Override |
106 |
public List<String> getChildHandles(String handle) { |
107 |
Object object = getObjectForHandle(handle); |
108 |
if (object instanceof OutlineItem) { |
109 |
OutlineItem item = (OutlineItem) object; |
110 |
if (!item.getChildren().isEmpty()) { |
111 |
List<String> handles = new ArrayList<String>(item.getChildren().size()); |
112 |
for (OutlineItem child : item.getChildren()) { |
113 |
handles.add(getHandleIdentifier(child)); |
114 |
} |
115 |
return handles; |
116 |
} |
117 |
} |
118 |
return Collections.emptyList(); |
119 |
} |
120 |
|
121 |
@Override |
122 |
public String getContentType() { |
123 |
return CONTENT_TYPE; |
124 |
} |
125 |
|
126 |
@Override |
127 |
public String getContentType(String elementHandle) { |
128 |
if (elementHandle.indexOf(HANDLE_FILE_SEPARATOR) == -1) { |
129 |
return parentContentType; |
130 |
} |
131 |
return CONTENT_TYPE; |
132 |
} |
133 |
|
134 |
@Override |
135 |
public String getHandleForOffsetInObject(Object object, int offset) { |
136 |
IResource resource = null; |
137 |
try { |
138 |
if (object instanceof IResource) { |
139 |
resource = (IResource) object; |
140 |
} else if (object instanceof IMarker) { |
141 |
resource = ((IMarker) object).getResource(); |
142 |
} else { |
143 |
try { |
144 |
// works with ConcreteMarker without creating a compile-time dependency on internals |
145 |
IMarker marker = (IMarker) object.getClass().getMethod("getMarker").invoke(object); //$NON-NLS-1$ |
146 |
resource = marker.getResource(); |
147 |
} catch (Exception e) { |
148 |
// ignore |
149 |
} |
150 |
} |
151 |
} catch (Exception e) { |
152 |
return null; |
153 |
} |
154 |
if (resource instanceof IFile) { |
155 |
IFile file = (IFile) resource; |
156 |
if (acceptsObject(file)) { |
157 |
OutlineItem outline = getOutline(file); |
158 |
if (outline != null) { |
159 |
OutlineItem item = outline.findNearestMatchingOffset(offset); |
160 |
if (item != null) { |
161 |
return getHandleIdentifier(item); |
162 |
} |
163 |
} |
164 |
} |
165 |
} |
166 |
return null; |
167 |
} |
168 |
|
169 |
@Override |
170 |
public String getLabel(Object object) { |
171 |
if (object instanceof OutlineItem) { |
172 |
OutlineItem item = (OutlineItem) object; |
173 |
if (item.getParent() == null) { |
174 |
return getFile(item).getName(); |
175 |
} else { |
176 |
return item.getLabel(); |
177 |
} |
178 |
} |
179 |
return ""; //$NON-NLS-1$ |
180 |
} |
181 |
|
182 |
@Override |
183 |
public Object getObjectForHandle(String handle) { |
184 |
if (handle == null) { |
185 |
return null; |
186 |
} |
187 |
int idxOfSeparator = handle.indexOf(HANDLE_FILE_SEPARATOR); |
188 |
String filename = handle; |
189 |
if (idxOfSeparator != -1) { |
190 |
filename = handle.substring(0, idxOfSeparator); |
191 |
} |
192 |
IFile file; |
193 |
try { |
194 |
file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filename)); |
195 |
} catch (Exception e) { |
196 |
// be error-tolerant since we don't know much about the handle at this point |
197 |
return null; |
198 |
} |
199 |
if (file != null) { |
200 |
if (idxOfSeparator != -1) { |
201 |
String headingId = handle.substring(idxOfSeparator + 1); |
202 |
OutlineItem outline = getOutline(file); |
203 |
if (outline != null) { |
204 |
OutlineItem item = outline.findItemById(headingId); |
205 |
return item; |
206 |
} |
207 |
} else { |
208 |
return file; |
209 |
} |
210 |
} |
211 |
return null; |
212 |
} |
213 |
|
214 |
@Override |
215 |
public String getParentHandle(String handle) { |
216 |
Object object = getObjectForHandle(handle); |
217 |
if (object instanceof OutlineItem) { |
218 |
OutlineItem item = (OutlineItem) object; |
219 |
if (item.getParent() != null) { |
220 |
return getHandleIdentifier(item.getParent()); |
221 |
} |
222 |
} |
223 |
return null; |
224 |
} |
225 |
|
226 |
@Override |
227 |
public boolean isDocument(String handle) { |
228 |
Object object = getObjectForHandle(handle); |
229 |
if (object instanceof OutlineItem) { |
230 |
OutlineItem item = (OutlineItem) object; |
231 |
return item.getParent() == null; |
232 |
} |
233 |
return false; |
234 |
} |
235 |
|
236 |
@Override |
237 |
public String getHandleIdentifier(Object object) { |
238 |
if (object instanceof OutlineItem) { |
239 |
OutlineItem item = (OutlineItem) object; |
240 |
return item.getResourcePath() + HANDLE_FILE_SEPARATOR + item.getId(); |
241 |
} else if (object instanceof IFile && acceptsObject(object)) { |
242 |
return ((IFile) object).getFullPath().toString(); |
243 |
} |
244 |
return null; |
245 |
} |
246 |
|
247 |
private IPath getFullPath(OutlineItem item) { |
248 |
String resourcePath = item.getResourcePath(); |
249 |
return resourcePath == null ? null : new Path(resourcePath); |
250 |
} |
251 |
|
252 |
private OutlineItem getOutline(IFile file) { |
253 |
// FIXME: is editor integration the way to go?? we probably need some kind of core model |
254 |
IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); |
255 |
if (editorPart != null) { |
256 |
OutlineItem outline = (OutlineItem) editorPart.getAdapter(OutlineItem.class); |
257 |
if (outline != null) { |
258 |
return outline; |
259 |
} |
260 |
} |
261 |
MarkupLanguage markupLanguage = WikiText.getMarkupLanguageForFilename(file.getName()); |
262 |
if (markupLanguage != null) { |
263 |
OutlineParser parser = new OutlineParser(markupLanguage); |
264 |
try { |
265 |
String contents = getContents(file); |
266 |
OutlineItem outline = parser.parse(contents); |
267 |
outline.setResourcePath(file.getFullPath().toString()); |
268 |
return outline; |
269 |
} catch (Exception e) { |
270 |
// ignore |
271 |
return null; |
272 |
} |
273 |
} |
274 |
return null; |
275 |
} |
276 |
|
277 |
private String getContents(IFile file) throws Exception { |
278 |
String charset = file.getCharset(); |
279 |
StringWriter writer = new StringWriter(); |
280 |
Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents()), charset); |
281 |
int i; |
282 |
while ((i = reader.read()) != -1) { |
283 |
writer.write(i); |
284 |
} |
285 |
return writer.toString(); |
286 |
} |
287 |
|
288 |
IFile getFile(OutlineItem item) { |
289 |
IPath fullPath = getFullPath(item); |
290 |
|
291 |
return fullPath == null ? null : ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath); |
292 |
} |
293 |
} |