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