|
Removed
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 - 2006 University Of British Columbia 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 |
* University Of British Columbia - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.mylar.internal.ide.xml.pde; |
| 13 |
|
| 14 |
import java.util.Collections; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
import org.eclipse.core.internal.resources.File; |
| 18 |
import org.eclipse.core.internal.resources.Workspace; |
| 19 |
import org.eclipse.core.resources.IFile; |
| 20 |
import org.eclipse.core.resources.IResource; |
| 21 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 22 |
import org.eclipse.core.runtime.IPath; |
| 23 |
import org.eclipse.core.runtime.Path; |
| 24 |
import org.eclipse.jface.text.Document; |
| 25 |
import org.eclipse.jface.text.IDocument; |
| 26 |
import org.eclipse.mylar.context.core.AbstractContextStructureBridge; |
| 27 |
import org.eclipse.mylar.context.core.ContextCorePlugin; |
| 28 |
import org.eclipse.mylar.context.core.IMylarElement; |
| 29 |
import org.eclipse.mylar.core.MylarStatusHandler; |
| 30 |
import org.eclipse.mylar.internal.ide.xml.XmlNodeHelper; |
| 31 |
import org.eclipse.pde.internal.core.text.build.BuildEntry; |
| 32 |
import org.eclipse.pde.internal.core.text.plugin.PluginNode; |
| 33 |
import org.eclipse.pde.internal.core.text.plugin.PluginObjectNode; |
| 34 |
import org.eclipse.pde.internal.ui.editor.PDEFormPage; |
| 35 |
import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor; |
| 36 |
import org.eclipse.ui.IEditorPart; |
| 37 |
import org.eclipse.ui.PlatformUI; |
| 38 |
import org.eclipse.ui.views.markers.internal.ProblemMarker; |
| 39 |
|
| 40 |
/** |
| 41 |
* @author Mik Kersten |
| 42 |
* |
| 43 |
*/ |
| 44 |
public class PdeStructureBridge extends AbstractContextStructureBridge { |
| 45 |
|
| 46 |
public final static String CONTENT_TYPE = "plugin.xml"; |
| 47 |
|
| 48 |
@Override |
| 49 |
public String getContentType() { |
| 50 |
return CONTENT_TYPE; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public String getContentType(String elementHandle) { |
| 55 |
if (elementHandle.endsWith(".xml")) { |
| 56 |
return parentContentType; |
| 57 |
// return parentBridge.getContentType(); |
| 58 |
} else { |
| 59 |
return CONTENT_TYPE; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public List<String> getChildHandles(String handle) { |
| 65 |
return Collections.emptyList(); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public String getParentHandle(String handle) { |
| 70 |
// we can only get the parent if we have a PluginObjectNode |
| 71 |
|
| 72 |
Object object = getObjectForHandle(handle); |
| 73 |
if (object instanceof PluginObjectNode) { |
| 74 |
PluginObjectNode parent = (PluginObjectNode) ((PluginObjectNode) object).getParentNode(); |
| 75 |
if (parent != null) { |
| 76 |
return getHandleIdentifier(parent); |
| 77 |
} else { |
| 78 |
// the parent is the plugin.xml file, so return that handle |
| 79 |
int delimeterIndex = handle.indexOf(";"); |
| 80 |
if (delimeterIndex != -1) { |
| 81 |
String parentHandle = handle.substring(0, delimeterIndex); |
| 82 |
return parentHandle; |
| 83 |
} else { |
| 84 |
return null; |
| 85 |
} |
| 86 |
} |
| 87 |
} else if (object instanceof PluginNode) { |
| 88 |
PluginNode parent = (PluginNode) ((PluginNode) object).getParentNode(); |
| 89 |
if (parent != null) { |
| 90 |
return getHandleIdentifier(parent); |
| 91 |
} else { |
| 92 |
// the parent is the plugin.xml file, so return that handle |
| 93 |
int delimeterIndex = handle.indexOf(";"); |
| 94 |
if (delimeterIndex != -1) { |
| 95 |
String parentHandle = handle.substring(0, delimeterIndex); |
| 96 |
return parentHandle; |
| 97 |
} else { |
| 98 |
return null; |
| 99 |
} |
| 100 |
} |
| 101 |
} else if (object instanceof IFile) { |
| 102 |
// String fileHandle = parentBridge.getParentHandle(handle); |
| 103 |
AbstractContextStructureBridge parentBridge = ContextCorePlugin.getDefault().getStructureBridge(parentContentType); |
| 104 |
return parentBridge.getParentHandle(handle); |
| 105 |
} else { |
| 106 |
return null; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @see org.eclipse.mylar.context.core.AbstractContextStructureBridge#getObjectForHandle(java.lang.String) |
| 112 |
*/ |
| 113 |
@Override |
| 114 |
public Object getObjectForHandle(String handle) { |
| 115 |
if (handle == null) |
| 116 |
return null; |
| 117 |
int first = handle.indexOf(";"); |
| 118 |
String filename = ""; |
| 119 |
if (first == -1) { |
| 120 |
AbstractContextStructureBridge parentBridge = ContextCorePlugin.getDefault().getStructureBridge(parentContentType); |
| 121 |
return parentBridge.getObjectForHandle(handle); |
| 122 |
} else { |
| 123 |
// extract the filename from the handle since it represents a node |
| 124 |
filename = handle.substring(0, first); |
| 125 |
} |
| 126 |
|
| 127 |
try { |
| 128 |
// get the file and create a FileEditorInput |
| 129 |
IPath path = new Path(filename); |
| 130 |
IFile f = (IFile) ((Workspace) ResourcesPlugin.getWorkspace()).newResource(path, IResource.FILE); |
| 131 |
|
| 132 |
// get the start line for the element |
| 133 |
int start = Integer.parseInt(handle.substring(first + 1)); |
| 134 |
|
| 135 |
// // get the content and the document so that we can get the offset |
| 136 |
// String content = XmlNodeHelper.getContents(f.getContents()); |
| 137 |
// IDocument d = new Document(content); |
| 138 |
|
| 139 |
// get the offsets for the element |
| 140 |
// make sure that we are on a character and not whitespace |
| 141 |
// int offset = d.getLineOffset(start); |
| 142 |
// while(d.getChar(offset) == ' ') |
| 143 |
// offset++; |
| 144 |
|
| 145 |
// get the current editor which should be the ManifestEditor so that |
| 146 |
// we can get the element that we want |
| 147 |
IEditorPart editorPart = null; |
| 148 |
try { |
| 149 |
editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); |
| 150 |
} catch (NullPointerException e) { |
| 151 |
// do nothing, this just means that there is no active page |
| 152 |
} |
| 153 |
if (editorPart != null && editorPart instanceof ManifestEditor) { |
| 154 |
PluginObjectNode node = PdeEditingMonitor.getNode((ManifestEditor) editorPart, start, true); |
| 155 |
// get the element based on the offset |
| 156 |
return node; |
| 157 |
} else { |
| 158 |
String content = XmlNodeHelper.getContents(f.getContents()); |
| 159 |
IDocument d = new Document(content); |
| 160 |
PluginObjectNode node = PdeEditingMonitor.getNode(d, f, start, true); |
| 161 |
return node; |
| 162 |
} |
| 163 |
} catch (Exception e) { |
| 164 |
// ignore, means file doesn't exist |
| 165 |
// ContextCorePlugin.log(e, "handle failed"); |
| 166 |
} |
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Handle is filename;hashcodeOfElementAndAttributes |
| 172 |
* |
| 173 |
* @see org.eclipse.mylar.context.core.AbstractContextStructureBridge#getHandleIdentifier(java.lang.Object) |
| 174 |
*/ |
| 175 |
@Override |
| 176 |
public String getHandleIdentifier(Object object) { |
| 177 |
// we can only create handles for PluginObjectNodes and plugin.xml files |
| 178 |
if (object instanceof XmlNodeHelper) { |
| 179 |
return ((XmlNodeHelper) object).getHandle(); |
| 180 |
} else if (object instanceof PluginObjectNode) { |
| 181 |
PluginObjectNode node = (PluginObjectNode) object; |
| 182 |
try { |
| 183 |
// get the handle for the PluginObjectNode |
| 184 |
if (node.getModel() == null || node.getModel().getUnderlyingResource() == null |
| 185 |
|| node.getModel().getUnderlyingResource().getFullPath() == null) { |
| 186 |
// ContextCorePlugin.log("PDE xml node's resource or model is |
| 187 |
// null: " + node.getName(), this); |
| 188 |
return null; |
| 189 |
} |
| 190 |
IPath path = new Path(node.getModel().getUnderlyingResource().getFullPath().toString()); |
| 191 |
IFile file = (IFile) ((Workspace) ResourcesPlugin.getWorkspace()).newResource(path, IResource.FILE); |
| 192 |
String handle = new XmlNodeHelper(file.getFullPath().toString(), PdeEditingMonitor |
| 193 |
.getStringOfNode(node).hashCode()).getHandle(); |
| 194 |
return handle; |
| 195 |
} catch (Exception e) { |
| 196 |
MylarStatusHandler.log(e, "pde handle failed"); |
| 197 |
} |
| 198 |
} else if (object instanceof PluginNode) { |
| 199 |
PluginNode node = (PluginNode) object; |
| 200 |
try { |
| 201 |
if (node.getModel() == null || node.getModel().getUnderlyingResource() == null |
| 202 |
|| node.getModel().getUnderlyingResource().getFullPath() == null) { |
| 203 |
// ContextCorePlugin.log("PDE xml node's resource or model is |
| 204 |
// null: " + node.getName(), this); |
| 205 |
return null; |
| 206 |
} |
| 207 |
IPath path = new Path(node.getModel().getUnderlyingResource().getFullPath().toString()); |
| 208 |
IFile file = (IFile) ((Workspace) ResourcesPlugin.getWorkspace()).newResource(path, IResource.FILE); |
| 209 |
String handle = new XmlNodeHelper(file.getFullPath().toString(), PdeEditingMonitor |
| 210 |
.getStringOfNode(node).hashCode()).getHandle(); |
| 211 |
return handle; |
| 212 |
} catch (Exception e) { |
| 213 |
MylarStatusHandler.log(e, "pde handle failed"); |
| 214 |
} |
| 215 |
|
| 216 |
} else if (object instanceof File) { |
| 217 |
// get the handle for the file if it is plugin.xml |
| 218 |
File file = (File) object; |
| 219 |
if (file.getFullPath().toString().endsWith("plugin.xml")) |
| 220 |
return file.getFullPath().toString(); |
| 221 |
} |
| 222 |
return null; |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public String getName(Object object) { |
| 227 |
if (object instanceof PluginObjectNode) { |
| 228 |
PluginObjectNode node = (PluginObjectNode) object; |
| 229 |
String name = node.getXMLAttributeValue("name"); |
| 230 |
if (name == null) |
| 231 |
name = node.getXMLTagName(); |
| 232 |
name = node.getModel().getUnderlyingResource().getName() + ": " + name; |
| 233 |
return name; |
| 234 |
} else if (object instanceof File) { |
| 235 |
File file = (File) object; |
| 236 |
if (file.getFullPath().toString().endsWith("plugin.xml")) |
| 237 |
return "plugin.xml"; |
| 238 |
} |
| 239 |
return ""; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* TODO: make a non-handle based test |
| 244 |
*/ |
| 245 |
@Override |
| 246 |
public boolean canBeLandmark(String handle) { |
| 247 |
if (handle == null) { |
| 248 |
return false; |
| 249 |
} else { |
| 250 |
return handle.indexOf(';') == -1; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public boolean acceptsObject(Object object) { |
| 256 |
// we only accept PluginObjectNodes and plugin.xml Files |
| 257 |
if (object instanceof PluginNode || object instanceof PluginObjectNode || object instanceof BuildEntry || object instanceof PDEFormPage) { |
| 258 |
return true; |
| 259 |
} else if (object instanceof XmlNodeHelper) { |
| 260 |
if (((XmlNodeHelper) object).getFilename().endsWith("plugin.xml")) |
| 261 |
return true; |
| 262 |
} else if (object instanceof File) { |
| 263 |
File file = (File) object; |
| 264 |
if (file.getFullPath().toString().endsWith("plugin.xml")) |
| 265 |
return true; |
| 266 |
} |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
@Override |
| 271 |
public boolean canFilter(Object element) { |
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
public boolean isDocument(String handle) { |
| 277 |
return handle.indexOf(';') == -1; |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public String getHandleForOffsetInObject(Object resource, int offset) { |
| 282 |
if (resource == null) |
| 283 |
return null; |
| 284 |
if (resource instanceof ProblemMarker) { |
| 285 |
ProblemMarker marker = (ProblemMarker) resource; |
| 286 |
|
| 287 |
// we can only get a handle for a marker with the resource |
| 288 |
// plugin.xml |
| 289 |
try { |
| 290 |
IResource res = marker.getResource(); |
| 291 |
|
| 292 |
if (res instanceof IFile) { |
| 293 |
IFile file = (IFile) res; |
| 294 |
if (file.getFullPath().toString().endsWith("plugin.xml")) { |
| 295 |
return file.getFullPath().toString(); |
| 296 |
} else { |
| 297 |
return null; |
| 298 |
} |
| 299 |
} |
| 300 |
return null; |
| 301 |
} catch (Throwable t) { |
| 302 |
MylarStatusHandler.log(t, "Could not find element for: " + marker); |
| 303 |
return null; |
| 304 |
} |
| 305 |
} else if (resource instanceof IFile) { |
| 306 |
try { |
| 307 |
IFile file = (IFile) resource; |
| 308 |
if (file.getFullPath().toString().endsWith("plugin.xml")) { |
| 309 |
String content = XmlNodeHelper.getContents(file.getContents()); |
| 310 |
IDocument d = new Document(content); |
| 311 |
PluginObjectNode node = PdeEditingMonitor.getNode(d, file, offset, false); |
| 312 |
String handle = new XmlNodeHelper(file.getFullPath().toString(), PdeEditingMonitor.getStringOfNode( |
| 313 |
node).hashCode()).getHandle(); |
| 314 |
return handle; |
| 315 |
} |
| 316 |
} catch (Exception e) { |
| 317 |
MylarStatusHandler.log(e, "Unable to get handle for offset in object"); |
| 318 |
} |
| 319 |
} |
| 320 |
return null; |
| 321 |
} |
| 322 |
|
| 323 |
// public IProject getProjectForObject(Object object) { |
| 324 |
// while (!(object instanceof IFile)) { |
| 325 |
// String handle = getParentHandle(getHandleIdentifier(object)); |
| 326 |
// if (handle == null) |
| 327 |
// break; |
| 328 |
// object = getObjectForHandle(handle); |
| 329 |
// } |
| 330 |
// if (object instanceof IFile && acceptsObject(object)) { |
| 331 |
// return ((IFile) object).getProject(); |
| 332 |
// } |
| 333 |
// return null; |
| 334 |
// } |
| 335 |
|
| 336 |
public boolean containsProblem(IMylarElement node) { |
| 337 |
// TODO Auto-generated method stub |
| 338 |
return false; |
| 339 |
} |
| 340 |
} |