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.File;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22
23 import javax.xml.stream.XMLInputFactory;
24 import javax.xml.stream.XMLOutputFactory;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.stream.XMLStreamReader;
27 import javax.xml.stream.XMLStreamWriter;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.eclipse.mylyn.tasks.core.data.TaskData;
32
33
34
35
36
37
38
39 public class XmlTaskDataStore extends AbstractFileTaskDataStore
40 {
41
42
43
44
45
46
47
48 private static final Log LOG = LogFactory.getLog(XmlTaskDataStore.class);
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public XmlTaskDataStore(final File persistentStoreDirectory)
63 {
64 super(persistentStoreDirectory, ".xml");
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 protected TaskData readTask(final InputStream inputStream)
86 throws PersistenceException
87 {
88 final XMLInputFactory factory = XMLInputFactory.newInstance();
89 XMLStreamReader xmlReader = null;
90 try
91 {
92 xmlReader = factory.createXMLStreamReader(inputStream);
93 final XmlTaskReader taskReader = new XmlTaskReader(xmlReader);
94 return taskReader.readTask();
95 }
96 catch (final XMLStreamException e)
97 {
98 throw new PersistenceException("Cannot read task from XML stream.", e);
99 }
100 finally
101 {
102 if (xmlReader != null)
103 {
104 try
105 {
106 xmlReader.close();
107 }
108 catch (final XMLStreamException e)
109 {
110
111 if (LOG.isErrorEnabled())
112 {
113 LOG.error("Cannot close input stream. Continue...", e);
114 }
115 }
116 }
117 }
118 }
119
120
121
122
123
124
125
126
127
128
129 protected void writeTask(final OutputStream outputStream, final TaskData task)
130 throws PersistenceException
131 {
132 final XMLOutputFactory factory = XMLOutputFactory.newInstance();
133 XMLStreamWriter xmlWriter = null;
134 try
135 {
136 xmlWriter = factory.createXMLStreamWriter(outputStream);
137 xmlWriter.writeStartDocument();
138 final XmlTaskWriter taskWriter = new XmlTaskWriter(xmlWriter);
139 taskWriter.writeTask(task);
140 xmlWriter.writeEndDocument();
141 }
142 catch (final XMLStreamException e)
143 {
144 final String message =
145 "Cannot persist task data with ID '" + task.getTaskId() + "'.";
146 LOG.warn(message, e);
147 throw new PersistenceException(message, e);
148 }
149 finally
150 {
151 if (xmlWriter != null)
152 {
153 try
154 {
155 xmlWriter.close();
156 }
157 catch (final XMLStreamException e)
158 {
159
160 if (LOG.isErrorEnabled())
161 {
162 LOG.error("Cannot close output stream. Continue...", e);
163 }
164 }
165 }
166 }
167 }
168
169
170
171 }