1 11 package org.eclipse.ui.externaltools.internal.registry; 12 13 14 import java.util.ArrayList ; 15 import java.util.Map ; 16 import java.util.StringTokenizer ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.ILaunchConfiguration; 21 import org.eclipse.debug.core.ILaunchConfigurationType; 22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 23 import org.eclipse.debug.core.ILaunchManager; 24 import org.eclipse.debug.ui.IDebugUIConstants; 25 import org.eclipse.debug.ui.RefreshTab; 26 import org.eclipse.ui.externaltools.internal.model.ExternalToolsPlugin; 27 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants; 28 import org.eclipse.ui.externaltools.internal.ui.ExternalToolsUIMessages; 29 30 34 public final class ExternalToolMigration { 35 39 public static final class VariableDefinition { 40 45 public int start = -1; 46 47 52 public int end = -1; 53 54 58 public String name = null; 59 60 65 public String argument = null; 66 67 70 private VariableDefinition() { 71 super(); 72 } 73 } 74 75 78 private static final String VAR_TAG_START = "${"; private static final String VAR_TAG_END = "}"; private static final String VAR_TAG_SEP = ":"; 82 85 public static final String TOOL_TYPE_ANT_BUILD = "antBuildType"; 90 public static final String ID_ANT_BUILDER_LAUNCH_CONFIGURATION_TYPE = "org.eclipse.ant.AntBuilderLaunchConfigurationType"; 92 public static final String RUN_TARGETS_ATTRIBUTE = TOOL_TYPE_ANT_BUILD + ".runTargets"; 94 102 public static final String ATTR_ANT_TARGETS = IExternalToolConstants.PLUGIN_ID + ".ATTR_ANT_TARGETS"; 104 107 public static final String TAG_TOOL_TYPE = "!{tool_type}"; public static final String TAG_TOOL_NAME = "!{tool_name}"; public static final String TAG_TOOL_LOCATION = "!{tool_loc}"; public static final String TAG_TOOL_ARGUMENTS = "!{tool_args}"; public static final String TAG_TOOL_DIRECTORY = "!{tool_dir}"; public static final String TAG_TOOL_REFRESH = "!{tool_refresh}"; public static final String TAG_TOOL_SHOW_LOG = "!{tool_show_log}"; public static final String TAG_TOOL_BUILD_TYPES = "!{tool_build_types}"; public static final String TAG_TOOL_BLOCK = "!{tool_block}"; 117 private static final String TOOL_TYPE_ANT = "org.eclipse.ui.externaltools.type.ant"; private static final String TOOL_TYPE_PROGRAM = "org.eclipse.ui.externaltools.type.program"; 121 124 public static final String TAG_TYPE = "type"; public static final String TAG_NAME = "name"; public static final String TAG_LOCATION = "location"; public static final String TAG_WORK_DIR = "workDirectory"; public static final String TAG_CAPTURE_OUTPUT = "captureOutput"; public static final String TAG_SHOW_CONSOLE = "showConsole"; public static final String TAG_RUN_BKGRND = "runInBackground"; public static final String TAG_PROMPT_ARGS = "promptForArguments"; public static final String TAG_ARGS = "arguments"; public static final String TAG_REFRESH_SCOPE = "refreshScope"; public static final String TAG_REFRESH_RECURSIVE = "refreshRecursive"; public static final String TAG_RUN_BUILD_KINDS = "runForBuildKinds"; public static final String TAG_EXTRA_ATTR = "extraAttribute"; public static final String TAG_VERSION = "version"; 139 private static final String EXTRA_ATTR_SEPARATOR = "="; 141 private static final String VERSION_21 = "2.1"; 143 private static final String TRUE = "true"; private static final String FALSE = "false"; 146 149 private ExternalToolMigration() { 150 super(); 151 } 152 153 159 public static ILaunchConfigurationWorkingCopy configFromArgumentMap(Map args) { 160 String version = (String ) args.get(TAG_VERSION); 161 if (VERSION_21.equals(version)) { 162 return configFrom21ArgumentMap(args); 163 } 164 return configFrom20ArgumentMap(args); 165 } 166 167 public static ILaunchConfigurationWorkingCopy configFrom21ArgumentMap(Map commandArgs) { 168 String name = (String ) commandArgs.get(TAG_NAME); 169 String type = (String ) commandArgs.get(TAG_TYPE); 170 171 ILaunchConfigurationWorkingCopy config = newConfig(type, name); 172 if (config == null) { 173 return null; 174 } 175 176 config.setAttribute(IExternalToolConstants.ATTR_LOCATION, (String ) commandArgs.get(TAG_LOCATION)); 177 config.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String ) commandArgs.get(TAG_WORK_DIR)); 178 config.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, TRUE.equals(commandArgs.get(TAG_CAPTURE_OUTPUT))); 179 config.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, TRUE.equals(commandArgs.get(TAG_SHOW_CONSOLE))); 180 config.setAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, TRUE.equals(commandArgs.get(TAG_RUN_BKGRND))); 181 config.setAttribute(IExternalToolConstants.ATTR_PROMPT_FOR_ARGUMENTS, TRUE.equals(commandArgs.get(TAG_PROMPT_ARGS))); 182 config.setAttribute(RefreshTab.ATTR_REFRESH_SCOPE, (String ) commandArgs.get(TAG_REFRESH_SCOPE)); 183 config.setAttribute(RefreshTab.ATTR_REFRESH_RECURSIVE, TRUE.equals(commandArgs.get(TAG_REFRESH_RECURSIVE))); 184 185 config.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, (String ) commandArgs.get(TAG_RUN_BUILD_KINDS)); 186 187 String args = (String ) commandArgs.get(TAG_ARGS); 188 if (args != null) { 189 config.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, args); 190 } 191 192 String extraAttributes = (String ) commandArgs.get(TAG_EXTRA_ATTR); 193 if (extraAttributes != null) { 194 StringTokenizer tokenizer = new StringTokenizer (extraAttributes, EXTRA_ATTR_SEPARATOR); 195 while (tokenizer.hasMoreTokens()) { 196 String key = tokenizer.nextToken(); 197 if (!tokenizer.hasMoreTokens()) 198 break; 199 String value = tokenizer.nextToken(); 200 if (key.equals(RUN_TARGETS_ATTRIBUTE)) { 201 config.setAttribute(ATTR_ANT_TARGETS, value); 203 } 204 } 205 } 206 return config; 207 } 208 209 212 public static ILaunchConfigurationWorkingCopy configFrom20ArgumentMap(Map args) { 213 String type = (String ) args.get(TAG_TOOL_TYPE); 215 if (TOOL_TYPE_ANT.equals(type)) { 216 type = TOOL_TYPE_ANT_BUILD; 217 } else if (TOOL_TYPE_PROGRAM.equals(type)){ 218 type = IExternalToolConstants.TOOL_TYPE_PROGRAM; 219 } else { 220 return null; 221 } 222 223 String name = (String ) args.get(TAG_TOOL_NAME); 224 225 ILaunchConfigurationWorkingCopy config = newConfig(type, name); 226 if (config == null) { 227 return null; 228 } 229 230 String location = (String ) args.get(TAG_TOOL_LOCATION); 232 config.setAttribute(IExternalToolConstants.ATTR_LOCATION, location); 233 234 String refresh = (String ) args.get(TAG_TOOL_REFRESH); 236 if (refresh != null) { 237 VariableDefinition varDef = extractVariableDefinition(refresh, 0); 238 if ("none".equals(varDef.name)) { refresh = null; 240 } 241 config.setAttribute(RefreshTab.ATTR_REFRESH_SCOPE, refresh); 242 } 243 244 String arguments = (String ) args.get(TAG_TOOL_ARGUMENTS); 246 if (type.equals(TOOL_TYPE_ANT_BUILD)) { 247 String targetNames = null; 248 if (arguments != null) { 249 int start = 0; 250 ArrayList targets = new ArrayList (); 251 StringBuffer buffer = new StringBuffer (); 252 VariableDefinition varDef = extractVariableDefinition(arguments, start); 253 while (varDef.end != -1) { 254 if ("ant_target".equals(varDef.name) && varDef.argument != null) { targets.add(varDef.argument); 256 buffer.append(arguments.substring(start, varDef.start)); 257 } else { 258 buffer.append(arguments.substring(start, varDef.end)); 259 } 260 start = varDef.end; 261 varDef = extractVariableDefinition(arguments, start); 262 } 263 buffer.append(arguments.substring(start, arguments.length())); 264 arguments = buffer.toString(); 265 266 buffer.setLength(0); 267 for (int i = 0; i < targets.size(); i++) { 268 String target = (String ) targets.get(i); 269 if (target != null && target.length() > 0) { 270 buffer.append(target); 271 buffer.append(","); } 273 } 274 targetNames = buffer.toString(); 275 } 276 if (targetNames != null && targetNames.length() > 0) { 277 config.setAttribute(ATTR_ANT_TARGETS, targetNames); 278 } 279 } 280 config.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments); 281 282 config.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, TRUE.equals(args.get(TAG_TOOL_SHOW_LOG))); 284 config.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, TRUE.equals(args.get(TAG_TOOL_SHOW_LOG))); 285 config.setAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, FALSE.equals(args.get(TAG_TOOL_BLOCK))); 286 String buildKinds= (String ) args.get(TAG_TOOL_BUILD_TYPES); 287 if (buildKinds != null) { 288 buildKinds= buildKinds.replace(';', ','); } 290 config.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, buildKinds); 291 config.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String ) args.get(TAG_TOOL_DIRECTORY)); 292 return config; 293 } 294 295 299 private static ILaunchConfigurationWorkingCopy newConfig(String type, String name) { 300 if (type == null || name == null) { 301 return null; 302 } 303 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); 304 ILaunchConfigurationType configType; 305 if (TOOL_TYPE_ANT_BUILD.equals(type)) { 306 configType = manager.getLaunchConfigurationType(ID_ANT_BUILDER_LAUNCH_CONFIGURATION_TYPE); 307 } else if (IExternalToolConstants.TOOL_TYPE_PROGRAM.equals(type)) { 308 configType = manager.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_BUILDER_LAUNCH_CONFIGURATION_TYPE); 309 } else { 310 return null; 311 } 312 try { 313 if (configType != null) { 314 return configType.newInstance(null, name); 315 } 316 } catch (CoreException e) { 317 ExternalToolsPlugin.getDefault().log(e); 318 } 319 return null; 320 } 321 322 326 public static String getNameFromCommandArgs(Map commandArgs) { 327 String name= (String ) commandArgs.get(TAG_NAME); 328 if (name == null) { 329 name= (String ) commandArgs.get(TAG_TOOL_NAME); 330 } 331 return name; 332 } 333 334 341 public static ILaunchConfiguration migrateRunInBackground(ILaunchConfiguration config) { 342 String noValueFlag= "NoValue"; String attr= null; 344 try { 345 attr = config.getAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, noValueFlag); 346 } catch (CoreException e) { 347 return config; 350 } 351 if (noValueFlag.equals(attr)) { 352 String ATTR_RUN_IN_BACKGROUND= IExternalToolConstants.PLUGIN_ID + ".ATTR_RUN_IN_BACKGROUND"; boolean runInBackground= false; 355 try { 356 runInBackground = config.getAttribute(ATTR_RUN_IN_BACKGROUND, runInBackground); 357 } catch (CoreException e) { 358 ExternalToolsPlugin.getDefault().log(ExternalToolsUIMessages.ExternalToolMigration_37, e); 359 } 360 try { 361 ILaunchConfigurationWorkingCopy workingCopy= config.getWorkingCopy(); 362 workingCopy.setAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, runInBackground); 363 config= workingCopy.doSave(); 364 } catch (CoreException e) { 365 ExternalToolsPlugin.getDefault().log(ExternalToolsUIMessages.ExternalToolMigration_38, e); 366 } 367 } 368 return config; 369 } 370 371 378 public static VariableDefinition extractVariableDefinition(String text, int start) { 379 VariableDefinition varDef = new VariableDefinition(); 380 381 varDef.start = text.indexOf(VAR_TAG_START, start); 382 if (varDef.start < 0){ 383 return varDef; 384 } 385 start = varDef.start + VAR_TAG_START.length(); 386 387 int end = text.indexOf(VAR_TAG_END, start); 388 if (end < 0) { 389 return varDef; 390 } 391 varDef.end = end + VAR_TAG_END.length(); 392 if (end == start) { 393 return varDef; 394 } 395 396 int mid = text.indexOf(VAR_TAG_SEP, start); 397 if (mid < 0 || mid > end) { 398 varDef.name = text.substring(start, end); 399 } else { 400 if (mid > start) { 401 varDef.name = text.substring(start, mid); 402 } 403 mid = mid + VAR_TAG_SEP.length(); 404 if (mid < end) { 405 varDef.argument = text.substring(mid, end); 406 } 407 } 408 409 return varDef; 410 } 411 } 412 | Popular Tags |