Skip to main content
summaryrefslogtreecommitdiffstats
blob: d3fa5fd227157303b045f0d2cf3ad055f105d40a (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.mylyn.internal.tasks.core.externalization;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.core.TaskExternalizationException;
import org.eclipse.mylyn.internal.tasks.core.TaskList;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryQuery;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.core.AbstractTaskCategory;
import org.eclipse.mylyn.tasks.core.AbstractTaskContainer;
import org.eclipse.mylyn.tasks.core.AbstractTaskListFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Mik Kersten
 * @author Ken Sueda
 * @author Rob Elves
 * @author Jevgeni Holodkov
 */
public class TaskListExternalizer {

	private static final String ERROR_TASKLIST_READ = "Failed to load Task List";

	private static final String TRANSFORM_PROPERTY_VERSION = "version";

	// May 2007: There was a bug when reading in 1.1
	// Result was an infinite loop within the parser
	private static final String XML_VERSION = "1.0";

	public static final String ATTRIBUTE_VERSION = "Version";

	public static final String ELEMENT_TASK_LIST = "TaskList";

	private static final String VALUE_VERSION = "1.0.1";

	private static final String VALUE_VERSION_1_0_0 = "1.0.0";

	private List<AbstractTaskListFactory> externalizers;

	private DelegatingTaskExternalizer delagatingExternalizer;

	private final List<Node> orphanedTaskNodes = new ArrayList<Node>();

	private final List<Node> orphanedQueryNodes = new ArrayList<Node>();

	private String readVersion = "";

	public TaskListExternalizer() {
		this.delagatingExternalizer = new DelegatingTaskExternalizer();
	}

	public void setDelegateExternalizers(List<AbstractTaskListFactory> externalizers) {
		this.externalizers = externalizers;
		this.delagatingExternalizer.setFactories(externalizers);
	}

	public void writeTaskList(TaskList taskList, File outFile) throws CoreException {
		try {
			FileOutputStream outStream = new FileOutputStream(outFile);
			try {
				writeTaskList(taskList, outStream);
			} finally {
				outStream.close();
			}
		} catch (Exception e) {
			throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Saving Task List failed",
					e));
		}
	}

	private void writeTaskList(TaskList taskList, OutputStream outputStream) throws IOException {
		Document doc = createTaskListDocument();
		if (doc == null) {
			return;
		}

		Element root = createTaskListRoot(doc);

		// create task nodes...
		for (AbstractTask task : taskList.getAllTasks()) {
			delagatingExternalizer.createTaskElement(task, doc, root);
		}

		// create the categorie nodes...
		for (AbstractTaskContainer category : taskList.getCategories()) {
			delagatingExternalizer.createCategoryElement(category, doc, root);
		}

		// create query nodes...
		for (AbstractRepositoryQuery query : taskList.getQueries()) {
			try {
				delagatingExternalizer.createQueryElement(query, doc, root);
			} catch (Throwable t) {
				// FIXME use log?
				StatusHandler.fail(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Did not externalize: "
						+ query.getSummary(), t));
			}
		}

		// Persist orphaned tasks...
		for (Node orphanedTaskNode : orphanedTaskNodes) {
			Node tempNode = doc.importNode(orphanedTaskNode, true);
			if (tempNode != null) {
				root.appendChild(tempNode);
			}
		}

		// Persist orphaned queries....
		for (Node orphanedQueryNode : orphanedQueryNodes) {
			Node tempNode = doc.importNode(orphanedQueryNode, true);
			if (tempNode != null) {
				root.appendChild(tempNode);
			}
		}

		ZipOutputStream zipOutStream = new ZipOutputStream(outputStream);
		writeTaskList(doc, zipOutStream);
		zipOutStream.finish();
	}

	/**
	 * @param doc
	 * @param outputStream
	 * @throws IOException
	 */
	private void writeTaskList(Document doc, ZipOutputStream outputStream) throws IOException {
		ZipEntry zipEntry = new ZipEntry(ITasksCoreConstants.OLD_TASK_LIST_FILE);
		outputStream.putNextEntry(zipEntry);
		outputStream.setMethod(ZipOutputStream.DEFLATED);
		writeDOMtoStream(doc, outputStream);
		outputStream.flush();
		outputStream.closeEntry();
	}

	/**
	 * Writes the provided XML document out to the specified output stream.
	 * 
	 * doc - the document to be written outputStream - the stream to which the document is to be written
	 */
	private void writeDOMtoStream(Document doc, OutputStream outputStream) {
		// Prepare the DOM document for writing
		// DOMSource - Acts as a holder for a transformation Source tree in the
		// form of a Document Object Model (DOM) tree
		Source source = new DOMSource(doc);

		// StreamResult - Acts as an holder for a XML transformation result
		// Prepare the output stream
		Result result = new StreamResult(outputStream);

		// An instance of this class can be obtained with the
		// TransformerFactory.newTransformer method. This instance may
		// then be used to process XML from a variety of sources and write
		// the transformation output to a variety of sinks

		Transformer xformer = null;
		try {
			xformer = TransformerFactory.newInstance().newTransformer();
			xformer.setOutputProperty(TRANSFORM_PROPERTY_VERSION, XML_VERSION);
			xformer.transform(source, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e1) {
			e1.printStackTrace();
		}
	}

	private Document createTaskListDocument() {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		Document doc = null;

		try {
			db = dbf.newDocumentBuilder();
			doc = db.newDocument();
		} catch (ParserConfigurationException e) {
			// FIXME propagate exception?
			StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Could not create document", e));
			return doc;
		}

		return doc;
	}

	private Element createTaskListRoot(Document doc) {
		Element root = doc.createElement(ELEMENT_TASK_LIST);
		root.setAttribute(ATTRIBUTE_VERSION, VALUE_VERSION);
		doc.appendChild(root);
		return root;
	}

	/**
	 * Reads the Query from the specified Node. If taskList is not null, then also adds this query to the TaskList
	 * 
	 * @throws TaskExternalizationException
	 */
	private AbstractRepositoryQuery readQuery(TaskList taskList, Node child) {
		AbstractRepositoryQuery query = null;
		for (AbstractTaskListFactory externalizer : externalizers) {
			Set<String> queryTagNames = externalizer.getQueryElementNames();
			if (queryTagNames != null && queryTagNames.contains(child.getNodeName())) {
				Element childElement = (Element) child;
				// TODO: move this stuff into externalizer
				String repositoryUrl = childElement.getAttribute(DelegatingTaskExternalizer.KEY_REPOSITORY_URL);
				String queryString = childElement.getAttribute(AbstractTaskListFactory.KEY_QUERY_STRING);
				if (queryString.length() == 0) { // fallback for legacy
					queryString = childElement.getAttribute(AbstractTaskListFactory.KEY_QUERY);
				}
				String label = childElement.getAttribute(DelegatingTaskExternalizer.KEY_NAME);
				if (label.length() == 0) { // fallback for legacy
					label = childElement.getAttribute(DelegatingTaskExternalizer.KEY_LABEL);
				}

				query = externalizer.createQuery(repositoryUrl, queryString, label, childElement);
				if (query != null) {
					if (childElement.getAttribute(DelegatingTaskExternalizer.KEY_LAST_REFRESH) != null
							&& !childElement.getAttribute(DelegatingTaskExternalizer.KEY_LAST_REFRESH).equals("")) {
						query.setLastSynchronizedStamp(childElement.getAttribute(DelegatingTaskExternalizer.KEY_LAST_REFRESH));
					}
				}

				// add created Query to the TaskList and read QueryHits (Tasks related to the Query)
				if (taskList != null) {
					if (query != null) {
						taskList.addQuery(query);
					}

					NodeList queryChildren = child.getChildNodes();
					delagatingExternalizer.readTaskReferences(query, queryChildren, taskList);
				}

				break;
			}
		}
		if (query == null) {
			orphanedQueryNodes.add(child);
		}

		return query;
	}

	public void setDelegatingExternalizer(DelegatingTaskExternalizer delagatingExternalizer) {
		this.delagatingExternalizer = delagatingExternalizer;
	}

	public List<AbstractTaskListFactory> getExternalizers() {
		return externalizers;
	}

	public void readTaskList(TaskList taskList, File inFile) throws CoreException {
		Map<AbstractTask, NodeList> tasksWithSubtasks = new HashMap<AbstractTask, NodeList>();
		if (!inFile.exists()) {
			throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN,
					"Task list file not found \"" + inFile.getAbsolutePath() + "\""));
		}

		Document doc;
		doc = openAsDOM(inFile);

		if (doc == null) {
			throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, ERROR_TASKLIST_READ));
		}

		Element root = doc.getDocumentElement();
		readVersion = root.getAttribute(ATTRIBUTE_VERSION);

		if (readVersion.equals(VALUE_VERSION_1_0_0)) {
			throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Task list version \""
					+ readVersion + "\" not supported"));
		} else {
			NodeList list = root.getChildNodes();

			// Read Tasks
			for (int i = 0; i < list.getLength(); i++) {
				Node child = list.item(i);
				if (!child.getNodeName().endsWith(DelegatingTaskExternalizer.KEY_CATEGORY)
						&& !child.getNodeName().endsWith(AbstractTaskListFactory.KEY_QUERY)) {

					AbstractTask task = delagatingExternalizer.readTask(child, null, null);
					if (task == null) {
						orphanedTaskNodes.add(child);
					} else {
						taskList.addTask(task);
						if (child.getChildNodes() != null && child.getChildNodes().getLength() > 0) {
							tasksWithSubtasks.put(task, child.getChildNodes());
						}
					}
				}
			}

			for (AbstractTask task : tasksWithSubtasks.keySet()) {
				NodeList nodes = tasksWithSubtasks.get(task);
				delagatingExternalizer.readTaskReferences(task, nodes, taskList);
			}

			// Read Queries
			for (int i = 0; i < list.getLength(); i++) {
				Node child = list.item(i);
				if (child.getNodeName().endsWith(AbstractTaskListFactory.KEY_QUERY)) {
					readQuery(taskList, child);
				}
			}

			// Read Categories
			for (int i = 0; i < list.getLength(); i++) {
				Node child = list.item(i);
				if (child.getNodeName().endsWith(DelegatingTaskExternalizer.KEY_CATEGORY)) {
					delagatingExternalizer.readCategory(child, taskList);
				}
			}

			// Legacy migration for task nodes that have the old Category handle on the element
			if (delagatingExternalizer.getLegacyParentCategoryMap().size() > 0) {
				for (AbstractTask task : delagatingExternalizer.getLegacyParentCategoryMap().keySet()) {
					AbstractTaskCategory category = taskList.getContainerForHandle(delagatingExternalizer.getLegacyParentCategoryMap()
							.get(task));
					if (category != null) {
						taskList.addTask(task, category);
					}
				}
			}
		}
	}

	/**
	 * Opens the specified XML file and parses it into a DOM Document.
	 * 
	 * Filename - the name of the file to open Return - the Document built from the XML file Throws - XMLException if
	 * the file cannot be parsed as XML - IOException if the file cannot be opened
	 * 
	 * @throws CoreException
	 * 
	 */
	private Document openAsDOM(File inputFile) throws CoreException {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		Document document = null;
		try {
			builder = factory.newDocumentBuilder();
			InputStream inputStream = null;
			if (inputFile.getName().endsWith(ITasksCoreConstants.FILE_EXTENSION)) {
				inputStream = new ZipInputStream(new FileInputStream(inputFile));
				ZipEntry entry = ((ZipInputStream) inputStream).getNextEntry();
				while (entry != null) {
					if (ITasksCoreConstants.OLD_TASK_LIST_FILE.equals(entry.getName())) {
						break;
					}
					entry = ((ZipInputStream) inputStream).getNextEntry();
				}
				if (entry == null) {
					return null;
				}
			} else {
				inputStream = new FileInputStream(inputFile);
			}
			document = builder.parse(inputStream);
		} catch (Exception se) {
			throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, ERROR_TASKLIST_READ, se));
		}
		return document;
	}

	// Stream based versions of task list reading
