1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package de.smartics.maven.issues.cache;
18
19 import java.io.Closeable;
20 import java.util.Map;
21
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.stream.XMLStreamWriter;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
28 import org.eclipse.mylyn.tasks.core.data.TaskAttributeMetaData;
29 import org.eclipse.mylyn.tasks.core.data.TaskData;
30
31
32
33
34
35
36
37 public class XmlTaskWriter implements TaskWriter, Closeable
38 {
39
40
41
42
43
44
45
46
47
48 private final Log log = LogFactory.getLog(XmlTaskWriter.class);
49
50
51
52
53 protected final XMLStreamWriter xmlWriter;
54
55
56
57
58
59
60
61
62
63
64
65
66 public XmlTaskWriter(final XMLStreamWriter xmlWriter)
67 throws NullPointerException
68 {
69 if (xmlWriter == null)
70 {
71 throw new NullPointerException("XML writer must not be 'null'.");
72 }
73
74 this.xmlWriter = xmlWriter;
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public void writeTask(final TaskData task) throws XMLStreamException
93 {
94 xmlWriter.writeStartElement(TaskInfoKey.TASK_DATA);
95 writeValueAsAttributeIfNotNull(TaskInfoKey.TASK_ID, task.getTaskId());
96 writeValueAsAttributeIfNotNull(TaskInfoKey.CONNECTOR_KIND,
97 task.getConnectorKind());
98 writeValueAsAttributeIfNotNull(TaskInfoKey.VERSION, task.getVersion());
99 writeValueAsAttributeIfNotNull(TaskInfoKey.REPOSITORY_URL,
100 task.getRepositoryUrl());
101 writeAttribute(task.getRoot());
102 xmlWriter.writeEndElement();
103 }
104
105
106
107
108
109
110
111
112
113 private void writeValueAsAttributeIfNotNull(final String name,
114 final String value) throws XMLStreamException
115 {
116 if (value != null)
117 {
118 xmlWriter.writeAttribute(name, value);
119 }
120 }
121
122
123
124
125
126
127 public void writeAttribute(final TaskAttribute attribute)
128 throws XMLStreamException
129 {
130 if (attribute != null)
131 {
132 xmlWriter.writeStartElement(TaskInfoKey.ATTRIBUTE);
133 xmlWriter.writeAttribute(TaskInfoKey.ATTRIBUTE_ID, attribute.getId());
134
135 writeAttributeValues(attribute);
136 writeAttributes(attribute.getAttributes());
137 final TaskAttributeMetaData metaData = attribute.getMetaData();
138 if (metaData != null)
139 {
140 writeMap(TaskInfoKey.META_DATA, metaData.getValues());
141 }
142 writeMap(TaskInfoKey.OPTIONS, attribute.getOptions());
143
144 xmlWriter.writeEndElement();
145 }
146 }
147
148
149
150
151
152
153 public void writeAttributes(final Map<String, TaskAttribute> attributes)
154 throws XMLStreamException
155 {
156 if (attributes != null && !attributes.values().isEmpty())
157 {
158 xmlWriter.writeStartElement(TaskInfoKey.ATTRIBUTES);
159 for (TaskAttribute attribute : attributes.values())
160 {
161 writeAttribute(attribute);
162 }
163 xmlWriter.writeEndElement();
164 }
165 }
166
167
168
169
170
171
172 public void writeAttributeValues(final TaskAttribute attribute)
173 throws XMLStreamException
174 {
175 if (attribute != null && !attribute.getValues().isEmpty())
176 {
177 xmlWriter.writeStartElement(TaskInfoKey.VALUES);
178 for (String value : attribute.getValues())
179 {
180 writeSimpleElement(TaskInfoKey.VALUE, value);
181 }
182 xmlWriter.writeEndElement();
183 }
184 }
185
186
187
188
189
190
191
192 public void writeMap(final String gi, final Map<String, String> map)
193 throws XMLStreamException
194 {
195 if (map != null && !map.isEmpty())
196 {
197 xmlWriter.writeStartElement(gi);
198 for (Map.Entry<String, String> entry : map.entrySet())
199 {
200 xmlWriter.writeStartElement(TaskInfoKey.ENTRY);
201 writeSimpleElement(TaskInfoKey.KEY, entry.getKey());
202 writeSimpleElement(TaskInfoKey.VALUE, entry.getValue());
203 xmlWriter.writeEndElement();
204 }
205 xmlWriter.writeEndElement();
206 }
207 }
208
209
210
211
212
213
214
215 public void writeSimpleElement(final String gi, final String content)
216 throws XMLStreamException
217 {
218 xmlWriter.writeStartElement(gi);
219 xmlWriter.writeCharacters(content);
220 xmlWriter.writeEndElement();
221 }
222
223
224
225
226
227
228
229
230
231
232 public void close()
233 {
234 try
235 {
236 this.xmlWriter.close();
237 }
238 catch (final XMLStreamException e)
239 {
240 if (log.isErrorEnabled())
241 {
242 log.error("Cannot close XML stream of task writer.");
243 }
244 }
245 }
246
247
248
249 }