Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 157ff0f5310007513b9a972a4253449acdab680e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.eclipse.ui.externaltools.internal.core;

/**********************************************************************
Copyright (c) 2002 IBM Corp. and others.
All rights reserved.   This program and the accompanying materials
are made available under the terms of the Common Public License v0.5
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v05.html
 
Contributors:
**********************************************************************/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.XMLMemento;

/**
 * The registry of user defined external tools that can
 * be run using the external tool menu. It does not include
 * any external tools that are assigned as builders on a
 * project.
 */
public final class ExternalToolsRegistry {
	private static final String STATE_FILE_NAME = "externaltools.xml"; //$NON-NLS-1$
	private static final String TAG_EXTERNALTOOLS = "externaltools"; //$NON-NLS-1$
	private static final String TAG_TOOL = "tool"; //$NON-NLS-1$
	private static final String TAG_ENTRY = "entry"; //$NON-NLS-1$
	private static final String TAG_KEY = "key"; //$NON-NLS-1$
	private static final String TAG_VALUE = "value"; //$NON-NLS-1$
	
	private ArrayList externalTools;

	/**
	 * Creates the registry and loads the saved
	 * external tools.
	 */
	/*package*/ ExternalToolsRegistry() {
		super();
		loadExternalTools();
	}

	/**
	 * Returns the named external tool or <code>null</code>
	 * if not found.
	 */
	public ExternalTool getExternalTool(String name) {
		Iterator enum = externalTools.iterator();
		while (enum.hasNext()) {
			ExternalTool script = (ExternalTool)enum.next();
			if (script.getName().equals(name))
				return script;
		}
		return null;
	}
	
	/**
	 * Returns the external tools of the registry
	 */
	public ArrayList getExternalTools() {
		return externalTools;
	}
	
	/**
	 * Sets the external tools for the registry.
	 * Causes them to be saved to disk.
	 */
	public boolean setExternalTools(ArrayList scripts) {
		this.externalTools = scripts;
		return saveExternalTools();
	}
	
	/**
	 * Loads the external tools from storage and
	 * adds them to the registry.
	 */
	private void loadExternalTools() {
		IPath path = ExternalToolsPlugin.getDefault().getStateLocation();
		path = path.append(STATE_FILE_NAME);
		InputStreamReader reader = null;
		try {
			FileInputStream input = new FileInputStream(path.toFile());
			reader = new InputStreamReader(input, "utf-8"); //$NON-NLS-1$
			XMLMemento memento = XMLMemento.createReadRoot(reader);
			
			// Get the tool script children element
			IMemento[] scripts = memento.getChildren(TAG_TOOL);
			externalTools = new ArrayList(scripts.length);
			for (int i = 0; i < scripts.length; i++) {
				HashMap args = new HashMap();
				IMemento[] entries = scripts[i].getChildren(TAG_ENTRY);
				for (int j = 0; j < entries.length; j++) {
					String key = entries[j].getString(TAG_KEY);
					if (key != null) {
						String value = entries[j].getTextData();
						args.put(key, value);
					}
				}
				ExternalTool script = ExternalTool.fromArgumentMap(args);
				if (script != null)
					externalTools.add(script);
			}
		}
		catch (FileNotFoundException e) {
			// Silently ignore this...
		}
		catch (IOException e) {
			ExternalToolsPlugin.getDefault().log("File I/O error with external tool state reader.", e); //$NON-NLS-1$
		}
		finally {
			if (reader != null) {
				try {
					reader.close();
				} catch(IOException e) {
					ExternalToolsPlugin.getDefault().log("Unable to close external tool state reader.", e); //$NON-NLS-1$
				}
			}
			if (externalTools == null)
				externalTools = new ArrayList(0);
		}
	}
	
	/**
	 * Saves the external tool to storage.
	 * 
	 * @return true if save is successful, false otherwise.
	 */
	/*package*/ boolean saveExternalTools() {
		boolean successful = true;
		
		// Populate the memento
		XMLMemento memento = XMLMemento.createWriteRoot(TAG_EXTERNALTOOLS);
		Iterator enum = externalTools.iterator();
		while (enum.hasNext()) {
			IMemento scriptMemento = memento.createChild(TAG_TOOL);
			ExternalTool script = (ExternalTool)enum.next();
			Map args = script.toArgumentMap();
			Iterator entries = args.entrySet().iterator();
			while (entries.hasNext()) {
				Map.Entry entry = (Map.Entry)entries.next();
				IMemento entryMemento = scriptMemento.createChild(TAG_ENTRY);
				entryMemento.putString(TAG_KEY, (String)entry.getKey());
				entryMemento.putTextData((String)entry.getValue());
			}
		}

		// Write the memento to the state file		
		IPath path = ExternalToolsPlugin.getDefault().getStateLocation();
		path = path.append(STATE_FILE_NAME);
		File stateFile = path.toFile();
		try {
			FileOutputStream stream = new FileOutputStream(stateFile);
			OutputStreamWriter writer = new OutputStreamWriter(stream, "utf-8"); //$NON-NLS-1$
			memento.save(writer);
			writer.close();
		} catch (IOException e) {
			stateFile.delete();
			MessageDialog.openError(
				null,
				ToolMessages.getString("ExternalToolsRegistry.saveStateErrorTitle"), //$NON-NLS-1$
				ToolMessages.getString("ExternalToolsRegistry.saveStateError")); //$NON-NLS-1$
			successful = false;
		}
		
		return successful;
	}
}

Back to the top