27
27
import org.xml.sax.SAXException;
28
28
import org.xml.sax.helpers.DefaultHandler;
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;
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;
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";
48
69
// accumulate notecontent is this var since it spans multiple xml tags
49
private StringBuilder sb;
70
private SpannableStringBuilder ssb;
54
75
public NoteHandler(Note note) {
78
// we will use the SpannableStringBuilder from the note
79
this.ssb = note.getNoteContent();
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();
93
// if we are in note-content, keep and convert formatting tags
94
if (inNoteContentTag) {
95
if (localName.equals(BOLD)) {
97
} else if (localName.equals(ITALIC)) {
99
} else if (localName.equals(STRIKETHROUGH)) {
101
} else if (localName.equals(HIGHLIGHT)) {
102
inHighlighTag = true;
103
} else if (localName.equals(MONOSPACE)) {
104
inMonospaceTag = true;
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());
83
// no need of the builder anymore
122
// if we are in note-content, keep and convert formatting tags
123
if (inNoteContentTag) {
124
if (localName.equals(BOLD)) {
126
} else if (localName.equals(ITALIC)) {
128
} else if (localName.equals(STRIKETHROUGH)) {
130
} else if (localName.equals(HIGHLIGHT)) {
131
inHighlighTag = false;
132
} else if (localName.equals(MONOSPACE)) {
133
inMonospaceTag = false;
89
140
public void characters(char[] ch, int start, int length)
90
141
throws SAXException {
143
// TODO remove this call to avoid creating unused strings
91
144
Log.i(this.toString(), "char string: " + new String(ch, start, length));
93
146
if (inNoteContentTag) {
95
148
// while we are in note-content, append
96
sb.append(ch, start, length);
149
ssb.append(new String(ch), start, length);
151
// apply style if required
152
// TODO I haven't tested nested tags yet
154
ssb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
157
ssb.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
160
ssb.setSpan(new StrikethroughSpan(), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
163
ssb.setSpan(new BackgroundColorSpan(Note.NOTE_HIGHLIGHT_COLOR), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
165
if (inMonospaceTag) {
166
ssb.setSpan(new TypefaceSpan(Note.NOTE_MONOSPACE_TYPEFACE), ssb.length()-length, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);