1 11 package org.eclipse.debug.internal.ui.stringsubstitution; 12 13 import java.net.URI ; 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 35 public class ResourceResolver implements IDynamicVariableResolver { 36 37 40 public String resolveValue(IDynamicVariable variable, String 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 []{getReferenceExpression(variable, argument)}), null); 54 return null; 55 } 56 57 66 protected IResource translateSelectedResource(IResource resource) { 67 return resource; 68 } 69 70 75 protected IWorkspaceRoot getWorkspaceRoot() { 76 return ResourcesPlugin.getWorkspace().getRoot(); 77 } 78 79 87 protected String getReferenceExpression(IDynamicVariable variable, String argument) { 88 StringBuffer reference = new StringBuffer (); 89 reference.append("${"); reference.append(variable.getName()); 91 if (argument != null) { 92 reference.append(":"); reference.append(argument); 94 } 95 reference.append("}"); return reference.toString(); 97 } 98 99 106 protected void abort(String message, Throwable exception) throws CoreException { 107 throw new CoreException(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.INTERNAL_ERROR, message, exception)); 108 } 109 110 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 []{getReferenceExpression(variable, null)}), null); 121 } 122 return resource; 123 } 124 125 133 protected String translateToValue(IResource resource, IDynamicVariable variable) throws CoreException { 134 String name = variable.getName(); 135 IPath path = null; 136 URI uri = null; 137 if (name.endsWith("_loc")) { 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")) { path = resource.getFullPath(); 147 if(path != null) { 148 return path.toOSString(); 149 } 150 } else if (name.endsWith("_name")) { return resource.getName(); 152 } 153 abort(MessageFormat.format(StringSubstitutionMessages.ResourceResolver_8, new String []{getReferenceExpression(variable, null)}), null); 154 return null; 155 } 156 157 } 158 | Popular Tags |