~plaxx/tomdroid/iteration-3

« back to all changes in this revision

Viewing changes to src/org/tomdroid/util/xml/NoteHandler.java

  • Committer: Olivier Bilodeau
  • Date: 2008-12-02 04:42:57 UTC
  • Revision ID: olivier@bottomlesspit.org-20081202044257-lwwadk931yjd5ahu
Now parsing and showing *bold*, _italic_, -striked-, =highlighted= and monospace in notes!
To achieve that, the Note.noteContent is now a SpannableStringBuilder.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import org.xml.sax.SAXException;
28
28
import org.xml.sax.helpers.DefaultHandler;
29
29
 
 
30
import android.text.Spannable;
 
31
import android.text.SpannableStringBuilder;
 
32
import android.text.style.BackgroundColorSpan;
 
33
import android.text.style.StrikethroughSpan;
 
34
import android.text.style.StyleSpan;
 
35
import android.text.style.TypefaceSpan;
30
36
import android.util.Log;
31
37
 
32
38
/*
41
47
        private boolean inNoteTag = false;
42
48
        private boolean inTextTag = false;
43
49
        private boolean inNoteContentTag = false;
 
50
        private boolean inBoldTag = false;
 
51
        private boolean inItalicTag = false;
 
52
        private boolean inStrikeTag = false;
 
53
        private boolean inHighlighTag = false;
 
54
        private boolean inMonospaceTag = false;
44
55
        
45
56
        // tag names
46
57
        private final static String NOTE_CONTENT = "note-content";
 
58
        private final static String BOLD = "bold";
 
59
        private final static String ITALIC = "italic";
 
60
        private final static String STRIKETHROUGH = "strikethrough";
 
61
        private final static String HIGHLIGHT = "highlight";
 
62
        private final static String MONOSPACE = "monospace";
 
63
        // TODO do these with RelativeSizeSpan maybe?
 
64
        private final static String SMALL = "small";
 
65
        private final static String LARGE = "large";
 
66
        private final static String HUGE = "huge";
 
67
        
47
68
        
48
69
        // accumulate notecontent is this var since it spans multiple xml tags
49
 
        private StringBuilder sb;
 
70
        private SpannableStringBuilder ssb;
50
71
        
51
72
        // link to model 
52
73
        private Note note;
53
74
        
54
75
        public NoteHandler(Note note) {
55
76
                this.note = note;
 
77
                
 
78
                // we will use the SpannableStringBuilder from the note
 
79
                this.ssb = note.getNoteContent();
56
80
        }
57
81
        
58
82
        @Override
64
88
                        // we are under the note-content tag
65
89
                        // we will append all its nested tags so I create a string builder to do that
66
90
                        inNoteContentTag = true;
67
 
                        sb = new StringBuilder();
68
 
                }
 
91
                }
 
92
 
 
93
                // if we are in note-content, keep and convert formatting tags
 
94
                if (inNoteContentTag) {
 
95
                        if (localName.equals(BOLD)) {
 
96
                                inBoldTag = true;
 
97
                        } else if (localName.equals(ITALIC)) {
 
98
                                inItalicTag = true;
 
99
                        } else if (localName.equals(STRIKETHROUGH)) {
 
100
                                inStrikeTag = true;
 
101
                        } else if (localName.equals(HIGHLIGHT)) {
 
102
                                inHighlighTag = true;
 
103
                        } else if (localName.equals(MONOSPACE)) {
 
104
                                inMonospaceTag = true;
 
105
                        }
 
106
                }
 
107
 
69
108
        }
70
109
 
71
110
        @Override
78
117
                        
79
118
                        // note-content is over, we can set the builded note to Note's noteContent
80
119
                        inNoteContentTag = false;
81
 
                        note.setNoteContent(sb.toString());
82
 
                        
83
 
                        // no need of the builder anymore
84
 
                        sb = null;
85
 
                }
 
120
                }
 
121
                
 
122
                // if we are in note-content, keep and convert formatting tags
 
123
                if (inNoteContentTag) {
 
124
                        if (localName.equals(BOLD)) {
 
125
                                inBoldTag = false;
 
126
                        } else if (localName.equals(ITALIC)) {
 
127
                                inItalicTag = false;
 
128
                        } else if (localName.equals(STRIKETHROUGH)) {
 
129
                                inStrikeTag = false;
 
130
                        } else if (localName.equals(HIGHLIGHT)) {
 
131
                                inHighlighTag = false;
 
132
                        } else if (localName.equals(MONOSPACE)) {
 
133
                                inMonospaceTag = false;
 
134
                        }
 
135
                }
 
136
 
86
137
        }
87
138
 
88
139
        @Override
89
140
        public void characters(char[] ch, int start, int length)
90
141
                        throws SAXException {
 
142
                
 
143
                // TODO remove this call to avoid creating unused strings
91
144
                Log.i(this.toString(), "char string: " + new String(ch, start, length));
92
145
 
93
146
                if (inNoteContentTag) {
94
147
                        
95
148
                        // while we are in note-content, append
96
 
                        sb.append(ch, start, length);
 
149
                        ssb.append(new String(ch), start, length);
 
150
                        
 
151
                        // apply style if required
 
152
                        // TODO I haven't tested nested tags yet
 
153
                        if (inBoldTag) {
 
154
                                ssb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
155
                        }
 
156
                        if (inItalicTag) {
 
157
                                ssb.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
158
                        }
 
159
                        if (inStrikeTag) {
 
160
                                ssb.setSpan(new StrikethroughSpan(), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
161
                        }
 
162
                        if (inHighlighTag) {
 
163
                                ssb.setSpan(new BackgroundColorSpan(Note.NOTE_HIGHLIGHT_COLOR), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
164
                        }
 
165
                        if (inMonospaceTag) {
 
166
                                ssb.setSpan(new TypefaceSpan(Note.NOTE_MONOSPACE_TYPEFACE), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
167
                        }
97
168
                }
98
169
        }
99
170