Skip to main content
summaryrefslogtreecommitdiffstats
blob: 82485ca5bef7c8cb2650839af52d57e2a6a5271a (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
/*******************************************************************************
 * Copyright (c) 2004, 2013 Tasktop Technologies 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
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.core.data;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.core.data.TaskDataExternalizer.Xml11InputStream;
import org.eclipse.mylyn.tasks.core.IRepositoryManager;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.xml.sax.SAXException;

/**
 * @author Steffen Pingel
 */
public class TaskDataStore {

	private static final String FILE_NAME_INTERNAL = "data.xml"; //$NON-NLS-1$

	private final TaskDataExternalizer externalizer;

	public TaskDataStore(IRepositoryManager taskRepositoryManager) {
		this.externalizer = new TaskDataExternalizer(taskRepositoryManager);
	}

	public synchronized TaskDataState discardEdits(File file) throws CoreException {
		TaskDataState state = readState(file);
		if (state != null) {
			state.setEditsData(null);
		}
		writeState(file, state);
		return state;
	}

	public synchronized TaskDataState getTaskDataState(File file) throws CoreException {
		return readState(file);
	}

	public synchronized void putEdits(File file, TaskData data) throws CoreException {
		Assert.isNotNull(file);
		Assert.isNotNull(data);
		TaskDataState state = readState(file);
		if (state == null) {
			state = new TaskDataState(data.getConnectorKind(), data.getRepositoryUrl(), data.getTaskId());
		}
		state.setEditsData(data);
		writeState(file, state);
	}

	public synchronized TaskDataState putTaskData(File file, TaskData data, boolean setLastRead, boolean user)
			throws CoreException {
		Assert.isNotNull(file);
		Assert.isNotNull(data);
		TaskDataState state = null;
		try {
			state = readState(file);
		} catch (CoreException e) {
			if (!user) {
				throw new CoreException(
						new Status(
								IStatus.ERROR,
								ITasksCoreConstants.ID_PLUGIN,
								"Reading of existing task data failed. Forcing synchronization will override outgoing changes.", //$NON-NLS-1$
								e));
			}
		}
		if (state == null) {
			state = new TaskDataState(data.getConnectorKind(), data.getRepositoryUrl(), data.getTaskId());
		}
		if (setLastRead) {
			state.setLastReadData(state.getRepositoryData());
		}
		state.setRepositoryData(data);
		writeState(file, state);
		return state;
	}

	public synchronized TaskDataState setTaskData(File file, TaskData data) throws CoreException {
		Assert.isNotNull(file);
		Assert.isNotNull(data);

		// TODO consider reading old task data and compare submitted results to check if all outgoing changes were accepted by repository

		TaskDataState state = new TaskDataState(data.getConnectorKind(), data.getRepositoryUrl(), data.getTaskId());
		state.setRepositoryData(data);
		state.setEditsData(null);
		state.setLastReadData(data);
		writeState(file, state);
		return state;
	}

	private TaskDataState readStateInternal(File file, boolean xml11) throws IOException, SAXException {
		try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)))) {
			in.getNextEntry();
			// bug 268456: When TaskData that contains C0 control characters is written to disk using XML 1.0 reading it back
			// in fails with a SAXException. The XML 1.1 standard allows C0 entities but fails if C1 entities. If C0 control
			// characters are detected while parsing file as XML 1.0 a second attempt is made using XML 1.1. If the file contains
			// C0 and C1 control characters reading will fail regardless.
			if (xml11) {
				return externalizer.readState(new Xml11InputStream(in));
			} else {
				return externalizer.readState(in);
			}
		}
	}

	private TaskDataState readState(File file) throws CoreException {
		try {
			if (file.exists()) {
				try {
					try {
						return readStateInternal(file, false);
					} catch (SAXException e) {
						// bug 268456: if reading fails, try again using a different XML version
						if (e.getMessage() != null
								&& (e.getMessage().contains("invalid XML character") || e.getMessage().contains(" \"&#"))) { //$NON-NLS-1$ //$NON-NLS-2$
							return readStateInternal(file, true);
						} else {
							throw e;
						}
					}

				} catch (SAXException e) {
					throw new IOException("Error parsing task data: " + e.getMessage()); //$NON-NLS-1$
				}
			}
			return null;
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Error reading task data", //$NON-NLS-1$
					e));
		}
	}

	private void writeState(File file, TaskDataState state) throws CoreException {
		try {
			try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
				out.setMethod(ZipOutputStream.DEFLATED);

				ZipEntry entry = new ZipEntry(FILE_NAME_INTERNAL);
				out.putNextEntry(entry);

				externalizer.writeState(out, state);
			}
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Error writing task data", //$NON-NLS-1$
					e));
		}
	}

//	public synchronized void putLastRead(File file, TaskData data) throws CoreException {
//		Assert.isNotNull(file);
//		Assert.isNotNull(data);
//
//		TaskDataState state = readState(file);
//		if (state == null) {
//			state = new TaskDataState(data.getConnectorKind(), data.getRepositoryUrl(), data.getTaskId());
//		}
//		state.setLastReadData(data);
//		writeState(file, state);
//	}

	public synchronized void putTaskData(File file, TaskDataState state) throws CoreException {
		writeState(file, state);
	}

	public synchronized boolean deleteTaskData(File file) {
		return file.delete();
	}

}

Back to the top