KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > registry > ExternalToolMigration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.externaltools.internal.registry;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
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 /**
31  * Responsible reading an old external tool format and creating
32  * and migrating it to create a new external tool.
33  */

34 public final class ExternalToolMigration {
35     /**
36      * Structure to represent a variable definition within a
37      * source string.
38      */

39     public static final class VariableDefinition {
40         /**
41          * Index in the source text where the variable started
42          * or <code>-1</code> if no valid variable start tag
43          * identifier found.
44          */

45         public int start = -1;
46         
47         /**
48          * Index in the source text of the character following
49          * the end of the variable or <code>-1</code> if no
50          * valid variable end tag found.
51          */

52         public int end = -1;
53         
54         /**
55          * The variable's name found in the source text, or
56          * <code>null</code> if no valid variable found.
57          */

58         public String JavaDoc name = null;
59         
60         /**
61          * The variable's argument found in the source text, or
62          * <code>null</code> if no valid variable found or if
63          * the variable did not specify an argument
64          */

65         public String JavaDoc argument = null;
66         
67         /**
68          * Create an initialized variable definition.
69          */

70         private VariableDefinition() {
71             super();
72         }
73     }
74     
75     /**
76      * Variable tag indentifiers
77      */

78     private static final String JavaDoc VAR_TAG_START = "${"; //$NON-NLS-1$
79
private static final String JavaDoc VAR_TAG_END = "}"; //$NON-NLS-1$
80
private static final String JavaDoc VAR_TAG_SEP = ":"; //$NON-NLS-1$
81

82     /**
83      * External tool type for Ant build files (value <code>antBuildType</code>).
84      */

85     public static final String JavaDoc TOOL_TYPE_ANT_BUILD = "antBuildType"; //$NON-NLS-1$;
86
/**
87      * Ant builder launch configuration type identifier. Ant project builders
88      * are of this type.
89      */

90     public static final String JavaDoc ID_ANT_BUILDER_LAUNCH_CONFIGURATION_TYPE = "org.eclipse.ant.AntBuilderLaunchConfigurationType"; //$NON-NLS-1$
91

92     public static final String JavaDoc RUN_TARGETS_ATTRIBUTE = TOOL_TYPE_ANT_BUILD + ".runTargets"; //$NON-NLS-1$;
93

94     /**
95     * String attribute indicating the Ant targets to execute. Default value is
96      * <code>null</code> which indicates that the default target is to be
97      * executed. Format is a comma separated listing of targets.
98      * NOTE: This value is copied here from org.eclipse.ant.ui.internal.IAntLaunchConfigurationConstants.
99      * Ant no longer resides in External Tools and this plug-in. This value is kept here only
100      * for migration.
101      */

102     public static final String JavaDoc ATTR_ANT_TARGETS = IExternalToolConstants.PLUGIN_ID + ".ATTR_ANT_TARGETS"; //$NON-NLS-1$
103

104     /*
105      * 2.0 External Tool Tags
106      */

107     public static final String JavaDoc TAG_TOOL_TYPE = "!{tool_type}"; //$NON-NLS-1$
108
public static final String JavaDoc TAG_TOOL_NAME = "!{tool_name}"; //$NON-NLS-1$
109
public static final String JavaDoc TAG_TOOL_LOCATION = "!{tool_loc}"; //$NON-NLS-1$
110
public static final String JavaDoc TAG_TOOL_ARGUMENTS = "!{tool_args}"; //$NON-NLS-1$
111
public static final String JavaDoc TAG_TOOL_DIRECTORY = "!{tool_dir}"; //$NON-NLS-1$
112
public static final String JavaDoc TAG_TOOL_REFRESH = "!{tool_refresh}"; //$NON-NLS-1$
113
public static final String JavaDoc TAG_TOOL_SHOW_LOG = "!{tool_show_log}"; //$NON-NLS-1$
114
public static final String JavaDoc TAG_TOOL_BUILD_TYPES = "!{tool_build_types}"; //$NON-NLS-1$
115
public static final String JavaDoc TAG_TOOL_BLOCK = "!{tool_block}"; //$NON-NLS-1$
116

117     // Known kind of tools
118
private static final String JavaDoc TOOL_TYPE_ANT = "org.eclipse.ui.externaltools.type.ant"; //$NON-NLS-1$
119
private static final String JavaDoc TOOL_TYPE_PROGRAM = "org.eclipse.ui.externaltools.type.program"; //$NON-NLS-1$
120

121     /*
122      * 2.1 External Tool Keys
123      */

124     public static final String JavaDoc TAG_TYPE = "type"; //$NON-NLS-1$
125
public static final String JavaDoc TAG_NAME = "name"; //$NON-NLS-1$
126
public static final String JavaDoc TAG_LOCATION = "location"; //$NON-NLS-1$
127
public static final String JavaDoc TAG_WORK_DIR = "workDirectory"; //$NON-NLS-1$
128
public static final String JavaDoc TAG_CAPTURE_OUTPUT = "captureOutput"; //$NON-NLS-1$
129
public static final String JavaDoc TAG_SHOW_CONSOLE = "showConsole"; //$NON-NLS-1$
130
public static final String JavaDoc TAG_RUN_BKGRND = "runInBackground"; //$NON-NLS-1$
131
public static final String JavaDoc TAG_PROMPT_ARGS = "promptForArguments"; //$NON-NLS-1$
132
public static final String JavaDoc TAG_ARGS = "arguments"; //$NON-NLS-1$
133
public static final String JavaDoc TAG_REFRESH_SCOPE = "refreshScope"; //$NON-NLS-1$
134
public static final String JavaDoc TAG_REFRESH_RECURSIVE = "refreshRecursive"; //$NON-NLS-1$
135
public static final String JavaDoc TAG_RUN_BUILD_KINDS = "runForBuildKinds"; //$NON-NLS-1$
136
public static final String JavaDoc TAG_EXTRA_ATTR = "extraAttribute"; //$NON-NLS-1$
137
public static final String JavaDoc TAG_VERSION = "version"; //$NON-NLS-1$
138

139     private static final String JavaDoc EXTRA_ATTR_SEPARATOR = "="; //$NON-NLS-1$
140

141     private static final String JavaDoc VERSION_21 = "2.1"; //$NON-NLS-1$;
142

143     private static final String JavaDoc TRUE = "true"; //$NON-NLS-1$
144
private static final String JavaDoc FALSE = "false"; //$NON-NLS-1$
145

146     /**
147      * Allows no instances.
148      */

149     private ExternalToolMigration() {
150         super();
151     }
152
153     /**
154      * Returns a launch configuration working copy from the argument map or
155      * <code>null</code> if the given map cannot be interpreted as a 2.0 or 2.1
156      * branch external tool. The returned working copy will be unsaved and its
157      * location will be set to the metadata area.
158      */

159     public static ILaunchConfigurationWorkingCopy configFromArgumentMap(Map JavaDoc args) {
160         String JavaDoc version = (String JavaDoc) 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 JavaDoc commandArgs) {
168         String JavaDoc name = (String JavaDoc) commandArgs.get(TAG_NAME);
169         String JavaDoc type = (String JavaDoc) 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 JavaDoc) commandArgs.get(TAG_LOCATION));
177         config.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String JavaDoc) 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 JavaDoc) 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 JavaDoc) commandArgs.get(TAG_RUN_BUILD_KINDS));
186         
187         String JavaDoc args = (String JavaDoc) commandArgs.get(TAG_ARGS);
188         if (args != null) {
189             config.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, args);
190         }
191
192         String JavaDoc extraAttributes = (String JavaDoc) commandArgs.get(TAG_EXTRA_ATTR);
193         if (extraAttributes != null) {
194             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(extraAttributes, EXTRA_ATTR_SEPARATOR);
195             while (tokenizer.hasMoreTokens()) {
196                 String JavaDoc key = tokenizer.nextToken();
197                 if (!tokenizer.hasMoreTokens())
198                     break;
199                 String JavaDoc value = tokenizer.nextToken();
200                 if (key.equals(RUN_TARGETS_ATTRIBUTE)) {
201                     // 2.1 implementation only defined 1 "extra attribute"
202
config.setAttribute(ATTR_ANT_TARGETS, value);
203                 }
204             }
205         }
206         return config;
207     }
208
209     /**
210      * Creates an external tool from the map.
211      */

