1 | /* |
2 | * Tomdroid |
3 | * Tomboy on Android |
4 | * http://www.launchpad.net/tomdroid |
5 | * |
6 | * Copyright 2008, 2009, 2010 Olivier Bilodeau <olivier@bottomlesspit.org> |
7 | * Copyright 2009, Benoit Garret <benoit.garret_launchpad@gadz.org> |
8 | * |
9 | * This file is part of Tomdroid. |
10 | * |
11 | * Tomdroid is free software: you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by |
13 | * the Free Software Foundation, either version 3 of the License, or |
14 | * (at your option) any later version. |
15 | * |
16 | * Tomdroid is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU General Public License |
22 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
23 | */ |
24 | package org.tomdroid.sync.sd; |
25 | |
26 | import org.tomdroid.Note; |
27 | import org.xml.sax.Attributes; |
28 | import org.xml.sax.SAXException; |
29 | import org.xml.sax.helpers.DefaultHandler; |
30 | |
31 | import android.util.TimeFormatException; |
32 | |
33 | public class NoteHandler extends DefaultHandler { |
34 | |
35 | // position keepers |
36 | private boolean inTitleTag = false; |
37 | private boolean inLastChangeDateTag = false; |
38 | |
39 | // -- Tomboy's notes XML tags names -- |
40 | // Metadata related |
41 | private final static String TITLE = "title"; |
42 | private final static String LAST_CHANGE_DATE = "last-change-date"; |
43 | |
44 | // Buffers for parsed elements |
45 | private StringBuilder title = new StringBuilder(); |
46 | private StringBuilder lastChangeDate = new StringBuilder(); |
47 | |
48 | // link to model |
49 | private Note note; |
50 | |
51 | public NoteHandler(Note note) { |
52 | this.note = note; |
53 | } |
54 | |
55 | @Override |
56 | public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { |
57 | |
58 | // TODO validate top-level tag for tomboy notes and throw exception if its the wrong version number (maybe offer to parse also?) |
59 | |
60 | if (localName.equals(TITLE)) { |
61 | inTitleTag = true; |
62 | } else if (localName.equals(LAST_CHANGE_DATE)) { |
63 | inLastChangeDateTag = true; |
64 | } |
65 | |
66 | } |
67 | |
68 | @Override |
69 | public void endElement(String uri, String localName, String name) |
70 | throws SAXException, TimeFormatException { |
71 | |
72 | if (localName.equals(TITLE)) { |
73 | inTitleTag = false; |
74 | note.setTitle(title.toString()); |
75 | } else if (localName.equals(LAST_CHANGE_DATE)) { |
76 | inLastChangeDateTag = false; |
77 | note.setLastChangeDate(lastChangeDate.toString()); |
78 | } |
79 | } |
80 | |
81 | @Override |
82 | public void characters(char[] ch, int start, int length) |
83 | throws SAXException { |
84 | |
85 | if (inTitleTag) { |
86 | title.append(ch, start, length); |
87 | } else if (inLastChangeDateTag) { |
88 | lastChangeDate.append(ch, start, length); |
89 | } |
90 | } |
91 | } |