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
|
/*******************************************************************************
* Copyright (c) 2004, 2011 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;
import static org.eclipse.mylyn.internal.commons.core.XmlStringConverter.convertXmlToString;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.google.common.base.Strings;
/**
* Adapted from SaxContextContentHandler
*
* @author Rob Elves
*/
public class SaxRepositoriesContentHandler extends DefaultHandler {
static final String ATTRIBUTE_INTERACTION_EVENT = "InteractionEvent"; //$NON-NLS-1$
private final Set<TaskRepository> taskRepositories = new HashSet<TaskRepository>();
private TaskRepository currentRepository;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
try {
if (localName.equals(TaskRepositoriesExternalizer.ELEMENT_TASK_REPOSITORY)) {
handleRepositoryElement(attributes);
} else if (localName.equals(TaskRepositoriesExternalizer.ELEMENT_PROPERTY) && currentRepository != null) {
// properties are stored as attributes on the repository node as well as children property nodes
handleProperty(attributes);
}
} catch (Exception e) {
StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Could not read repositories" //$NON-NLS-1$
, e));
}
}
@Override
public void endElement(String uri, String localName, String qName) {
if (currentRepository != null && localName.equals(TaskRepositoriesExternalizer.ELEMENT_TASK_REPOSITORY)) {
taskRepositories.add(currentRepository);
currentRepository = null;
}
}
@SuppressWarnings({ "deprecation", "restriction" })
private void handleRepositoryElement(Attributes attributes) throws SAXException {
String kind = convertXmlToString(attributes.getValue(IRepositoryConstants.PROPERTY_CONNECTOR_KIND));
String url = convertXmlToString(attributes.getValue(IRepositoryConstants.PROPERTY_URL));
if (!Strings.isNullOrEmpty(kind) && !Strings.isNullOrEmpty(url)) {
currentRepository = new TaskRepository(kind, url);
// properties are stored as attributes on the repository node as well as children property nodes
for (int index = 0; index < attributes.getLength(); index++) {
String key = convertXmlToString(attributes.getLocalName(index));
String value = convertXmlToString(attributes.getValue(index));
currentRepository.setProperty(key, value);
}
}
}
private void handleProperty(Attributes attributes) throws SAXException {
String key = attributes.getValue(TaskRepositoriesExternalizer.PROPERTY_KEY);
String value = attributes.getValue(TaskRepositoriesExternalizer.PROPERTY_VALUE);
if (key != null && value != null) {
currentRepository.setProperty(key, value);
}
}
public Set<TaskRepository> getRepositories() {
return taskRepositories;
}
}
|