212     public static ILaunchConfigurationWorkingCopy configFrom20ArgumentMap(Map JavaDoc args) {
213         // Update the type...
214
String JavaDoc type = (String JavaDoc) 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 JavaDoc name = (String JavaDoc) args.get(TAG_TOOL_NAME);
224         
225         ILaunchConfigurationWorkingCopy config = newConfig(type, name);
226         if (config == null) {
227             return null;
228         }
229
230         // Update the location...
231
String JavaDoc location = (String JavaDoc) args.get(TAG_TOOL_LOCATION);
232         config.setAttribute(IExternalToolConstants.ATTR_LOCATION, location);
233
234         // Update the refresh scope...
235
String JavaDoc refresh = (String JavaDoc) args.get(TAG_TOOL_REFRESH);
236         if (refresh != null) {
237             VariableDefinition varDef = extractVariableDefinition(refresh, 0);
238             if ("none".equals(varDef.name)) { //$NON-NLS-1$
239
refresh = null;
240             }
241             config.setAttribute(RefreshTab.ATTR_REFRESH_SCOPE, refresh);
242         }
243
244         // Update the arguments
245
String JavaDoc arguments = (String JavaDoc) args.get(TAG_TOOL_ARGUMENTS);
246         if (type.equals(TOOL_TYPE_ANT_BUILD)) {
247             String JavaDoc targetNames = null;
248             if (arguments != null) {
249                 int start = 0;
250                 ArrayList JavaDoc targets = new ArrayList JavaDoc();
251                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
252                 VariableDefinition varDef = extractVariableDefinition(arguments, start);
253                 while (varDef.end != -1) {
254                     if ("ant_target".equals(varDef.name) && varDef.argument != null) { //$NON-NLS-1$
255
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 JavaDoc target = (String JavaDoc) targets.get(i);
269                     if (target != null && target.length() > 0) {
270                         buffer.append(target);
271                         buffer.append(","); //$NON-NLS-1$
272
}
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         // Collect the rest of the information
283
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 JavaDoc buildKinds= (String JavaDoc) args.get(TAG_TOOL_BUILD_TYPES);
287         if (buildKinds != null) {
288             buildKinds= buildKinds.replace(';', ','); // Replace the old separator with the new
289
}
290         config.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, buildKinds);
291         config.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String JavaDoc) args.get(TAG_TOOL_DIRECTORY));
292         return config;
293     }
294
295     /**
296      * Returns a new working copy with the given external tool name and external
297      * tool type or <code>null</code> if no config could be created.
298      */

299     private static ILaunchConfigurationWorkingCopy newConfig(String JavaDoc type, String JavaDoc 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     /**
323      * Returns the tool name extracted from the given command argument map.
324      * Extraction is attempted using 2.0 and 2.1 external tool formats.
325      */

326     public static String JavaDoc getNameFromCommandArgs(Map JavaDoc commandArgs) {
327         String JavaDoc name= (String JavaDoc) commandArgs.get(TAG_NAME);
328         if (name == null) {
329             name= (String JavaDoc) commandArgs.get(TAG_TOOL_NAME);
330         }
331         return name;
332     }
333     
334     /**
335      * Migrate the old RUN_IN_BACKGROUND launch config attribute to the new
336      * LAUNCH_IN_BACKGROUND attribute provided by the debug ui plugin.
337      *
338      * @param config the config to migrate
339      * @return the migrated config
340      */

341     public static ILaunchConfiguration migrateRunInBackground(ILaunchConfiguration config) {
342         String JavaDoc noValueFlag= "NoValue"; //$NON-NLS-1$
343
String JavaDoc attr= null;
344         try {
345             attr = config.getAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, noValueFlag);
346         } catch (CoreException e) {
347             // Exception will occur if the attribute is already set because the attribute is actually a boolean.
348
// No migration necessary.
349
return config;
350         }
351         if (noValueFlag.equals(attr)) {
352             //the old constant
353
String JavaDoc ATTR_RUN_IN_BACKGROUND= IExternalToolConstants.PLUGIN_ID + ".ATTR_RUN_IN_BACKGROUND"; //$NON-NLS-1$
354
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     /**
372      * Extracts a variable name and argument from the given string.
373      *
374      * @param text the source text to parse for a variable tag
375      * @param start the index in the string to start the search
376      * @return the variable definition
377      */

378     public static VariableDefinition extractVariableDefinition(String JavaDoc 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