//	public void readTaskList(TaskList taskList, InputStream inFile) throws CoreException {
//	delagatingExternalizer.getLegacyParentCategoryMap().clear();
//	Map<AbstractTask, NodeList> tasksWithSubtasks = new HashMap<AbstractTask, NodeList>();
//	orphanedTaskNodes.clear();
//	orphanedQueryNodes.clear();
//	Document doc = openAsDOM(inFile);
//	Element root = doc.getDocumentElement();
//	readVersion = root.getAttribute(ATTRIBUTE_VERSION);
//
//	if (readVersion.equals(VALUE_VERSION_1_0_0)) {
//		throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Task list version \""
//				+ readVersion + "\" not supported"));
//	} else {
//		NodeList list = root.getChildNodes();
//
//		// Read Tasks
//		for (int i = 0; i < list.getLength(); i++) {
//			Node child = list.item(i);
//			if (!child.getNodeName().endsWith(DelegatingTaskExternalizer.KEY_CATEGORY)
//					&& !child.getNodeName().endsWith(AbstractTaskListFactory.KEY_QUERY)) {
//
//				AbstractTask task = delagatingExternalizer.readTask(child, null, null);
//				if (task == null) {
//					orphanedTaskNodes.add(child);
//				} else {
//					taskList.addTask(task);
//					if (child.getChildNodes() != null && child.getChildNodes().getLength() > 0) {
//						tasksWithSubtasks.put(task, child.getChildNodes());
//					}
//				}
//			}
//		}
//
//		for (AbstractTask task : tasksWithSubtasks.keySet()) {
//			NodeList nodes = tasksWithSubtasks.get(task);
//			delagatingExternalizer.readTaskReferences(task, nodes, taskList);
//		}
//
//		// Read Queries
//		for (int i = 0; i < list.getLength(); i++) {
//			Node child = list.item(i);
//			if (child.getNodeName().endsWith(AbstractTaskListFactory.KEY_QUERY)) {
//				readQuery(taskList, child);
//			}
//		}
//
//		// Read Categories
//		for (int i = 0; i < list.getLength(); i++) {
//			Node child = list.item(i);
//			if (child.getNodeName().endsWith(DelegatingTaskExternalizer.KEY_CATEGORY)) {
//				delagatingExternalizer.readCategory(child, taskList);
//			}
//		}
//
//		// Legacy migration for task nodes that have the old Category handle on the element
//		if (delagatingExternalizer.getLegacyParentCategoryMap().size() > 0) {
//			for (AbstractTask task : delagatingExternalizer.getLegacyParentCategoryMap().keySet()) {
//				AbstractTaskCategory category = taskList.getContainerForHandle(delagatingExternalizer.getLegacyParentCategoryMap()
//						.get(task));
//				if (category != null) {
//					taskList.addTask(task, category);
//				}
//			}
//		}
//	}
//}

///**
// * Parses <code>inputStream</code> into a DOM Document.
// */
//private Document openAsDOM(InputStream inputStream) throws CoreException {
//	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//	DocumentBuilder builder = null;
//	Document document = null;
//	try {
//		builder = factory.newDocumentBuilder();
//		document = builder.parse(inputStream);
//	} catch (Exception e) {
//		throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, ERROR_READING_TASKLIST, e));
//	}
//	return document;
//}
}

Back to the top