211027d4d5108b52751c14180d442fa99cc5a5bf
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / externaltools / variable / ResourceExpander.java
1 package net.sourceforge.phpdt.externaltools.variable;
2
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
8  
9 Contributors:
10 **********************************************************************/
11
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;
17
18 /**
19  * Expands a resource type variable into the desired
20  * result format.
21  * <p>
22  * This class is not intended to be extended by clients.
23  * </p>
24  */
25 public class ResourceExpander implements IVariableLocationExpander, IVariableResourceExpander, IVariableTextExpander {
26
27         /**
28          * Create an instance
29          */
30         public ResourceExpander() {
31                 super();
32         }
33
34         /**
35          * Expands the variable to a resource.
36          */
37         /*package*/ IResource expand(String varValue, ExpandVariableContext context) {
38                 if (varValue != null && varValue.length() > 0) {
39                         return expandToMember(varValue);
40                 } else {
41                         return expandUsingContext(context);
42                 }
43         }
44         
45         /**
46          * Expands using the current context information.
47          * By default, return the selected resource of the
48          * context.
49          */
50         /*package*/ IResource expandUsingContext(ExpandVariableContext context) {
51                 return context.getSelectedResource();
52         }
53         
54         /**
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.
58          */
59         /*package*/ IResource expandToMember(String varValue) {
60                 return getWorkspaceRoot().findMember(varValue);
61         }
62         
63         /* (non-Javadoc)
64          * Method declared on IVariableLocationExpander.
65          */
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();
71                         } else {
72                                 return resource.getLocation();
73                         }
74                 } else {
75                         return null;
76                 }
77         }
78         
79         /**
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.
83          */
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);
88         }
89
90         /* (non-Javadoc)
91          * Method declared on IVariableResourceExpander.
92          */
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};
97                 } else {
98                         return null;
99                 }
100         }
101         
102         /**
103          * Returns the workspace root resource.
104          */
105         protected final IWorkspaceRoot getWorkspaceRoot() {
106                 return ResourcesPlugin.getWorkspace().getRoot();
107         }
108         
109         /**
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>.
112          * 
113          * @see IVariableTextExpander#getText(String, String, ExpandVariableContext)
114          */
115         public String getText(String varTag, String varValue, ExpandVariableContext context) {
116                 IPath path= getPath(varTag, varValue, context);
117                 if (path != null) {
118                         return path.toString();
119                 }
120                 return null;
121         }
122
123 }