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 org.eclipse.core.resources.IResource;
13 import org.eclipse.core.resources.IWorkspaceRoot;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.IPath;
16 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
19 * Expands a resource type variable into the desired
22 * This class is not intended to be extended by clients.
25 public class ResourceExpander implements IVariableLocationExpander, IVariableResourceExpander, IVariableTextExpander {
30 public ResourceExpander() {
35 * Expands the variable to a resource.
37 /*package*/ IResource expand(String varValue, ExpandVariableContext context) {
38 if (varValue != null && varValue.length() > 0) {
39 return expandToMember(varValue);
41 return expandUsingContext(context);
46 * Expands using the current context information.
47 * By default, return the selected resource of the
50 /*package*/ IResource expandUsingContext(ExpandVariableContext context) {
51 return context.getSelectedResource();
55 * Expands the variable value to a resource. The value
56 * will not be <code>null</code> nor empty. By default,
57 * lookup the member from the workspace root.
59 /*package*/ IResource expandToMember(String varValue) {
60 return getWorkspaceRoot().findMember(varValue);
64 * Method declared on IVariableLocationExpander.
66 public IPath getPath(String varTag, String varValue, ExpandVariableContext context) {
67 IResource resource = expand(varValue, context);
68 if (resource != null) {
69 if (isPathVariable(varTag)) {
70 return resource.getFullPath();
72 return resource.getLocation();
80 * Returns whether the given variable tag is a known path
81 * variable tag. Path variable tags represent variables that
82 * expand to paths relative to the workspace root.
84 private boolean isPathVariable(String varTag) {
85 return varTag.equals(IExternalToolConstants.VAR_CONTAINER_PATH) ||
86 varTag.equals(IExternalToolConstants.VAR_PROJECT_PATH) ||
87 varTag.equals(IExternalToolConstants.VAR_RESOURCE_PATH);
91 * Method declared on IVariableResourceExpander.
93 public IResource[] getResources(String varTag, String varValue, ExpandVariableContext context) {
94 IResource resource = expand(varValue, context);
95 if (resource != null) {
96 return new IResource[] {resource};
103 * Returns the workspace root resource.
105 protected final IWorkspaceRoot getWorkspaceRoot() {
106 return ResourcesPlugin.getWorkspace().getRoot();
110 * Returns a string representation of the path to a file or directory
111 * for the given variable tag and value or <code>null</code>.
113 * @see IVariableTextExpander#getText(String, String, ExpandVariableContext)
115 public String getText(String varTag, String varValue, ExpandVariableContext context) {
116 IPath path= getPath(varTag, varValue, context);
118 return path.toString();