Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9845358aa83d63800e3fb31159304ecfbfb936b0 (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
/*******************************************************************************
 * Copyright (c) 2016 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.externalization;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.tasks.core.IAttributeContainer;
import org.eclipse.mylyn.tasks.core.IRepositoryElement;
import org.xml.sax.SAXException;

public abstract class SaxTaskListElementWriter<T extends IRepositoryElement> {

	protected final ContentHandlerWrapper handler;

	private final MultiStatus errors;

	public SaxTaskListElementWriter(ContentHandlerWrapper handler) {
		this.handler = handler;
		this.errors = new MultiStatus(ITasksCoreConstants.ID_PLUGIN, IStatus.OK, null, null);
	}

	public abstract void writeElement(T element) throws SAXException;

	protected void writeAttributes(IAttributeContainer container) throws SAXException {
		Map<String, String> attributes = container.getAttributes();
		for (Map.Entry<String, String> entry : attributes.entrySet()) {
			AttributesWrapper xmlAttributes = new AttributesWrapper();
			xmlAttributes.addAttribute(TaskListExternalizationConstants.KEY_KEY, entry.getKey());

			handler.startElement(TaskListExternalizationConstants.NODE_ATTRIBUTE, xmlAttributes);
			handler.characters(entry.getValue());
			handler.endElement(TaskListExternalizationConstants.NODE_ATTRIBUTE);
		}
	}

	@SuppressWarnings({ "restriction" })
	protected String stripControlCharacters(String text) {
		if (text == null) {
			return ""; //$NON-NLS-1$
		}
		return org.eclipse.mylyn.internal.commons.core.XmlStringConverter.cleanXmlString(text);
	}

	protected String formatExternDate(Date date) {
		if (date == null) {
			return ""; //$NON-NLS-1$
		}
		SimpleDateFormat format = new SimpleDateFormat(TaskListExternalizationConstants.OUT_DATE_FORMAT,
				Locale.ENGLISH);
		return format.format(date);
	}

	protected String formatExternCalendar(Calendar date) {
		if (date == null) {
			return ""; //$NON-NLS-1$
		}
		return formatExternDate(date.getTime());
	}

	protected void addError(IStatus status) {
		errors.add(status);
	}

	public IStatus getErrors() {
		return errors;
	}

}

Back to the top