EMMA Coverage Report (generated Tue Jun 26 14:54:12 CEST 2012)
[all classes][org.tomdroid.util]

COVERAGE SUMMARY FOR SOURCE FILE [NoteContentBuilder.java]

nameclass, %method, %block, %line, %
NoteContentBuilder.java100% (1/1)100% (6/6)89%  (93/105)83%  (25/30)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NoteContentBuilder100% (1/1)100% (6/6)89%  (93/105)83%  (25/30)
run (): void 100% (1/1)76%  (29/38)69%  (9/13)
warnHandler (boolean): void 100% (1/1)82%  (14/17)83%  (5/6)
NoteContentBuilder (): void 100% (1/1)100% (11/11)100% (3/3)
build (): SpannableStringBuilder 100% (1/1)100% (12/12)100% (3/3)
setCaller (Handler): NoteContentBuilder 100% (1/1)100% (5/5)100% (2/2)
setInputSource (String): NoteContentBuilder 100% (1/1)100% (22/22)100% (3/3)

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 */
23package org.tomdroid.util;
24 
25import java.io.StringReader;
26 
27import javax.xml.parsers.SAXParser;
28import javax.xml.parsers.SAXParserFactory;
29 
30import org.tomdroid.ui.Tomdroid;
31import org.tomdroid.xml.NoteContentHandler;
32import org.xml.sax.InputSource;
33 
34import android.os.Handler;
35import android.os.Message;
36import android.text.SpannableStringBuilder;
37import android.util.Log;
38 
39public 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}

[all classes][org.tomdroid.util]
EMMA 0.0.0 (unsupported private build) (C) Vladimir Roubtsov