1 package net.sourceforge.phpdt.externaltools.internal.registry;
3 /**********************************************************************
4 Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
10 **********************************************************************/
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.util.ArrayList;
18 import java.util.HashMap;
20 import java.util.StringTokenizer;
22 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
23 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
24 import net.sourceforge.phpeclipse.externaltools.ExternalToolsPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.debug.core.DebugPlugin;
29 import org.eclipse.debug.core.ILaunchConfigurationType;
30 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
31 import org.eclipse.debug.core.ILaunchManager;
32 import org.eclipse.ui.IMemento;
33 import org.eclipse.ui.WorkbenchException;
34 import org.eclipse.ui.XMLMemento;
37 * Responsible reading an old external tool format and creating and migrating it
38 * to create a new external tool.
40 public final class ExternalToolMigration {
41 //private static final String SEPERATOR = ";"; //$NON-NLS-1$
43 private static final String STATE_FILE_NAME_OLD = "oldexternaltools.xml"; //$NON-NLS-1$
45 private static final String STATE_FILE_NAME = "externaltools.xml"; //$NON-NLS-1$
47 //private static final String TAG_EXTERNALTOOLS = "externaltools"; //$NON-NLS-1$
49 private static final String TAG_TOOL = "tool"; //$NON-NLS-1$
51 private static final String TAG_ENTRY = "entry"; //$NON-NLS-1$
53 // private static final String TAG_KEY = "key"; //$NON-NLS-1$
54 // private static final String TAG_VALUE = "value"; //$NON-NLS-1$
59 // public static final String RUN_TARGETS_ATTRIBUTE =
60 // IExternalToolConstants.TOOL_TYPE_ANT_BUILD + ".runTargets";
63 * 2.0 External Tool Tags
65 private static final String TAG_TOOL_TYPE = "!{tool_type}"; //$NON-NLS-1$
67 private static final String TAG_TOOL_NAME = "!{tool_name}"; //$NON-NLS-1$
69 private static final String TAG_TOOL_LOCATION = "!{tool_loc}"; //$NON-NLS-1$
71 private static final String TAG_TOOL_ARGUMENTS = "!{tool_args}"; //$NON-NLS-1$
73 private static final String TAG_TOOL_DIRECTORY = "!{tool_dir}"; //$NON-NLS-1$
75 private static final String TAG_TOOL_REFRESH = "!{tool_refresh}"; //$NON-NLS-1$
77 private static final String TAG_TOOL_SHOW_LOG = "!{tool_show_log}"; //$NON-NLS-1$
79 private static final String TAG_TOOL_BUILD_TYPES = "!{tool_build_types}"; //$NON-NLS-1$
81 private static final String TAG_TOOL_BLOCK = "!{tool_block}"; //$NON-NLS-1$
83 // Known kind of tools
84 //private static final String TOOL_TYPE_PROGRAM = "net.sourceforge.phpdt.externaltools.type.program"; //$NON-NLS-1$
86 // private static final String TOOL_TYPE_ANT =
87 // "org.eclipse.ui.externaltools.type.ant"; //$NON-NLS-1$
90 * 2.1 External Tool Keys
92 //private static final String TAG_EXTERNAL_TOOL = "externalTool"; //$NON-NLS-1$
94 private static final String TAG_TYPE = "type"; //$NON-NLS-1$
96 private static final String TAG_NAME = "name"; //$NON-NLS-1$
98 private static final String TAG_LOCATION = "location"; //$NON-NLS-1$
100 private static final String TAG_WORK_DIR = "workDirectory"; //$NON-NLS-1$
102 private static final String TAG_CAPTURE_OUTPUT = "captureOutput"; //$NON-NLS-1$
104 private static final String TAG_SHOW_CONSOLE = "showConsole"; //$NON-NLS-1$
106 private static final String TAG_RUN_BKGRND = "runInBackground"; //$NON-NLS-1$
108 //private static final String TAG_OPEN_PERSP = "openPerspective"; //$NON-NLS-1$
110 private static final String TAG_PROMPT_ARGS = "promptForArguments"; //$NON-NLS-1$
112 //private static final String TAG_SHOW_MENU = "showInMenu"; //$NON-NLS-1$
114 //private static final String TAG_SAVE_DIRTY = "saveDirtyEditors"; //$NON-NLS-1$
116 private static final String TAG_ARGS = "arguments"; //$NON-NLS-1$
118 private static final String TAG_REFRESH_SCOPE = "refreshScope"; //$NON-NLS-1$
120 private static final String TAG_REFRESH_RECURSIVE = "refreshRecursive"; //$NON-NLS-1$
122 private static final String TAG_RUN_BUILD_KINDS = "runForBuildKinds"; //$NON-NLS-1$
124 private static final String TAG_EXTRA_ATTR = "extraAttribute"; //$NON-NLS-1$
126 private static final String TAG_KEY = "key"; //$NON-NLS-1$
128 private static final String TAG_VERSION = "version"; //$NON-NLS-1$
130 //private static final String BUILD_TYPE_SEPARATOR = ","; //$NON-NLS-1$
132 private static final String EXTRA_ATTR_SEPARATOR = "="; //$NON-NLS-1$
134 private static final String VERSION_21 = "2.1"; //$NON-NLS-1$;
136 private static final String TRUE = "true"; //$NON-NLS-1$
138 private static final String FALSE = "false"; //$NON-NLS-1$
141 * Allows no instances.
143 private ExternalToolMigration() {
148 * Loads the external tools from storage and adds them to the registry.
151 // This method is not called. It is left here in case
152 // we decide to do tool migration in the future
153 // private static void readInOldTools() {
158 // private static void readIn21Tools() {
161 public static void readIn20Tools() {
162 boolean migrationSuccessful = true;
163 IPath path = ExternalToolsPlugin.getDefault().getStateLocation();
164 File file = path.append(STATE_FILE_NAME).toFile();
168 InputStreamReader reader = null;
170 FileInputStream input = new FileInputStream(file);
171 reader = new InputStreamReader(input, "utf-8"); //$NON-NLS-1$
172 XMLMemento memento = XMLMemento.createReadRoot(reader);
174 // Get the external tool children element
175 IMemento[] tools = memento.getChildren(TAG_TOOL);
176 for (int i = 0; i < tools.length; i++) {
177 HashMap args = new HashMap();
178 IMemento[] entries = tools[i].getChildren(TAG_ENTRY);
179 for (int j = 0; j < entries.length; j++) {
180 String key = entries[j].getString(TAG_KEY);
182 String value = entries[j].getTextData();
183 args.put(key, value);
186 ILaunchConfigurationWorkingCopy config = configFromArgumentMap(args);
187 if (config != null) {
190 } catch (CoreException e) {
191 // TODO: Decide what to do when saving fails.
195 } catch (FileNotFoundException e) {
196 // Silently ignore this...
197 } catch (IOException e) {
198 ExternalToolsPlugin.getDefault().log(
199 "File I/O error with reading old external tools.", e);
200 migrationSuccessful = false;
201 } catch (WorkbenchException e) {
202 ExternalToolsPlugin.getDefault().getLog().log(e.getStatus());
204 .println("Error reading old external tools. See .log file for more details");
205 migrationSuccessful = false;
207 if (reader != null) {
210 } catch (IOException e) {
211 ExternalToolsPlugin.getDefault().log(
212 "Unable to close external tool old state reader.",
218 if (migrationSuccessful) {
219 if (!file.renameTo(path.append(STATE_FILE_NAME_OLD).toFile())) {
223 "Unable to rename old external tool state file. Please rename externaltools.xml to oldexternaltools.xml manually.",
226 .println("Unable to rename old external tool state file. Please rename externaltools.xml to oldexternaltools.xml manually.");
232 * Returns a launch configuration working copy from the argument map or
233 * <code>null</code> if the given map cannot be interpreted as a 2.0 or
234 * 2.1 branch external tool. The returned working copy will be unsaved and
235 * its location will be set to the metadata area.
237 public static ILaunchConfigurationWorkingCopy configFromArgumentMap(Map args) {
238 String version = (String) args.get(TAG_VERSION);
239 if (VERSION_21.equals(version)) {
240 return configFrom21ArgumentMap(args);
242 return configFrom20ArgumentMap(args);
245 public static ILaunchConfigurationWorkingCopy configFrom21ArgumentMap(
247 String name = (String) commandArgs.get(TAG_NAME);
248 String type = (String) commandArgs.get(TAG_TYPE);
250 ILaunchConfigurationWorkingCopy config = newConfig(type, name);
251 if (config == null) {
255 config.setAttribute(IExternalToolConstants.ATTR_LOCATION,
256 (String) commandArgs.get(TAG_LOCATION));
257 config.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY,
258 (String) commandArgs.get(TAG_WORK_DIR));
259 config.setAttribute(IExternalToolConstants.ATTR_CAPTURE_OUTPUT, TRUE
260 .equals((String) commandArgs.get(TAG_CAPTURE_OUTPUT)));
261 config.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, TRUE
262 .equals((String) commandArgs.get(TAG_SHOW_CONSOLE)));
263 config.setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, TRUE
264 .equals((String) commandArgs.get(TAG_RUN_BKGRND)));
265 config.setAttribute(IExternalToolConstants.ATTR_PROMPT_FOR_ARGUMENTS,
266 TRUE.equals((String) commandArgs.get(TAG_PROMPT_ARGS)));
267 config.setAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE,
268 (String) commandArgs.get(TAG_REFRESH_SCOPE));
269 config.setAttribute(IExternalToolConstants.ATTR_REFRESH_RECURSIVE, TRUE
270 .equals((String) commandArgs.get(TAG_REFRESH_RECURSIVE)));
272 config.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS,
273 (String) commandArgs.get(TAG_RUN_BUILD_KINDS));
275 String args = (String) commandArgs.get(TAG_ARGS);
277 config.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS,
281 String extraAttributes = (String) commandArgs.get(TAG_EXTRA_ATTR);
282 if (extraAttributes != null) {
283 StringTokenizer tokenizer = new StringTokenizer(extraAttributes,
284 EXTRA_ATTR_SEPARATOR);
285 while (tokenizer.hasMoreTokens()) {
286 String key = tokenizer.nextToken();
287 if (!tokenizer.hasMoreTokens())
289 String value = tokenizer.nextToken();
290 // if (key.equals(RUN_TARGETS_ATTRIBUTE)) {
291 // // 2.1 implementation only defined 1 "extra attribute"
292 // config.setAttribute(IExternalToolConstants.ATTR_ANT_TARGETS,
301 * Creates an external tool from the map.
303 public static ILaunchConfigurationWorkingCopy configFrom20ArgumentMap(
305 // Update the type...
306 String type = (String) args.get(TAG_TOOL_TYPE);
307 // if (TOOL_TYPE_ANT.equals(type))
308 // type = IExternalToolConstants.TOOL_TYPE_ANT_BUILD;
310 type = IExternalToolConstants.TOOL_TYPE_PROGRAM;
312 String name = (String) args.get(TAG_TOOL_NAME);
314 ILaunchConfigurationWorkingCopy config = newConfig(type, name);
315 if (config == null) {
319 // Update the location...
320 String location = (String) args.get(TAG_TOOL_LOCATION);
321 if (location != null) {
322 ToolUtil.VariableDefinition varDef = ToolUtil.extractVariableTag(
324 if (IExternalToolConstants.VAR_WORKSPACE_LOC.equals(varDef.name)) {
325 location = ToolUtil.buildVariableTag(
326 IExternalToolConstants.VAR_RESOURCE_LOC,
329 config.setAttribute(IExternalToolConstants.ATTR_LOCATION, location);
332 // Update the refresh scope...
333 String refresh = (String) args.get(TAG_TOOL_REFRESH);
334 if (refresh != null) {
335 ToolUtil.VariableDefinition varDef = ToolUtil.extractVariableTag(
337 if ("none".equals(varDef.name)) { //$NON-NLS-1$
340 config.setAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE,
344 // Update the arguments
345 String arguments = (String) args.get(TAG_TOOL_ARGUMENTS);
346 String targetNames = null;
347 if (arguments != null) {
349 ArrayList targets = new ArrayList();
350 StringBuffer buffer = new StringBuffer();
351 ToolUtil.VariableDefinition varDef = ToolUtil.extractVariableTag(
353 while (varDef.end != -1) {
354 if ("ant_target".equals(varDef.name) && varDef.argument != null) { //$NON-NLS-1$
355 targets.add(varDef.argument);
356 buffer.append(arguments.substring(start, varDef.start));
358 buffer.append(arguments.substring(start, varDef.end));
361 varDef = ToolUtil.extractVariableTag(arguments, start);
363 buffer.append(arguments.substring(start, arguments.length()));
364 arguments = buffer.toString();
367 for (int i = 0; i < targets.size(); i++) {
368 String target = (String) targets.get(i);
369 if (target != null && target.length() > 0) {
370 buffer.append(target);
371 buffer.append(","); //$NON-NLS-1$
374 targetNames = buffer.toString();
376 if (targetNames != null && targetNames.length() > 0) {
377 config.setAttribute(IExternalToolConstants.ATTR_ANT_TARGETS,
381 // Collect the rest of the information
382 config.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, TRUE
383 .equals((String) args.get(TAG_TOOL_SHOW_LOG)));
384 config.setAttribute(IExternalToolConstants.ATTR_CAPTURE_OUTPUT, TRUE
385 .equals((String) args.get(TAG_TOOL_SHOW_LOG)));
386 config.setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND,
387 FALSE.equals((String) args.get(TAG_TOOL_BLOCK)));
388 config.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS,
389 (String) args.get(TAG_TOOL_BUILD_TYPES));
390 config.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS,
392 config.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY,
393 (String) args.get(TAG_TOOL_DIRECTORY));
398 * Returns a new working copy with the given external tool name and external
399 * tool type or <code>null</code> if no config could be created.
401 private static ILaunchConfigurationWorkingCopy newConfig(String type,
403 if (type == null || name == null) {
406 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
407 ILaunchConfigurationType configType;
408 // if (IExternalToolConstants.TOOL_TYPE_ANT_BUILD.equals(type)) {
410 // manager.getLaunchConfigurationType(IExternalToolConstants.ID_ANT_BUILDER_LAUNCH_CONFIGURATION_TYPE);
412 if (IExternalToolConstants.TOOL_TYPE_PROGRAM.equals(type)) {
414 .getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_BUILDER_LAUNCH_CONFIGURATION_TYPE);
419 return configType.newInstance(null, name);
420 } catch (CoreException e) {
426 * Returns the tool name extracted from the given command argument map.
427 * Extraction is attempted using 2.0 and 2.1 external tool formats.
429 public static String getNameFromCommandArgs(Map commandArgs) {
430 String name = (String) commandArgs.get(TAG_NAME);
432 name = (String) commandArgs.get(TAG_TOOL_NAME);