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 result format.
22 * This class is not intended to be extended by clients.
25 public class ResourceExpander implements IVariableLocationExpander,
26 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. By default, return the
48 * selected resource of the context.
50 /* package */IResource expandUsingContext(ExpandVariableContext context) {
51 return context.getSelectedResource();
55 * Expands the variable value to a resource. The value will not be
56 * <code>null</code> nor empty. By default, lookup the member from the
59 /* package */IResource expandToMember(String varValue) {
60 return getWorkspaceRoot().findMember(varValue);
64 * (non-Javadoc) Method declared on IVariableLocationExpander.
66 public IPath getPath(String varTag, String varValue,
67 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 variable tag. Path
82 * variable tags represent variables that expand to paths relative to the
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 * (non-Javadoc) Method declared on IVariableResourceExpander.
94 public IResource[] getResources(String varTag, String varValue,
95 ExpandVariableContext context) {
96 IResource resource = expand(varValue, context);
97 if (resource != null) {
98 return new IResource[] { resource };
105 * Returns the workspace root resource.
107 protected final IWorkspaceRoot getWorkspaceRoot() {
108 return ResourcesPlugin.getWorkspace().getRoot();
112 * Returns a string representation of the path to a file or directory for
113 * the given variable tag and value or <code>null</code>.
115 * @see IVariableTextExpander#getText(String, String, ExpandVariableContext)
117 public String getText(String varTag, String varValue,
118 ExpandVariableContext context) {
119 IPath path = getPath(varTag, varValue, context);
121 return path.toString();