1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / 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 net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
13
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;
18
19 /**
20  * Expands a resource type variable into the desired result format.
21  * <p>
22  * This class is not intended to be extended by clients.
23  * </p>
24  */
25 public class ResourceExpander implements IVariableLocationExpander,
26                 IVariableResourceExpander, IVariableTextExpander {
27
28         /**
29          * Create an instance
30          */
31         public ResourceExpander() {
32                 super();
33         }
34
35         /**
36          * Expands the variable to a resource.
37          */
38         /* package */IResource expand(String varValue, ExpandVariableContext context) {
39                 if (varValue != null && varValue.length() > 0) {
40                         return expandToMember(varValue);
41                 } else {
42                         return expandUsingContext(context);
43                 }
44         }
45
46         /**
47          * Expands using the current context information. By default, return the
48          * selected resource of the 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 will not be
56          * <code>null</code> nor empty. By default, lookup the member from the
57          * workspace root.
58          */
59         /* package */IResource expandToMember(String varValue) {
60                 return getWorkspaceRoot().findMember(varValue);
61         }
62
63         /*
64          * (non-Javadoc) Method declared on IVariableLocationExpander.
65          */
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();
72                         } else {
73                                 return resource.getLocation();
74                         }
75                 } else {
76                         return null;
77                 }
78         }
79
80         /**
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
83          * workspace root.
84          */
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);
89         }
90
91         /*
92          * (non-Javadoc) Method declared on IVariableResourceExpander.
93          */
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 };
99                 } else {
100                         return null;
101                 }
102         }
103
104         /**
105          * Returns the workspace root resource.
106          */
107         protected final IWorkspaceRoot getWorkspaceRoot() {
108                 return ResourcesPlugin.getWorkspace().getRoot();
109         }
110
111         /**
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>.
114          * 
115          * @see IVariableTextExpander#getText(String, String, ExpandVariableContext)
116          */
117         public String getText(String varTag, String varValue,
118                         ExpandVariableContext context) {
119                 IPath path = getPath(varTag, varValue, context);
120                 if (path != null) {
121                         return path.toString();
122                 }
123                 return null;
124         }
125
126 }