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