1 package net.sourceforge.phpdt.externaltools.variable;
3 /**********************************************************************
4 Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
10 **********************************************************************/
12 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.IWorkspaceRoot;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.IPath;
20 * Expands a resource type variable into the desired
23 * This class is not intended to be extended by clients.
26 public class ResourceExpander implements IVariableLocationExpander, IVariableResourceExpander, IVariableTextExpander {
31 public ResourceExpander() {
36 * Expands the variable to a resource.
38 /*package*/ IResource expand(String varValue, ExpandVariableContext context) {
39 if (varValue != null && varValue.length() > 0) {
40 return expandToMember(varValue);
42 return expandUsingContext(context);
47 * Expands using the current context information.
48 * By default, return the selected resource of the
51 /*package*/ IResource expandUsingContext(ExpandVariableContext context) {
52 return context.getSelectedResource();
56 * Expands the variable value to a resource. The value
57 * will not be <code>null</code> nor empty. By default,
58 * lookup the member from the workspace root.
60 /*package*/ IResource expandToMember(String varValue) {
61 return getWorkspaceRoot().findMember(varValue);
65 * Method declared on IVariableLocationExpander.
67 public IPath getPath(String varTag, String varValue, ExpandVariableContext context) {
68 IResource resource = expand(varValue, context);
69 if (resource != null) {
70 if (isPathVariable(varTag)) {
71 return resource.getFullPath();
73 return resource.getLocation();
81 * Returns whether the given variable tag is a known path
82 * variable tag. Path variable tags represent variables that
83 * expand to paths relative to the workspace root.
85 private boolean isPathVariable(String varTag) {
86 return varTag.equals(IExternalToolConstants.VAR_CONTAINER_PATH) ||
87 varTag.equals(IExternalToolConstants.VAR_PROJECT_PATH) ||
88 varTag.equals(IExternalToolConstants.VAR_RESOURCE_PATH);
92 * Method declared on IVariableResourceExpander.
94 public IResource[] getResources(String varTag, String varValue, ExpandVariableContext context) {
95 IResource resource = expand(varValue, context);
96 if (resource != null) {
97 return new IResource[] {resource};
104 * Returns the workspace root resource.
106 protected final IWorkspaceRoot getWorkspaceRoot() {
107 return ResourcesPlugin.getWorkspace().getRoot();
111 * Returns a string representation of the path to a file or directory
112 * for the given variable tag and value or <code>null</code>.
114 * @see IVariableTextExpander#getText(String, String, ExpandVariableContext)
116 public String getText(String varTag, String varValue, ExpandVariableContext context) {
117 IPath path= getPath(varTag, varValue, context);
119 return path.toString();