Added a ProblemsLabelDecorator for the Navigator tree view.
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpdt / externaltools / internal / registry / ExternalToolMigration.java
1 package net.sourceforge.phpdt.externaltools.internal.registry;
2
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
8
9 Contributors:
10 **********************************************************************/
11
12 import java.io.File;
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;
19 import java.util.Map;
20 import java.util.StringTokenizer;
21
22 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
23 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
24 import net.sourceforge.phpeclipse.externaltools.ExternalToolsPlugin;
25
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;
35
36 /**
37  * Responsible reading an old external tool format and creating
38  * and migrating it to create a new external tool.
39  */
40 public final class ExternalToolMigration {
41         private static final String SEPERATOR = ";"; //$NON-NLS-1$      
42         private static final String STATE_FILE_NAME_OLD = "oldexternaltools.xml"; //$NON-NLS-1$
43         private static final String STATE_FILE_NAME = "externaltools.xml"; //$NON-NLS-1$
44         private static final String TAG_EXTERNALTOOLS = "externaltools"; //$NON-NLS-1$
45         private static final String TAG_TOOL = "tool"; //$NON-NLS-1$
46         private static final String TAG_ENTRY = "entry"; //$NON-NLS-1$
47         //private static final String TAG_KEY = "key"; //$NON-NLS-1$
48         private static final String TAG_VALUE = "value"; //$NON-NLS-1$
49
50         /*
51          * Ant tags
52          */
53         //public static final String RUN_TARGETS_ATTRIBUTE = IExternalToolConstants.TOOL_TYPE_ANT_BUILD + ".runTargets"; //$NON-NLS-1$;
54
55         /*
56          * 2.0 External Tool Tags
57          */
58         private static final String TAG_TOOL_TYPE = "!{tool_type}"; //$NON-NLS-1$
59         private static final String TAG_TOOL_NAME = "!{tool_name}"; //$NON-NLS-1$
60         private static final String TAG_TOOL_LOCATION = "!{tool_loc}"; //$NON-NLS-1$
61         private static final String TAG_TOOL_ARGUMENTS = "!{tool_args}"; //$NON-NLS-1$
62         private static final String TAG_TOOL_DIRECTORY = "!{tool_dir}"; //$NON-NLS-1$
63         private static final String TAG_TOOL_REFRESH = "!{tool_refresh}"; //$NON-NLS-1$
64         private static final String TAG_TOOL_SHOW_LOG = "!{tool_show_log}"; //$NON-NLS-1$
65         private static final String TAG_TOOL_BUILD_TYPES = "!{tool_build_types}"; //$NON-NLS-1$
66         private static final String TAG_TOOL_BLOCK = "!{tool_block}"; //$NON-NLS-1$
67
68         // Known kind of tools
69         private static final String TOOL_TYPE_PROGRAM = "net.sourceforge.phpdt.externaltools.type.program"; //$NON-NLS-1$
70         //private static final String TOOL_TYPE_ANT = "org.eclipse.ui.externaltools.type.ant"; //$NON-NLS-1$
71
72         /*
73          * 2.1 External Tool Keys
74          */
75         private static final String TAG_EXTERNAL_TOOL = "externalTool"; //$NON-NLS-1$
76         private static final String TAG_TYPE = "type"; //$NON-NLS-1$
77         private static final String TAG_NAME = "name"; //$NON-NLS-1$
78         private static final String TAG_LOCATION = "location"; //$NON-NLS-1$
79         private static final String TAG_WORK_DIR = "workDirectory"; //$NON-NLS-1$
80         private static final String TAG_CAPTURE_OUTPUT = "captureOutput"; //$NON-NLS-1$
81         private static final String TAG_SHOW_CONSOLE = "showConsole"; //$NON-NLS-1$
82         private static final String TAG_RUN_BKGRND = "runInBackground"; //$NON-NLS-1$
83         private static final String TAG_OPEN_PERSP = "openPerspective"; //$NON-NLS-1$
84         private static final String TAG_PROMPT_ARGS = "promptForArguments"; //$NON-NLS-1$
85         private static final String TAG_SHOW_MENU = "showInMenu"; //$NON-NLS-1$
86         private static final String TAG_SAVE_DIRTY = "saveDirtyEditors"; //$NON-NLS-1$
87         private static final String TAG_ARGS = "arguments"; //$NON-NLS-1$
88         private static final String TAG_REFRESH_SCOPE = "refreshScope"; //$NON-NLS-1$
89         private static final String TAG_REFRESH_RECURSIVE = "refreshRecursive"; //$NON-NLS-1$
90         private static final String TAG_RUN_BUILD_KINDS = "runForBuildKinds"; //$NON-NLS-1$
91         private static final String TAG_EXTRA_ATTR = "extraAttribute"; //$NON-NLS-1$
92         private static final String TAG_KEY = "key"; //$NON-NLS-1$
93         private static final String TAG_VERSION = "version"; //$NON-NLS-1$
94
95         private static final String BUILD_TYPE_SEPARATOR = ","; //$NON-NLS-1$
96         private static final String EXTRA_ATTR_SEPARATOR = "="; //$NON-NLS-1$
97
98         private static final String VERSION_21 = "2.1"; //$NON-NLS-1$;
99
100         private static final String TRUE = "true"; //$NON-NLS-1$
101         private static final String FALSE = "false"; //$NON-NLS-1$
102
103         /**
104          * Allows no instances.
105          */
106         private ExternalToolMigration() {
107                 super();
108         }
109
110         /**
111          * Loads the external tools from storage and
112          * adds them to the registry.
113          */
114         /*package*/
115         // This method is not called. It is left here in case 
116         // we decide to do tool migration in the future
117         private static void readInOldTools() {
118                 readIn20Tools();
119                 readIn21Tools();
120         }
121
122         private static void readIn21Tools() {
123         }
124
125         public static void readIn20Tools() {
126                 boolean migrationSuccessful = true;
127                 IPath path = ExternalToolsPlugin.getDefault().getStateLocation();
128                 File file = path.append(STATE_FILE_NAME).toFile();
129                 if (!file.exists())
130                         return;
131
132                 InputStreamReader reader = null;
133                 try {
134                         FileInputStream input = new FileInputStream(file);
135                         reader = new InputStreamReader(input, "utf-8"); //$NON-NLS-1$
136                         XMLMemento memento = XMLMemento.createReadRoot(reader);
137
138                         // Get the external tool children element
139                         IMemento[] tools = memento.getChildren(TAG_TOOL);
140                         for (int i = 0; i < tools.length; i++) {
141                                 HashMap args = new HashMap();
142                                 IMemento[] entries = tools[i].getChildren(TAG_ENTRY);
143                                 for (int j = 0; j < entries.length; j++) {
144                                         String key = entries[j].getString(TAG_KEY);
145                                         if (key != null) {
146                                                 String value = entries[j].getTextData();
147                                                 args.put(key, value);
148                                         }
149                                 }
150                                 ILaunchConfigurationWorkingCopy config = configFromArgumentMap(args);
151                                 if (config != null) {
152                                         try {
153                                                 config.doSave();
154                                         } catch (CoreException e) {
155                                                 // TODO: Decide what to do when saving fails.
156                                         }
157                                 }
158                         }
159                 } catch (FileNotFoundException e) {
160                         // Silently ignore this...
161                 } catch (IOException e) {
162                         ExternalToolsPlugin.getDefault().log("File I/O error with reading old external tools.", e); 
163                         migrationSuccessful = false;
164                 } catch (WorkbenchException e) {
165                   ExternalToolsPlugin.getDefault().getLog().log(e.getStatus());
166                         System.err.println("Error reading old external tools. See .log file for more details"); 
167                         migrationSuccessful = false;
168                 } finally {
169                         if (reader != null) {
170                                 try {
171                                         reader.close();
172                                 } catch (IOException e) {
173                                         ExternalToolsPlugin.getDefault().log("Unable to close external tool old state reader.", e); 
174                                 }
175                         }
176                 }
177
178                 if (migrationSuccessful) {
179                         if (!file.renameTo(path.append(STATE_FILE_NAME_OLD).toFile())) {
180                                 ExternalToolsPlugin.getDefault().log("Unable to rename old external tool state file. Please rename externaltools.xml to oldexternaltools.xml manually.", null); 
181                                 System.err.println("Unable to rename old external tool state file. Please rename externaltools.xml to oldexternaltools.xml manually."); 
182                         }
183                 }
184         }
185
186         /**
187          * Returns a  launch configuration working copy from the argument map or
188          * <code>null</code> if the given map cannot be interpreted as a 2.0 or 2.1
189          * branch external tool. The returned working copy will be unsaved and its
190          * location will be set to the metadata area.
191          */
192         public static ILaunchConfigurationWorkingCopy configFromArgumentMap(Map args) {
193                 String version = (String) args.get(TAG_VERSION);
194                 if (VERSION_21.equals(version)) {
195                         return configFrom21ArgumentMap(args);
196                 }
197                 return configFrom20ArgumentMap(args);
198         }
199
200         public static ILaunchConfigurationWorkingCopy configFrom21ArgumentMap(Map commandArgs) {
201                 String name = (String) commandArgs.get(TAG_NAME);
202                 String type = (String) commandArgs.get(TAG_TYPE);
203                 
204                 ILaunchConfigurationWorkingCopy config = newConfig(type, name);
205                 if (config == null) {
206                         return null;
207                 }
208                 
209                 config.setAttribute(IExternalToolConstants.ATTR_LOCATION, (String) commandArgs.get(TAG_LOCATION));
210                 config.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String) commandArgs.get(TAG_WORK_DIR));
211                 config.setAttribute(IExternalToolConstants.ATTR_CAPTURE_OUTPUT, TRUE.equals((String) commandArgs.get(TAG_CAPTURE_OUTPUT)));
212                 config.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, TRUE.equals((String) commandArgs.get(TAG_SHOW_CONSOLE)));
213                 config.setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, TRUE.equals((String) commandArgs.get(TAG_RUN_BKGRND)));
214                 config.setAttribute(IExternalToolConstants.ATTR_PROMPT_FOR_ARGUMENTS, TRUE.equals((String) commandArgs.get(TAG_PROMPT_ARGS)));
215                 config.setAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE, (String) commandArgs.get(TAG_REFRESH_SCOPE));
216                 config.setAttribute(IExternalToolConstants.ATTR_REFRESH_RECURSIVE, TRUE.equals((String) commandArgs.get(TAG_REFRESH_RECURSIVE)));
217
218                 config.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, (String) commandArgs.get(TAG_RUN_BUILD_KINDS));
219                 
220                 String args = (String) commandArgs.get(TAG_ARGS);
221                 if (args != null) {
222                         config.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, args);
223                 }
224
225                 String extraAttributes = (String) commandArgs.get(TAG_EXTRA_ATTR);
226                 if (extraAttributes != null) {
227                         StringTokenizer tokenizer = new StringTokenizer(extraAttributes, EXTRA_ATTR_SEPARATOR);
228                         while (tokenizer.hasMoreTokens()) {
229                                 String key = tokenizer.nextToken();
230                                 if (!tokenizer.hasMoreTokens())
231                                         break;
232                                 String value = tokenizer.nextToken();
233 //                              if (key.equals(RUN_TARGETS_ATTRIBUTE)) {
234 //                                      // 2.1 implementation only defined 1 "extra attribute"
235 //                                      config.setAttribute(IExternalToolConstants.ATTR_ANT_TARGETS, value);
236 //                              }
237                         }
238                 }
239                 return config;
240         }
241
242         /**
243          * Creates an external tool from the map.
244          */
245         public static ILaunchConfigurationWorkingCopy configFrom20ArgumentMap(Map args) {
246                 // Update the type...
247                 String type = (String) args.get(TAG_TOOL_TYPE);
248 //              if (TOOL_TYPE_ANT.equals(type))
249 //                      type = IExternalToolConstants.TOOL_TYPE_ANT_BUILD;
250 //              else
251                         type = IExternalToolConstants.TOOL_TYPE_PROGRAM;
252
253                 String name = (String) args.get(TAG_TOOL_NAME);
254                 
255                 ILaunchConfigurationWorkingCopy config = newConfig(type, name);
256                 if (config == null) {
257                         return null;
258                 }
259
260                 // Update the location...
261                 String location = (String) args.get(TAG_TOOL_LOCATION);
262                 if (location != null) {
263                         ToolUtil.VariableDefinition varDef = ToolUtil.extractVariableTag(location, 0);
264                         if (IExternalToolConstants.VAR_WORKSPACE_LOC.equals(varDef.name)) {
265                                 location = ToolUtil.buildVariableTag(IExternalToolConstants.VAR_RESOURCE_LOC, varDef.argument);
266                         }
267                         config.setAttribute(IExternalToolConstants.ATTR_LOCATION, location);
268                 }
269
270                 // Update the refresh scope...
271                 String refresh = (String) args.get(TAG_TOOL_REFRESH);
272                 if (refresh != null) {
273                         ToolUtil.VariableDefinition varDef = ToolUtil.extractVariableTag(refresh, 0);
274                         if ("none".equals(varDef.name)) { //$NON-NLS-1$
275                                 refresh = null;
276                         }
277                         config.setAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE, refresh);
278                 }
279
280                 // Update the arguments
281                 String arguments = (String) args.get(TAG_TOOL_ARGUMENTS);
282                 String targetNames = null;
283                 if (arguments != null) {
284                         int start = 0;
285                         ArrayList targets = new ArrayList();
286                         StringBuffer buffer = new StringBuffer();
287                         ToolUtil.VariableDefinition varDef = ToolUtil.extractVariableTag(arguments, start);
288                         while (varDef.end != -1) {
289                                 if ("ant_target".equals(varDef.name) && varDef.argument != null) { //$NON-NLS-1$
290                                         targets.add(varDef.argument);
291                                         buffer.append(arguments.substring(start, varDef.start));
292                                 } else {
293                                         buffer.append(arguments.substring(start, varDef.end));
294                                 }
295                                 start = varDef.end;
296                                 varDef = ToolUtil.extractVariableTag(arguments, start);
297                         }
298                         buffer.append(arguments.substring(start, arguments.length()));
299                         arguments = buffer.toString();
300
301                         buffer.setLength(0);
302                         for (int i = 0; i < targets.size(); i++) {
303                                 String target = (String) targets.get(i);
304                                 if (target != null && target.length() > 0) {
305                                         buffer.append(target);
306                                         buffer.append(","); //$NON-NLS-1$
307                                 }
308                         }
309                         targetNames = buffer.toString();
310                 }
311                 if (targetNames != null && targetNames.length() > 0) {
312                         config.setAttribute(IExternalToolConstants.ATTR_ANT_TARGETS, targetNames);
313                 }
314
315                 // Collect the rest of the information
316                 config.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, TRUE.equals((String) args.get(TAG_TOOL_SHOW_LOG)));
317                 config.setAttribute(IExternalToolConstants.ATTR_CAPTURE_OUTPUT, TRUE.equals((String) args.get(TAG_TOOL_SHOW_LOG)));
318                 config.setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, FALSE.equals((String) args.get(TAG_TOOL_BLOCK)));
319                 config.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, (String) args.get(TAG_TOOL_BUILD_TYPES));
320                 config.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments);
321                 config.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String) args.get(TAG_TOOL_DIRECTORY));
322                 return config;
323         }
324
325         /**
326          * Returns a new working copy with the given external tool name and external
327          * tool type or <code>null</code> if no config could be created.
328          */
329         private static ILaunchConfigurationWorkingCopy newConfig(String type, String name) {
330                 if (type == null || name == null) {
331                         return null;
332                 }
333                 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
334                 ILaunchConfigurationType configType;
335 //              if (IExternalToolConstants.TOOL_TYPE_ANT_BUILD.equals(type)) {
336 //                      configType = manager.getLaunchConfigurationType(IExternalToolConstants.ID_ANT_BUILDER_LAUNCH_CONFIGURATION_TYPE);
337 //              } else 
338                 if (IExternalToolConstants.TOOL_TYPE_PROGRAM.equals(type)) {
339                         configType = manager.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_BUILDER_LAUNCH_CONFIGURATION_TYPE);
340                 } else {
341                         return null;
342                 }
343                 try {
344                         return configType.newInstance(null, name);
345                 } catch (CoreException e) {
346                         return null;
347                 }
348         }
349         
350         /**
351          * Returns the tool name extracted from the given command argument map.
352          * Extraction is attempted using 2.0 and 2.1 external tool formats.
353          */
354         public static String getNameFromCommandArgs(Map commandArgs) {
355                 String name= (String) commandArgs.get(TAG_NAME);
356                 if (name == null) {
357                         name= (String) commandArgs.get(TAG_TOOL_NAME);
358                 }
359                 return name;
360         }
361         
362 }