| 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 | * |
| 8 | * This file is part of Tomdroid. |
| 9 | * |
| 10 | * Tomdroid is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * Tomdroid is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | package org.tomdroid.util; |
| 24 | |
| 25 | import java.io.StringReader; |
| 26 | |
| 27 | import javax.xml.parsers.SAXParser; |
| 28 | import javax.xml.parsers.SAXParserFactory; |
| 29 | |
| 30 | import org.tomdroid.ui.Tomdroid; |
| 31 | import org.tomdroid.xml.NoteContentHandler; |
| 32 | import org.xml.sax.InputSource; |
| 33 | |
| 34 | import android.os.Handler; |
| 35 | import android.os.Message; |
| 36 | import android.text.SpannableStringBuilder; |
| 37 | import android.util.Log; |
| 38 | |
| 39 | public class NoteContentBuilder implements Runnable { |
| 40 | |
| 41 | public static final int PARSE_OK = 0; |
| 42 | public static final int PARSE_ERROR = 1; |
| 43 | |
| 44 | private InputSource noteContentIs; |
| 45 | |
| 46 | // this is what we are building here |
| 47 | private SpannableStringBuilder noteContent = new SpannableStringBuilder(); |
| 48 | |
| 49 | private final String TAG = "NoteContentBuilder"; |
| 50 | |
| 51 | // thread related |
| 52 | private Thread runner; |
| 53 | private Handler parentHandler; |
| 54 | |
| 55 | public NoteContentBuilder () {} |
| 56 | |
| 57 | public NoteContentBuilder setCaller(Handler parent) { |
| 58 | |
| 59 | parentHandler = parent; |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | public NoteContentBuilder setInputSource(String nc) { |
| 64 | |
| 65 | String noteContent = "<note-content>"+nc+"</note-content>"; |
| 66 | noteContentIs = new InputSource(new StringReader(noteContent)); |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | public SpannableStringBuilder build() { |
| 71 | |
| 72 | runner = new Thread(this); |
| 73 | runner.start(); |
| 74 | return noteContent; |
| 75 | } |
| 76 | |
| 77 | public void run() { |
| 78 | |
| 79 | boolean successful = true; |
| 80 | |
| 81 | try { |
| 82 | // Parsing |
| 83 | // XML |
| 84 | // Get a SAXParser from the SAXPArserFactory |
| 85 | SAXParserFactory spf = SAXParserFactory.newInstance(); |
| 86 | |
| 87 | // trashing the namespaces but keep prefixes (since we don't have the xml header) |
| 88 | spf.setFeature("http://xml.org/sax/features/namespaces", false); |
| 89 | spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true); |
| 90 | SAXParser sp = spf.newSAXParser(); |
| 91 | |
| 92 | if (Tomdroid.LOGGING_ENABLED) Log.v(TAG, "parsing note"); |
| 93 | sp.parse(noteContentIs, new NoteContentHandler(noteContent)); |
| 94 | } catch (Exception e) { |
| 95 | e.printStackTrace(); |
| 96 | // TODO handle error in a more granular way |
| 97 | Log.e(TAG, "There was an error parsing the note."); |
| 98 | successful = false; |
| 99 | } |
| 100 | |
| 101 | warnHandler(successful); |
| 102 | } |
| 103 | |
| 104 | private void warnHandler(boolean successful) { |
| 105 | |
| 106 | // notify the main UI that we are done here (sending an ok along with the note's title) |
| 107 | Message msg = Message.obtain(); |
| 108 | if (successful) { |
| 109 | msg.what = PARSE_OK; |
| 110 | } else { |
| 111 | |
| 112 | msg.what = PARSE_ERROR; |
| 113 | } |
| 114 | |
| 115 | parentHandler.sendMessage(msg); |
| 116 | } |
| 117 | } |