KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > stringsubstitution > ResourceResolver


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.stringsubstitution;
12
13 import java.net.URI JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IWorkspaceRoot;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.variables.IDynamicVariable;
24 import org.eclipse.core.variables.IDynamicVariableResolver;
25 import org.eclipse.debug.internal.ui.DebugUIPlugin;
26 import org.eclipse.debug.ui.IDebugUIConstants;
27
28 import com.ibm.icu.text.MessageFormat;
29
30 /**
31  * Common function of variable resolvers.
32  *
33  * @since 3.0
34  */

35 public class ResourceResolver implements IDynamicVariableResolver {
36
37     /* (non-Javadoc)
38      * @see org.eclipse.debug.internal.core.stringsubstitution.IContextVariableResolver#resolveValue(org.eclipse.debug.internal.core.stringsubstitution.IContextVariable, java.lang.String)
39      */

40     public String JavaDoc resolveValue(IDynamicVariable variable, String JavaDoc argument) throws CoreException {
41         IResource resource = null;
42         if (argument == null) {
43             resource = getSelectedResource(variable);
44         } else {
45             resource = getWorkspaceRoot().findMember(new Path(argument));
46         }
47         if (resource != null && resource.exists()) {
48             resource = translateSelectedResource(resource);
49             if (resource != null && resource.exists()) {
50                 return translateToValue(resource, variable);
51             }
52         }
53         abort(MessageFormat.format(StringSubstitutionMessages.ResourceResolver_6, new String JavaDoc[]{getReferenceExpression(variable, argument)}), null);
54         return null;
55     }
56     
57     /**
58      * Returns the resource applicable to this resolver, relative to the selected
59      * resource. This method is called when no argument is present in a variable
60      * expression. For, example, this method might return the project for the
61      * selected resource.
62      *
63      * @param resource selected resource
64      * @return resource applicable to this variable resolver
65      */

66     protected IResource translateSelectedResource(IResource resource) {
67         return resource;
68     }
69     
70     /**
71      * Returns the workspace root
72      *
73      * @return workspace root
74      */

75     protected IWorkspaceRoot getWorkspaceRoot() {
76         return ResourcesPlugin.getWorkspace().getRoot();
77     }
78
79     /**
80      * Returns an expression used to reference the given variable and optional argument.
81      * For example, <code>${var_name:arg}</code>.
82      *
83      * @param variable referenced variable
84      * @param argument referenced argument or <code>null</code>
85      * @return vraiable reference expression
86      */

87     protected String JavaDoc getReferenceExpression(IDynamicVariable variable, String JavaDoc argument) {
88         StringBuffer JavaDoc reference = new StringBuffer JavaDoc();
89         reference.append("${"); //$NON-NLS-1$
90
reference.append(variable.getName());
91         if (argument != null) {
92             reference.append(":"); //$NON-NLS-1$
93
reference.append(argument);
94         }
95         reference.append("}"); //$NON-NLS-1$
96
return reference.toString();
97     }
98     
99     /**
100      * Throws an exception with the given message and underlying exception.
101      *
102      * @param message exception message
103      * @param exception underlying exception or <code>null</code>
104      * @throws CoreException
105      */

106     protected void abort(String JavaDoc message, Throwable JavaDoc exception) throws CoreException {
107         throw new CoreException(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.INTERNAL_ERROR, message, exception));
108     }
109     
110     /**
111      * Returns the selected resource.
112      *
113      * @param variable variable referencing a resource
114      * @return selected resource
115      * @throws CoreException if there is no selection
116      */

117     protected IResource getSelectedResource(IDynamicVariable variable) throws CoreException {
118         IResource resource = SelectedResourceManager.getDefault().getSelectedResource();
119         if (resource == null) {
120             abort(MessageFormat.format(StringSubstitutionMessages.ResourceResolver_7, new String JavaDoc[]{getReferenceExpression(variable, null)}), null);
121         }
122         return resource;
123     }
124
125     /**
126      * Translates the given resource into a value for this variable resolver.
127      *
128      * @param resource the resource applicable to this resolver's variable
129      * @param variable the variable being resolved
130      * @return variable value
131      * @throws CoreException if the variable name is not recognized
132      */

133     protected String JavaDoc translateToValue(IResource resource, IDynamicVariable variable) throws CoreException {
134         String JavaDoc name = variable.getName();
135         IPath path = null;
136         URI JavaDoc uri = null;
137         if (name.endsWith("_loc")) { //$NON-NLS-1$
138
uri = resource.getLocationURI();
139             if(uri != null) {
140                 path = new Path(uri.getPath());
141                 if(path != null) {
142                     return path.toOSString();
143                 }
144             }
145         } else if (name.endsWith("_path")) { //$NON-NLS-1$
146
path = resource.getFullPath();
147             if(path != null) {
148                 return path.toOSString();
149             }
150         } else if (name.endsWith("_name")) { //$NON-NLS-1$
151
return resource.getName();
152         }
153         abort(MessageFormat.format(StringSubstitutionMessages.ResourceResolver_8, new String JavaDoc[]{getReferenceExpression(variable, null)}), null);
154         return null;
155     }
156
157 }
158
Popular Tags