different syntax highlighting for single and double quoted strings
[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 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
21  * result format.
22  * <p>
23  * This class is not intended to be extended by clients.
24  * </p>
25  */
26 public class ResourceExpander implements IVariableLocationExpander, 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.
48          * By default, return the selected resource of the
49          * context.
50          */
51         /*package*/ IResource expandUsingContext(ExpandVariableContext context) {
52                 return context.getSelectedResource();
53         }
54         
55         /**
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.
59          */
60         /*package*/ IResource expandToMember(String varValue) {
61                 return getWorkspaceRoot().findMember(varValue);
62         }
63         
64         /* (non-Javadoc)
65          * Method declared on IVariableLocationExpander.
66          */
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();
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
82          * variable tag. Path variable tags represent variables that
83          * expand to paths relative to the 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         /* (non-Javadoc)
92          * Method declared on IVariableResourceExpander.
93          */
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};
98                 } else {
99                         return null;
100                 }
101         }
102         
103         /**
104          * Returns the workspace root resource.
105          */
106         protected final IWorkspaceRoot getWorkspaceRoot() {
107                 return ResourcesPlugin.getWorkspace().getRoot();
108         }
109         
110         /**
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>.
113          * 
114          * @see IVariableTextExpander#getText(String, String, ExpandVariableContext)
115          */
116         public String getText(String varTag, String varValue, ExpandVariableContext context) {
117                 IPath path= getPath(varTag, varValue, context);
118                 if (path != null) {
119                         return path.toString();
120                 }
121                 return null;
122         }
123
124 }