1 // re/mind - Remote Reminder Software
2 // Copyright (C) 2002 Moritz Petersen
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // $Id: XMLSerializer.java,v 1.1.1.1 2002/09/29 17:26:05 powerpete Exp $
19 package de.jface.remind.io;
20
21 import de.jface.remind.core.IllegalUniqueNumberException;
22 import de.jface.remind.core.Note;
23 import de.jface.remind.core.NoteMetaData;
24 import de.jface.remind.core.NoteText;
25
26 import java.awt.Rectangle;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33
34 import java.text.ParseException;
35 import java.text.SimpleDateFormat;
36
37 import java.util.Collection;
38 import java.util.Date;
39 import java.util.Iterator;
40 import java.util.LinkedList;
41
42 import org.apache.log4j.Logger;
43 import org.jdom.Document;
44 import org.jdom.Element;
45 import org.jdom.input.SAXBuilder;
46 import org.jdom.output.XMLOutputter;
47
48 /***
49 * Class <code>XMLSerializer</code>
50 * of project RemotePostIt.
51 *
52 * @author Moritz Petersen
53 * @version $Revision: 1.1.1.1 $
54 */
55 public class XMLSerializer extends Serializer
56 {
57 private static final String ROOT = "notes";
58
59 /***
60 * "note" - the name of the JDOM element, that represents
61 * the Note object.
62 */
63 private static final String NOTE = "note";
64
65 /***
66 * "text" - the name of the JDOM element, that represents
67 * the NoteText object.
68 */
69 private static final String NOTE_TEXT = "text";
70 private static final String NOTE_TEXT_BODY = "body";
71
72 /***
73 * "meta-data" - the name of the JDOM element, that represents the
74 * the NoteMetaData object.
75 */
76 private static final String NOTE_META_DATA = "meta-data";
77 private static final String NOTE_META_DATA_UNIQUE_NUMBER = "unique-number";
78 private static final String NOTE_META_DATA_DATE_OF_CREATION =
79 "date-of-creation";
80 private static final String NOTE_META_DATA_BOUNDS = "bounds";
81 private static final String NOTE_META_DATA_BOUNDS_X = "x";
82 private static final String NOTE_META_DATA_BOUNDS_Y = "y";
83 private static final String NOTE_META_DATA_BOUNDS_WIDTH = "width";
84 private static final String NOTE_META_DATA_BOUNDS_HEIGHT = "height";
85 private static final String DATE_FORMAT_PATTERN = "dd.MM.yyyy HH:mm:ss.SSS";
86 private static final String NOTE_META_DATA_VISIBILITY = "visibility";
87
88 /***
89 * The Logger.
90 */
91 private final Logger log4j = Logger.getLogger(getClass());
92
93 /***
94 * Decodes the data, read from the InputStream into a Collection.
95 * The created Collection only contains elements of type Note.
96 *
97 * @param in the InputStream that is decoded into the Collection of
98 * Note objects.
99 * @return a Collection containing only Note objects.
100 */
101 public Collection deserialize(InputStream in)
102 throws SerializerException
103 {
104 try
105 {
106 log4j.debug("Reading notes");
107
108 Collection notes = new LinkedList();
109
110 Element root = new SAXBuilder().build(in).getRootElement();
111
112 for (Iterator i = root.getChildren("note").iterator(); i.hasNext();)
113 {
114 notes.add(createNote((Element) i.next()));
115 log4j.debug(".");
116 }
117
118 log4j.debug("success.");
119
120 return notes;
121 }
122 catch (Exception e)
123 {
124 log4j.fatal("Reading notes failed.", e);
125 throw new SerializerException(
126 "Reading notes failed. See the log for more details.");
127 }
128 }
129
130 /***
131 * Creates a Note object from the given JDOM element. The element
132 * itself must represent the Note object.
133 *
134 * @param element The JDOM element representing the Note object.
135 * This is usually the element with the name 'note'.
136 *
137 * @return The Note object, created from the given JDOM element.
138 * The note object contains default values for all elements,
139 * that are not defined in the JDOM element for any reason.
140 */
141 private Note createNote(Element element)
142 throws ParseException, IllegalUniqueNumberException
143 {
144 NoteMetaData noteMetaData =
145 createNoteMetaData(element.getChild(NOTE_META_DATA));
146 NoteText noteText = createNoteText(element.getChild(NOTE_TEXT));
147 Note note = new Note(noteMetaData);
148
149 note.setText(noteText);
150
151 return note;
152 }
153
154 private NoteMetaData createNoteMetaData(Element element)
155 throws ParseException, IllegalUniqueNumberException
156 {
157 if (element != null)
158 {
159 long uniqueNumber =
160 Long.parseLong(element.getChildTextTrim(
161 NOTE_META_DATA_UNIQUE_NUMBER));
162 Date dateOfCreation =
163 new SimpleDateFormat(DATE_FORMAT_PATTERN).parse(
164 element.getChildTextTrim(
165 NOTE_META_DATA_DATE_OF_CREATION));
166 NoteMetaData metaData =
167 new NoteMetaData(uniqueNumber, dateOfCreation);
168
169 Element boundsElement = element.getChild(NOTE_META_DATA_BOUNDS);
170
171 if (boundsElement != null)
172 {
173 Rectangle bounds =
174 new Rectangle(Integer.parseInt(
175 boundsElement.getChildTextTrim(
176 NOTE_META_DATA_BOUNDS_X)),
177 Integer.parseInt(
178 boundsElement.getChildTextTrim(
179 NOTE_META_DATA_BOUNDS_Y)),
180 Integer.parseInt(
181 boundsElement.getChildTextTrim(
182 NOTE_META_DATA_BOUNDS_WIDTH)),
183 Integer.parseInt(
184 boundsElement.getChildTextTrim(
185 NOTE_META_DATA_BOUNDS_HEIGHT)));
186
187 metaData.setBounds(bounds);
188 }
189
190 metaData.setVisibility(NoteMetaData.Visibility.getInstance(
191 element.getChildTextTrim(
192 NOTE_META_DATA_VISIBILITY)));
193
194 return metaData;
195 }
196 else
197 {
198 return new NoteMetaData();
199 }
200 }
201
202 private NoteText createNoteText(Element element)
203 {
204 return new NoteText(element.getChildText("body"));
205 }
206
207 /***
208 * Returns a Stream, containing the serialized notes. The stream is an
209 * InputStream, from which the serialized notes can be read.
210 *
211 * @param notes A Collection. Only Note objects are considered in
212 * writing to the InputStream.
213 * @return an InputStream from which the serialized notes can be read.
214 */
215 public InputStream serialize(Collection notes)
216 throws SerializerException
217 {
218 try
219 {
220 Element root = new Element(ROOT);
221
222 for (Iterator i = notes.iterator(); i.hasNext();)
223 {
224 Element note = createElement((Note) i.next());
225
226 root.addContent(note);
227 }
228
229 Document doc = new Document(root);
230
231 return getInputStream(doc);
232 }
233 catch (Exception e)
234 {
235 log4j.fatal("Reading notes failed.", e);
236 throw new SerializerException(
237 "Reading notes failed. See the log for more details.");
238 }
239 }
240
241 private Element createElement(Note note)
242 {
243 Element element = new Element(NOTE);
244
245 element.addContent(createElement(note.getMetaData()));
246 element.addContent(createElement(note.getText()));
247
248 return element;
249 }
250
251 private Element createElement(NoteText noteText)
252 {
253 Element body = new Element(NOTE_TEXT_BODY);
254
255 body.setText(noteText.getBody());
256
257 Element element = new Element(NOTE_TEXT);
258
259 element.addContent(body);
260
261 return element;
262 }
263
264 private Element createElement(NoteMetaData noteMetaData)
265 {
266 Element uniqueNumber = new Element(NOTE_META_DATA_UNIQUE_NUMBER);
267
268 uniqueNumber.setText(String.valueOf(noteMetaData.getUniqueNumber()));
269
270 Element dateOfCreation = new Element(NOTE_META_DATA_DATE_OF_CREATION);
271
272 dateOfCreation.setText(new SimpleDateFormat(DATE_FORMAT_PATTERN).format(
273 noteMetaData.getDateOfCreation()));
274
275 Rectangle bounds = noteMetaData.getBounds();
276 Element boundsXElement = new Element(NOTE_META_DATA_BOUNDS_X);
277
278 boundsXElement.setText(String.valueOf((int) bounds.getX()));
279
280 Element boundsYElement = new Element(NOTE_META_DATA_BOUNDS_Y);
281
282 boundsYElement.setText(String.valueOf((int) bounds.getY()));
283
284 Element boundsWidthElement = new Element(NOTE_META_DATA_BOUNDS_WIDTH);
285
286 boundsWidthElement.setText(String.valueOf((int) bounds.getWidth()));
287
288 Element boundsHeightElement = new Element(NOTE_META_DATA_BOUNDS_HEIGHT);
289
290 boundsHeightElement.setText(String.valueOf((int) bounds.getHeight()));
291
292 Element boundsElement = new Element(NOTE_META_DATA_BOUNDS);
293
294 boundsElement.addContent(boundsXElement);
295 boundsElement.addContent(boundsYElement);
296 boundsElement.addContent(boundsWidthElement);
297 boundsElement.addContent(boundsHeightElement);
298
299 Element visibility = new Element(NOTE_META_DATA_VISIBILITY);
300
301 visibility.setText(noteMetaData.getVisibility().toString());
302
303 Element element = new Element(NOTE_META_DATA);
304
305 element.addContent(uniqueNumber);
306 element.addContent(dateOfCreation);
307 element.addContent(boundsElement);
308 element.addContent(visibility);
309
310 return element;
311 }
312
313 private InputStream getInputStream(Document doc)
314 throws IOException
315 {
316 XMLOutputter outputter = new XMLOutputter();
317 ByteArrayOutputStream out = new ByteArrayOutputStream();
318
319 outputter.output(doc, out);
320
321 return new ByteArrayInputStream(out.toByteArray());
322 }
323 }
324
325 // ------1---------2---------3---------4---------5---------6---------7---------8
This page was automatically generated by Maven