diff options
4 files changed, 103 insertions, 32 deletions
diff --git a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringSubstitutionEngine.java b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringSubstitutionEngine.java index 79ec024f0..03d772137 100644 --- a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringSubstitutionEngine.java +++ b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringSubstitutionEngine.java @@ -16,7 +16,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Stack; - import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -44,7 +43,7 @@ public class StringSubstitutionEngine { private StringBuffer fResult; /** - * whether substitutions were performed + * Whether substitutions were performed */ private boolean fSubs; @@ -83,11 +82,11 @@ public class StringSubstitutionEngine { * @exception CoreException if unable to resolve a referenced variable or if a cycle exists * in referenced variables */ - public String performStringSubstitution(String expression, boolean reportUndefinedVariables, IStringVariableManager manager) throws CoreException { - substitute(expression, reportUndefinedVariables, manager); + public String performStringSubstitution(String expression, boolean reportUndefinedVariables, boolean resolveVariables, IStringVariableManager manager) throws CoreException { + substitute(expression, reportUndefinedVariables, resolveVariables, manager); List resolvedVariableSets = new ArrayList(); while (fSubs) { - HashSet resolved = substitute(fResult.toString(), reportUndefinedVariables, manager); + HashSet resolved = substitute(fResult.toString(), reportUndefinedVariables, true, manager); for(int i=resolvedVariableSets.size()-1; i>=0; i--) { @@ -114,14 +113,27 @@ public class StringSubstitutionEngine { } /** + * Performs recursive string validation to ensure that all of the variables + * contained in the expression exist + * @param expression expression to validate + * @param manager registry of variables + * @exception CoreException if a referenced variable does not exist or if a cycle exists + * in referenced variables + */ + public void validateStringVariables(String expression, IStringVariableManager manager) throws CoreException { + performStringSubstitution(expression, true, false, manager); + } + + /** * Makes a substitution pass of the given expression returns a Set of the variables that were resolved in this * pass * * @param expression source expression * @param reportUndefinedVariables whether to report undefined variables as an error + * @param resolveVariables whether to resolve the value of any variables * @exception CoreException if unable to resolve a variable */ - private HashSet substitute(String expression, boolean reportUndefinedVariables, IStringVariableManager manager) throws CoreException { + private HashSet substitute(String expression, boolean reportUndefinedVariables, boolean resolveVariables, IStringVariableManager manager) throws CoreException { fResult = new StringBuffer(expression.length()); fStack = new Stack(); fSubs = false; @@ -177,7 +189,7 @@ public class StringSubstitutionEngine { resolvedVariables.add(substring); pos = end + 1; - String value = resolve(tos, reportUndefinedVariables, manager); + String value= resolve(tos, reportUndefinedVariables, resolveVariables, manager); if (value == null) { value = ""; //$NON-NLS-1$ } @@ -219,11 +231,12 @@ public class StringSubstitutionEngine { * @param var * @param reportUndefinedVariables whether to report undefined variables as * an error + * @param resolveVariables whether to resolve the variables value or just to validate that this variable is valid * @param manager variable registry * @return variable value, possibly <code>null</code> * @exception CoreException if unable to resolve a value */ - private String resolve(VariableReference var, boolean reportUndefinedVariables, IStringVariableManager manager) throws CoreException { + private String resolve(VariableReference var, boolean reportUndefinedVariables, boolean resolveVariables, IStringVariableManager manager) throws CoreException { String text = var.getText(); int pos = text.indexOf(VARIABLE_ARG); String name = null; @@ -246,23 +259,37 @@ public class StringSubstitutionEngine { throw new CoreException(new Status(IStatus.ERROR, VariablesPlugin.getUniqueIdentifier(), VariablesPlugin.INTERNAL_ERROR, MessageFormat.format(VariablesMessages.getString("StringSubstitutionEngine.3"), new String[]{name}), null)); //$NON-NLS-1$ } else { // leave as is - StringBuffer res = new StringBuffer(var.getText()); - res.insert(0, VARIABLE_START); - res.append(VARIABLE_END); - return res.toString(); + return getOriginalVarText(var); } } else { - fSubs = true; - return dynamicVariable.getValue(arg); + if (resolveVariables) { + fSubs = true; + return dynamicVariable.getValue(arg); + } else { + //leave as is + return getOriginalVarText(var); + } } } else { if (arg == null) { - fSubs = true; - return valueVariable.getValue(); + if (resolveVariables) { + fSubs = true; + return valueVariable.getValue(); + } else { + //leave as is + return getOriginalVarText(var); + } } else { // error - an argument specified for a value variable throw new CoreException(new Status(IStatus.ERROR, VariablesPlugin.getUniqueIdentifier(), VariablesPlugin.INTERNAL_ERROR, MessageFormat.format(VariablesMessages.getString("StringSubstitutionEngine.4"), new String[]{valueVariable.getName()}), null)); //$NON-NLS-1$ } } } + + private String getOriginalVarText(VariableReference var) { + StringBuffer res = new StringBuffer(var.getText()); + res.insert(0, VARIABLE_START); + res.append(VARIABLE_END); + return res.toString(); + } }
\ No newline at end of file diff --git a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariableManager.java b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariableManager.java index 05bc6945b..41e66ddd8 100644 --- a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariableManager.java +++ b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariableManager.java @@ -527,11 +527,18 @@ public class StringVariableManager implements IStringVariableManager { buffer.append("}"); //$NON-NLS-1$ return buffer.toString(); } + /* (non-Javadoc) * @see org.eclipse.debug.internal.core.stringsubstitution.IStringVariableManager#performStringSubstitution(java.lang.String, boolean) */ public String performStringSubstitution(String expression, boolean reportUndefinedVariables) throws CoreException { - return new StringSubstitutionEngine().performStringSubstitution(expression, reportUndefinedVariables, this); + return new StringSubstitutionEngine().performStringSubstitution(expression, reportUndefinedVariables, true, this); } + /* (non-Javadoc) + * @see org.eclipse.core.variables.IStringVariableManager#validateStringVariables(java.lang.String) + */ + public void validateStringVariables(String expression) throws CoreException { + new StringSubstitutionEngine().validateStringVariables(expression, this); + } } diff --git a/org.eclipse.core.variables/src/org/eclipse/core/variables/IStringVariableManager.java b/org.eclipse.core.variables/src/org/eclipse/core/variables/IStringVariableManager.java index 541b48899..fa0a6536e 100644 --- a/org.eclipse.core.variables/src/org/eclipse/core/variables/IStringVariableManager.java +++ b/org.eclipse.core.variables/src/org/eclipse/core/variables/IStringVariableManager.java @@ -99,6 +99,15 @@ public interface IStringVariableManager { public String performStringSubstitution(String expression, boolean reportUndefinedVariables) throws CoreException; /** + * Recursively determines the validity of all variables references in the given + * expression and reports errors for references to undefined variables. + * + * @param expression expression referencing variables + * @throws CoreException if one or more variables does not exist + */ + public void validateStringVariables(String expression) throws CoreException; + + /** * Returns a new value variable with the given name and description. * * @param name variable name, cannot be <code>null</code> diff --git a/org.eclipse.ui.externaltools/External Tools Base/org/eclipse/ui/externaltools/internal/launchConfigurations/ExternalToolsMainTab.java b/org.eclipse.ui.externaltools/External Tools Base/org/eclipse/ui/externaltools/internal/launchConfigurations/ExternalToolsMainTab.java index 9201fb51e..56ee0e3fa 100644 --- a/org.eclipse.ui.externaltools/External Tools Base/org/eclipse/ui/externaltools/internal/launchConfigurations/ExternalToolsMainTab.java +++ b/org.eclipse.ui.externaltools/External Tools Base/org/eclipse/ui/externaltools/internal/launchConfigurations/ExternalToolsMainTab.java @@ -393,8 +393,8 @@ public abstract class ExternalToolsMainTab extends AbstractLaunchConfigurationTa * Validates the content of the location field. */ protected boolean validateLocation(boolean newConfig) { - String value = locationField.getText().trim(); - if (value.length() < 1) { + String location = locationField.getText().trim(); + if (location.length() < 1) { if (newConfig) { setErrorMessage(null); setMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.30")); //$NON-NLS-1$ @@ -405,15 +405,18 @@ public abstract class ExternalToolsMainTab extends AbstractLaunchConfigurationTa return false; } - String location = null; + String expandedLocation= null; try { - location= getValue(value); + expandedLocation= resolveValue(location); + if (expandedLocation == null) { //a variable that needs to be resolved at runtime + return true; + } } catch (CoreException e) { - setErrorMessage(e.getMessage()); + setErrorMessage(e.getStatus().getMessage()); return false; } - File file = new File(location); + File file = new File(expandedLocation); if (!file.exists()) { // The file does not exist. if (!newConfig) { setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_location_does_not_exist_19")); //$NON-NLS-1$ @@ -430,11 +433,33 @@ public abstract class ExternalToolsMainTab extends AbstractLaunchConfigurationTa } /** - * Returns the value of the given string with all variables substituted (if any). + * Validates the variables of the given string to determine if all variables are valid * * @param expression expression with variables - * @return resolved value of expression - * @exception CoreException if variable substitution fails + * @exception CoreException if a variable is specified that does not exist + */ + private void validateVaribles(String expression) throws CoreException { + IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); + manager.validateStringVariables(expression); + } + + private String resolveValue(String expression) throws CoreException { + String expanded= null; + try { + expanded= getValue(expression); + } catch (CoreException e) { //possibly just a variable that needs to be resolved at runtime + validateVaribles(expression); + return null; + } + return expanded; + } + + /** + * Validates the value of the given string to determine if any/all variables are valid + * + * @param expression expression with variables + * @return whether the expression contained any variable values + * @exception CoreException if variable resolution fails */ private String getValue(String expression) throws CoreException { IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); @@ -445,20 +470,23 @@ public abstract class ExternalToolsMainTab extends AbstractLaunchConfigurationTa * Validates the content of the working directory field. */ protected boolean validateWorkDirectory() { - String value = workDirectoryField.getText().trim(); - if (value.length() <= 0) { + String dir = workDirectoryField.getText().trim(); + if (dir.length() <= 0) { return true; } - String dir = null; + String expandedDir= null; try { - dir= getValue(value); + expandedDir= resolveValue(dir); + if (expandedDir == null) { //a variable that needs to be resolved at runtime + return true; + } } catch (CoreException e) { - setErrorMessage(e.getMessage()); + setErrorMessage(e.getStatus().getMessage()); return false; } - File file = new File(dir); + File file = new File(expandedDir); if (!file.exists()) { // The directory does not exist. setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_working_directory_does_not_exist_or_is_invalid_21")); //$NON-NLS-1$ return false; |