package org.eclipse.core.internal.variables;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.variables.IDynamicVariable;
import org.eclipse.core.variables.IDynamicVariableResolver;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.osgi.util.NLS;
public class DynamicVariable extends StringVariable implements IDynamicVariable {
private IDynamicVariableResolver fResolver;
@Override
public String getValue(String argument) throws CoreException {
if (!supportsArgument()) {
if (argument != null && argument.length() > 0) {
throw new CoreException(new Status(IStatus.ERROR, VariablesPlugin.getUniqueIdentifier(), VariablesPlugin.INTERNAL_ERROR, NLS.bind(VariablesMessages.DynamicVariable_0, new String[]{argument, getName()}), null));
}
}
if (fResolver == null) {
String name = getConfigurationElement().getAttribute("resolver");
if (name == null) {
throw new CoreException(new Status(IStatus.ERROR, VariablesPlugin.getUniqueIdentifier(), VariablesPlugin.INTERNAL_ERROR, NLS.bind("Contributed context variable {0} must specify a resolver.",new String[]{getName()}), null));
}
Object object = getConfigurationElement().createExecutableExtension("resolver");
if (object instanceof IDynamicVariableResolver) {
fResolver = (IDynamicVariableResolver)object;
} else {
throw new CoreException(new Status(IStatus.ERROR, VariablesPlugin.getUniqueIdentifier(), VariablesPlugin.INTERNAL_ERROR, NLS.bind("Contributed context variable resolver for {0} must be an instance of IContextVariableResolver.",new String[]{getName()}), null));
}
}
try {
return fResolver.resolveValue(this, argument);
} catch (RuntimeException e) {
throw new CoreException(new Status(IStatus.ERROR, VariablesPlugin.getUniqueIdentifier(), VariablesPlugin.INTERNAL_ERROR, NLS.bind("Error while evaluating variable {0}.",new String[]{getName()}), e));
}
}
public DynamicVariable(String name, String description, IConfigurationElement configurationElement) {
super(name, description, configurationElement);
}
@Override
public boolean supportsArgument() {
String arg = getConfigurationElement().getAttribute("supportsArgument");
return arg == null || Boolean.valueOf(arg).booleanValue();
}
}