Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 91c9bc45b35957638d7d470d169dfc714abaa787 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core.variables;

import java.io.File;
import java.net.URI;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.variables.IDynamicVariable;
import org.eclipse.core.variables.IDynamicVariableResolver;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.DebugPlugin;

import com.ibm.icu.text.MessageFormat;

/**
 * Common function of variable resolvers.
 * Moved to debug core in 3.5, existed in debug.iu since 3.0.
 * 
 * @since 3.5
 */
public class ResourceResolver implements IDynamicVariableResolver {

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.core.stringsubstitution.IContextVariableResolver#resolveValue(org.eclipse.debug.internal.core.stringsubstitution.IContextVariable, java.lang.String)
	 */
	public String resolveValue(IDynamicVariable variable, String argument) throws CoreException {
		IResource resource = null;
		if (argument == null) {
			resource = getSelectedResource(variable);
		} else {
			resource = getWorkspaceRoot().findMember(new Path(argument));
		}
		if (resource != null && resource.exists()) {
			resource = translateSelectedResource(resource);
			if (resource != null && resource.exists()) {
				return translateToValue(resource, variable);
			}
		}
		abort(MessageFormat.format(Messages.ResourceResolver_0, new String[]{getReferenceExpression(variable, argument)}), null);
		return null;
	}
	
	/**
	 * Returns the resource applicable to this resolver, relative to the selected
	 * resource. This method is called when no argument is present in a variable
	 * expression. For, example, this method might return the project for the
	 * selected resource.
	 * 
	 * @param resource selected resource
	 * @return resource applicable to this variable resolver
	 */
	protected IResource translateSelectedResource(IResource resource) {
		return resource;
	}
	
	/**
	 * Returns the workspace root
	 * 
	 * @return workspace root
	 */
	protected IWorkspaceRoot getWorkspaceRoot() {
		return ResourcesPlugin.getWorkspace().getRoot();
	}

	/**
	 * Returns an expression used to reference the given variable and optional argument.
	 * For example, <code>${var_name:arg}</code>.
	 * 
	 * @param variable referenced variable
	 * @param argument referenced argument or <code>null</code>
	 * @return variable reference expression
	 */
	protected String getReferenceExpression(IDynamicVariable variable, String argument) {
		StringBuffer reference = new StringBuffer();
		reference.append("${"); //$NON-NLS-1$
		reference.append(variable.getName());
		if (argument != null) {
			reference.append(":"); //$NON-NLS-1$
			reference.append(argument);
		}
		reference.append("}"); //$NON-NLS-1$
		return reference.toString();
	}
	
	/**
	 * Throws an exception with the given message and underlying exception.
	 *  
	 * @param message exception message
	 * @param exception underlying exception or <code>null</code> 
	 * @throws CoreException
	 */
	protected void abort(String message, Throwable exception) throws CoreException {
		throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, message, exception));
	}
	
	/**
	 * Returns the selected resource. Uses the ${selected_resource_path} variable
	 * to determine the selected resource. This variable is provided by the debug.ui
	 * plug-in. Selected resource resolution is only available when the debug.ui
	 * plug-in is present.
	 * 
	 * @param variable variable referencing a resource
	 * @return selected resource
	 * @throws CoreException if there is no selection
	 */
	protected IResource getSelectedResource(IDynamicVariable variable) throws CoreException {
		IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
		try {
			String pathString = manager.performStringSubstitution("${selected_resource_path}"); //$NON-NLS-1$
			return ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(pathString));
		} catch (CoreException e) {
			// unable to resolve a selection
		}
		abort(MessageFormat.format(Messages.ResourceResolver_1, new String[]{getReferenceExpression(variable, null)}), null);
		return null;	
	}

	/**
	 * Translates the given resource into a value for this variable resolver.
	 * 
	 * @param resource the resource applicable to this resolver's variable
	 * @param variable the variable being resolved
	 * @return variable value
	 * @throws CoreException if the variable name is not recognized
	 */
	protected String translateToValue(IResource resource, IDynamicVariable variable) throws CoreException {
		String name = variable.getName();
		IPath path = null;
		URI uri = null;
		if (name.endsWith("_loc")) { //$NON-NLS-1$
			uri = resource.getLocationURI();
			if(uri != null) {
				File file = EFS.getStore(uri).toLocalFile(0, null);
				if(file != null) {
					return file.getAbsolutePath();
				}
			}
		} else if (name.endsWith("_path")) { //$NON-NLS-1$
			path = resource.getFullPath();
			if(path != null) {
				return path.toOSString();
			}
		} else if (name.endsWith("_name")) { //$NON-NLS-1$
			return resource.getName();
		}
		abort(MessageFormat.format(Messages.ResourceResolver_2, new String[]{getReferenceExpression(variable, null)}), null);
		return null;
	}

}

Back to the top