Merge lp:~vbkaisetsu/tomdroid/sql-args into lp:~tomdroid-maintainers/tomdroid/main

Proposed by Koichi Akabe
Status: Superseded
Proposed branch: lp:~vbkaisetsu/tomdroid/sql-args
Merge into: lp:~tomdroid-maintainers/tomdroid/main
Diff against target: 440 lines (+190/-33)
5 files modified
src/org/tomdroid/Note.java (+7/-1)
src/org/tomdroid/NoteManager.java (+42/-14)
src/org/tomdroid/NoteProvider.java (+73/-18)
src/org/tomdroid/sync/sd/NoteHandler.java (+17/-0)
src/org/tomdroid/util/StringConverter.java (+51/-0)
To merge this branch: bzr merge lp:~vbkaisetsu/tomdroid/sql-args
Reviewer Review Type Date Requested Status
Olivier Bilodeau Needs Fixing
Review via email: mp+80555@code.launchpad.net

This proposal has been superseded by a proposal from 2012-09-08.

Description of the change

This branch fixes following things:
    - Bug #562089: the search-bar search also XML tags Remove
    - Bug #880322: SQL query should use "?" and selectionArgs Remove
    - The list always shows system:template notes
    - Can't search words which contains "%" or "_"

New features:
    - Added notebook argument to NoteManager.getListAdapter() function

Please see it.

Regards,
Koichi Akabe

To post a comment you must log in.
Revision history for this message
Piotr Adamski (mcveat) wrote :

Please consider using StringBuilder for constructing where clause and wrapping it in separate function to increase readability and divide responsibilities a bit.

lp:~vbkaisetsu/tomdroid/sql-args updated
256. By Koichi Akabe

changed to use StringBuilder

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

Hello Piotr,

Thanks your opinion.
I changed the code to use StringBuilder.

Regards,
Koichi Akabe.

Revision history for this message
Piotr Adamski (mcveat) wrote :

Great, Koichi! But your fix needs one more change in my opinion. The point of using StringBuilder is to avoid explicit string concatenation, as each produces new String in strings pool. It may strike back as memory issue, so it is worth to avoid them in cases like this, where there is a lot string operations. Consider replacing each concatenation with append method call. I know it doesn't look so concise, but in that situation i think it is worth it.

Regards
Piotr

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

I think constants' normal operation is faster than using StringBuffer.
Because they are calculated in compiling.

Note.XXXXXs are defined with final, so they are constant.

lp:~vbkaisetsu/tomdroid/sql-args updated
257. By Koichi Akabe

added NOTE_CONTENT_PLAIN to restore plain text contents
changed to search NOTE_CONTENT_PLAIN

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

The commit message is wrong: s/restore/store/

Sorry.

lp:~vbkaisetsu/tomdroid/sql-args updated
258. By Koichi Akabe

added some html escape codes

259. By Koichi Akabe

changed to use android.text.Html

260. By Koichi Akabe

changed SQL query to ignore templates

Revision history for this message
Stefan Hammer (j-4-deactivatedaccount) wrote :

Hi! The bug where searching for tags shows all notes is finally gone - great!
But somehow, the test notebook templates still show up in the notes list and in the search on my AVDevices. (even after complete reinstall.)
Do you have any idea?

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

In my environment, templates have "system:template" tag and they are not shown on the list.
Please debug it and tell me more information.

Revision history for this message
Stefan Hammer (j-4-deactivatedaccount) wrote :

Hi! I use Android 1.6 AVD, the sdcard image (sdcard.img) from our sources and exactly your code branch, with additional log entries. I still have both two test notebook templates in the list.

In this templates the tags look like:
  <tags>

    <tag>system:template</tag>

    <tag>system:notebook:test</tag>

  </tags>

  <tags>
    <tag>system:notebook:Hipo</tag>
    <tag>system:template</tag>
  </tags>

My log gives:
10-30 11:04:27.107: I/Tomdroid(486): Tomdroid is first run.
10-30 11:04:27.227: V/FirstNote(486): Creating first note
10-30 11:04:27.537: V/NoteManager(486): A new note has been detected (not yet in db)
10-30 11:04:27.547: V/NoteManager(486): Note inserted in content provider. ID: content://org.tomdroid.notes/notes/1 TITLE:Tomdroid's first note GUID:8f837a99-c920-4501-b303-6a39af57a714
10-30 11:04:27.626: V/NoteManager(486): where is: tags NOT LIKE '%system:template%'
10-30 11:04:27.626: V/NoteManager(486): TAGS is: tags
10-30 11:04:52.287: V/FirstNote(486): Creating first note
10-30 11:04:52.556: V/NoteManager(486): A new note has been detected (not yet in db)
10-30 11:04:52.566: V/NoteManager(486): Note inserted in content provider. ID: content://org.tomdroid.notes/notes/1 TITLE:Tomdroid's first note GUID:8f837a99-c920-4501-b303-6a39af57a714
10-30 11:04:55.497: V/SdCardSyncService(486): Loading local notes
10-30 11:04:55.497: I/SdCardSyncService(486): Path /sdcard/tomdroid exists: true

It works when syncing the AVD with Ubuntu One, using my own notes. One difference is, that there i do not use notebooks.
So, the tags look like:

  <tags>
    <tag>system:template</tag>
  </tags>

10-30 11:34:44.367: V/FirstNote(652): Creating first note
10-30 11:34:44.786: V/NoteManager(652): A new note has been detected (not yet in db)
10-30 11:34:44.797: V/NoteManager(652): Note inserted in content provider. ID: content://org.tomdroid.notes/notes/1 TITLE:Tomdroid's first note GUID:8f837a99-c920-4501-b303-6a39af57a714
10-30 11:35:30.927: V/NoteManager(652): where is: tags NOT LIKE '%system:template%'
10-30 11:35:30.927: V/NoteManager(652): query is: null

Does this help?

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

Notes list always shows templates on SD card, because Tomdroid doesn't check "tags" tag.

I registered another branch to solve it.
https://code.launchpad.net/~vbkaisetsu/tomdroid/sdcard-tags

Thanks for reporting!

lp:~vbkaisetsu/tomdroid/sql-args updated
261. By Koichi Akabe

marged sdcard-tags branch

Revision history for this message
Stefan Hammer (j-4-deactivatedaccount) wrote :

Thanks for your work. I already know the code very well and I think it's awesome. This is something we definitively have to include in the next release.
The new sql query system is much simpler/smarter and that notebook templates are in the database, but not shown - just as it should be ;-)

Problem is... I have never merged any important code and I am not sure, what Olivier's reviewing checklist/merging criteria contains. Therefore I suggest to wait for his opinion. If he is fine with it, I will merge immediately.

There was a idea coming up, when testing this stuff: We now store all tags, including the "system:notebook:****" tag. If you include Note.TAGS in the search query, it would be very easy to display all notes of one notebooks, just with the query "notebook:mynotebookname". Of course, we would have to think about a way to exclude queries like notebook, system or template from searching in Note.TAGS... But hey, intelligent searching! Further... how about title:mynotetitle?? :-D ... but this would be something for a new project, if you feel like it!

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

Hello Stefan,

It's wonderful but there are many problems for notebooks.
We have to register another branch to solve them.

lp:~vbkaisetsu/tomdroid/sql-args updated
262. By Koichi Akabe

added notebook support

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

I added notebook support to getListAdapter(), and you can search "%" and "_" now.
This code encodes some symbols of notes before store them.

I think this code is dirty. Please review it.

lp:~vbkaisetsu/tomdroid/sql-args updated
263. By Koichi Akabe

fixed wrong sql

Revision history for this message
Olivier Bilodeau (plaxx) wrote :

Doesn't automatically upgrade the SQL database. This can't be merged as-is.

Also, I would like to see the query arguments to be migrated into ? and selectionArgs as query() supports. docs/reference/android/database/sqlite/SQLiteDatabase.html#query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String)

review: Needs Fixing
lp:~vbkaisetsu/tomdroid/sql-args updated
264. By Koichi Akabe

merge with trunk to solve conflicts

265. By Koichi Akabe

change database version to 4 and support to update database from ver.2 or later

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

changed onUpgrade to support update from old versions 2 or 3. But version 1 dosn't have GUID and note contents (title only) so it doesn't support.

Revision history for this message
Olivier Bilodeau (plaxx) wrote :

w/o looking at the code, I would say not upgrading from 1 is ok.

What I'm really concerned about is the 0.4.1 users on the android market. I want them to upgrade cleanly. Pretty sure this fixes it.

Revision history for this message
Stefan Hammer (j-4-deactivatedaccount) wrote :

Hi!

Since our new beta, a lot of things changed. However, we still need to fix parts of the things treated in this branch.
    - Bug #562089: the search-bar search also XML tags Remove
    - Bug #880322: SQL query should use "?" and selectionArgs Remove
    - Can't search words which contains "%" or "_"

All the other features are already implemented (i think)
As I assume that this branch won't merge with the new beta it is probably best to rewrite these things too (or copy/paste the neccesary stuff into the new beta code).

@Koichi: do you want to fix this three issues in a new branch, or do you prefere if somebody else does it?

Revision history for this message
Koichi Akabe (vbkaisetsu) wrote :

I hope to fix this branch, but the other hand, it's hard work.
I'll appreciate someone's help.

Unmerged revisions

265. By Koichi Akabe

change database version to 4 and support to update database from ver.2 or later

264. By Koichi Akabe

merge with trunk to solve conflicts

263. By Koichi Akabe

fixed wrong sql

262. By Koichi Akabe

added notebook support

261. By Koichi Akabe

marged sdcard-tags branch

260. By Koichi Akabe

changed SQL query to ignore templates

259. By Koichi Akabe

changed to use android.text.Html

258. By Koichi Akabe

added some html escape codes

257. By Koichi Akabe

added NOTE_CONTENT_PLAIN to restore plain text contents
changed to search NOTE_CONTENT_PLAIN

256. By Koichi Akabe

changed to use StringBuilder

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/org/tomdroid/Note.java'
2--- src/org/tomdroid/Note.java 2011-10-27 16:33:12 +0000
3+++ src/org/tomdroid/Note.java 2012-04-01 04:35:23 +0000
4@@ -30,6 +30,7 @@
5 import org.json.JSONArray;
6 import org.json.JSONObject;
7 import org.tomdroid.util.NoteContentBuilder;
8+import org.tomdroid.util.StringConverter;
9 import org.tomdroid.util.TLog;
10 import org.tomdroid.util.XmlUtils;
11
12@@ -47,6 +48,7 @@
13 public static final String FILE = "file";
14 public static final String TAGS = "tags";
15 public static final String NOTE_CONTENT = "content";
16+ public static final String NOTE_CONTENT_PLAIN = "content_plain";
17
18 // Logging info
19 private static final String TAG = "Note";
20@@ -95,7 +97,7 @@
21 tags = new String();
22 if (jtags != null) {
23 for (int i = 0; i < jtags.length(); i++ ) {
24- tag = jtags.optString(i);
25+ tag = StringConverter.encode(jtags.optString(i));
26 tags += tag + ",";
27 }
28 }
29@@ -104,6 +106,10 @@
30 public String getTags() {
31 return tags;
32 }
33+
34+ public void setTags(String tags) {
35+ this.tags = tags;
36+ }
37
38 public String getUrl() {
39 return url;
40
41=== modified file 'src/org/tomdroid/NoteManager.java'
42--- src/org/tomdroid/NoteManager.java 2011-10-27 16:33:12 +0000
43+++ src/org/tomdroid/NoteManager.java 2012-04-01 04:35:23 +0000
44@@ -24,11 +24,22 @@
45 */
46 package org.tomdroid;
47
48+import java.util.ArrayList;
49+import java.util.regex.Matcher;
50+import java.util.regex.Pattern;
51+
52+import org.tomdroid.ui.Tomdroid;
53+import org.tomdroid.util.NoteListCursorAdapter;
54+import org.tomdroid.util.StringConverter;
55+import org.tomdroid.util.XmlUtils;
56+
57 import android.app.Activity;
58 import android.content.ContentResolver;
59 import android.content.ContentValues;
60 import android.database.Cursor;
61 import android.net.Uri;
62+import android.text.Html;
63+import android.util.Log;
64 import android.widget.ListAdapter;
65 import org.tomdroid.ui.Tomdroid;
66 import org.tomdroid.util.NoteListCursorAdapter;
67@@ -92,6 +103,9 @@
68 null);
69 activity.startManagingCursor(managedCursor);
70
71+ String xmlContent = note.getXmlContent();
72+ String plainContent = StringConverter.encode(Html.fromHtml(xmlContent).toString());
73+
74 // Preparing the values to be either inserted or updated
75 // depending on the result of the previous query
76 ContentValues values = new ContentValues();
77@@ -100,7 +114,8 @@
78 values.put(Note.GUID, note.getGuid().toString());
79 // Notice that we store the date in UTC because sqlite doesn't handle RFC3339 timezone information
80 values.put(Note.MODIFIED_DATE, note.getLastChangeDate().format3339(false));
81- values.put(Note.NOTE_CONTENT, note.getXmlContent());
82+ values.put(Note.NOTE_CONTENT, xmlContent);
83+ values.put(Note.NOTE_CONTENT_PLAIN, plainContent);
84 values.put(Note.TAGS, note.getTags());
85
86 if (managedCursor.getCount() == 0) {
87@@ -148,26 +163,33 @@
88 }
89
90
91- public static ListAdapter getListAdapter(Activity activity, String querys) {
92-
93- String where;
94- if (querys==null) {
95- where=null;
96- } else {
97+ public static ListAdapter getListAdapter(Activity activity, String querys, String notebook) {
98+
99+ StringBuilder where = new StringBuilder();
100+ ArrayList<String> qargs = new ArrayList<String>();
101+
102+ where.append(Note.TAGS + " NOT LIKE 'system:template%' AND " + Note.TAGS + " NOT LIKE '%,system:template%'");
103+
104+ if(notebook != null) {
105+ // sql statements to specify notebook
106+ String encodedNotebook = StringConverter.encodeForTag(notebook);
107+ where.append(" AND ("+Note.TAGS+" LIKE ? OR "+Note.TAGS+" LIKE ?)");
108+ qargs.add("%,system:notebook:"+encodedNotebook+",%");
109+ qargs.add("system:notebook:"+encodedNotebook+",%");
110+ }
111+
112+ if (querys!=null) {
113 // sql statements to search notes
114 String[] query = querys.split(" ");
115- where="";
116- int count=0;
117 for (String string : query) {
118- if (count>0) where = where + " AND ";
119- where = where + "("+Note.TITLE+" LIKE '%"+string+"%' OR "+Note.NOTE_CONTENT+" LIKE '%"+string+"%')";
120- count++;
121+ qargs.add("%"+StringConverter.encode(string)+"%");
122+ where.append(" AND "+Note.TITLE+" || "+Note.NOTE_CONTENT_PLAIN+" LIKE ?");
123 }
124 }
125
126 // get a cursor representing all notes from the NoteProvider
127 Uri notes = Tomdroid.CONTENT_URI;
128- Cursor notesCursor = activity.managedQuery(notes, LIST_PROJECTION, where, null, null);
129+ Cursor notesCursor = activity.managedQuery(notes, LIST_PROJECTION, where.toString(), qargs.toArray(new String[qargs.size()]), null);
130
131 // set up an adapter binding the TITLE field of the cursor to the list item
132 String[] from = new String[] { Note.TITLE };
133@@ -175,9 +197,14 @@
134 return new NoteListCursorAdapter(activity, R.layout.main_list_item, notesCursor, from, to);
135 }
136
137+ public static ListAdapter getListAdapter(Activity activity, String querys) {
138+
139+ return getListAdapter(activity, querys, null);
140+ }
141+
142 public static ListAdapter getListAdapter(Activity activity) {
143
144- return getListAdapter(activity, null);
145+ return getListAdapter(activity, null, null);
146 }
147
148 // gets the titles of the notes present in the db, used in ViewNote.buildLinkifyPattern()
149@@ -236,3 +263,4 @@
150 return xmlContent;
151 }
152 }
153+
154
155=== modified file 'src/org/tomdroid/NoteProvider.java'
156--- src/org/tomdroid/NoteProvider.java 2011-10-27 16:33:12 +0000
157+++ src/org/tomdroid/NoteProvider.java 2012-04-01 04:35:23 +0000
158@@ -51,22 +51,26 @@
159 import android.database.sqlite.SQLiteOpenHelper;
160 import android.database.sqlite.SQLiteQueryBuilder;
161 import android.net.Uri;
162+import android.text.Html;
163 import android.text.TextUtils;
164 import org.tomdroid.ui.Tomdroid;
165+import org.tomdroid.util.StringConverter;
166 import org.tomdroid.util.TLog;
167
168+import java.util.ArrayList;
169 import java.util.HashMap;
170+import java.util.Map;
171 import java.util.UUID;
172
173 public class NoteProvider extends ContentProvider {
174-
175- // ContentProvider stuff
176- // --
177- private static final String DATABASE_NAME = "tomdroid-notes.db";
178- private static final String DB_TABLE_NOTES = "notes";
179- private static final int DB_VERSION = 3;
180- private static final String DEFAULT_SORT_ORDER = Note.MODIFIED_DATE + " DESC";
181-
182+
183+ // ContentProvider stuff
184+ // --
185+ private static final String DATABASE_NAME = "tomdroid-notes.db";
186+ private static final String DB_TABLE_NOTES = "notes";
187+ private static final int DB_VERSION = 4;
188+ private static final String DEFAULT_SORT_ORDER = Note.MODIFIED_DATE + " DESC";
189+
190 private static HashMap<String, String> notesProjectionMap;
191
192 private static final int NOTES = 1;
193@@ -77,6 +81,14 @@
194
195 // Logging info
196 private static final String TAG = "NoteProvider";
197+
198+ // List of each version's columns
199+ private static final String[][] COLUMNS_VERSION = {
200+ { Note.TITLE, Note.FILE, Note.MODIFIED_DATE },
201+ { Note.GUID, Note.TITLE, Note.FILE, Note.NOTE_CONTENT, Note.MODIFIED_DATE },
202+ { Note.GUID, Note.TITLE, Note.FILE, Note.NOTE_CONTENT, Note.MODIFIED_DATE, Note.TAGS },
203+ { Note.GUID, Note.TITLE, Note.FILE, Note.NOTE_CONTENT, Note.NOTE_CONTENT_PLAIN, Note.MODIFIED_DATE, Note.TAGS }
204+ };
205
206 /**
207 * This class helps open, create, and upgrade the database file.
208@@ -89,12 +101,13 @@
209
210 @Override
211 public void onCreate(SQLiteDatabase db) {
212- db.execSQL("CREATE TABLE " + DB_TABLE_NOTES + " ("
213+ db.execSQL("CREATE TABLE " + DB_TABLE_NOTES + " ("
214 + Note.ID + " INTEGER PRIMARY KEY,"
215 + Note.GUID + " TEXT,"
216 + Note.TITLE + " TEXT,"
217 + Note.FILE + " TEXT,"
218 + Note.NOTE_CONTENT + " TEXT,"
219+ + Note.NOTE_CONTENT_PLAIN + " TEXT,"
220 + Note.MODIFIED_DATE + " STRING,"
221 + Note.TAGS + " STRING"
222 + ");");
223@@ -102,10 +115,51 @@
224
225 @Override
226 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
227- TLog.d(TAG, "Upgrading database from version {0} to {1}, which will destroy all old data",
228- oldVersion, newVersion);
229+ TLog.d(TAG, "Upgrading database from version {0} to {1}", oldVersion, newVersion);
230+
231+ Cursor notesCursor;
232+ ArrayList<Map<String, String>> db_list = new ArrayList<Map<String, String>>();
233+ notesCursor = db.query(DB_TABLE_NOTES, COLUMNS_VERSION[oldVersion - 1], null, null, null, null, null);
234+ notesCursor.moveToFirst();
235+
236+ if (oldVersion == 1) {
237+ // GUID and NOTE_CONTENT are not saved.
238+ TLog.d(TAG, "Database version {0} is not supported to update, all old datas will be destroyed", oldVersion);
239+ db.execSQL("DROP TABLE IF EXISTS notes");
240+ onCreate(db);
241+ return;
242+ }
243+
244+ // Get old datas from the SQL
245+ while(!notesCursor.isAfterLast()) {
246+ Map<String, String> row = new HashMap<String, String>();
247+ for(int i = 0; i < COLUMNS_VERSION[oldVersion - 1].length; i++) {
248+ row.put(COLUMNS_VERSION[oldVersion - 1][i], notesCursor.getString(i));
249+ }
250+
251+ // create new columns
252+ if (oldVersion <= 2) {
253+ row.put(Note.TAGS, "");
254+ }
255+ if (oldVersion <= 3) {
256+ row.put(Note.NOTE_CONTENT_PLAIN, StringConverter.encode(Html.fromHtml(row.get(Note.NOTE_CONTENT)).toString()));
257+ }
258+
259+ db_list.add(row);
260+ notesCursor.moveToNext();
261+ }
262+
263 db.execSQL("DROP TABLE IF EXISTS notes");
264 onCreate(db);
265+
266+ // put rows to the database
267+ ContentValues row = new ContentValues();
268+ for(int i = 0; i < db_list.size(); i++) {
269+ for(int j = 0; j < COLUMNS_VERSION[newVersion - 1].length; j++) {
270+ row.put(COLUMNS_VERSION[newVersion - 1][j], db_list.get(i).get(COLUMNS_VERSION[newVersion - 1][j]));
271+ }
272+ db.insert(DB_TABLE_NOTES, null, row);
273+ }
274 }
275 }
276
277@@ -135,11 +189,11 @@
278 break;
279
280 case NOTE_TITLE:
281- qb.setTables(DB_TABLE_NOTES);
282- qb.setProjectionMap(notesProjectionMap);
283- // TODO appendWhere + whereArgs instead (new String[] whereArgs = uri.getLas..)?
284- qb.appendWhere(Note.TITLE + " LIKE '" + uri.getLastPathSegment()+"'");
285- break;
286+ qb.setTables(DB_TABLE_NOTES);
287+ qb.setProjectionMap(notesProjectionMap);
288+ // TODO appendWhere + whereArgs instead (new String[] whereArgs = uri.getLas..)?
289+ qb.appendWhere(Note.TITLE + " LIKE '" + uri.getLastPathSegment()+"'");
290+ break;
291
292 default:
293 throw new IllegalArgumentException("Unknown URI " + uri);
294@@ -173,7 +227,7 @@
295 return Tomdroid.CONTENT_ITEM_TYPE;
296
297 case NOTE_TITLE:
298- return Tomdroid.CONTENT_ITEM_TYPE;
299+ return Tomdroid.CONTENT_ITEM_TYPE;
300
301 default:
302 throw new IllegalArgumentException("Unknown URI " + uri);
303@@ -205,7 +259,7 @@
304
305 // The guid is the unique identifier for a note so it has to be set.
306 if (values.containsKey(Note.GUID) == false) {
307- values.put(Note.GUID, UUID.randomUUID().toString());
308+ values.put(Note.GUID, UUID.randomUUID().toString());
309 }
310
311 // TODO does this make sense?
312@@ -291,6 +345,7 @@
313 notesProjectionMap.put(Note.TITLE, Note.TITLE);
314 notesProjectionMap.put(Note.FILE, Note.FILE);
315 notesProjectionMap.put(Note.NOTE_CONTENT, Note.NOTE_CONTENT);
316+ notesProjectionMap.put(Note.NOTE_CONTENT_PLAIN, Note.NOTE_CONTENT_PLAIN);
317 notesProjectionMap.put(Note.TAGS, Note.TAGS);
318 notesProjectionMap.put(Note.MODIFIED_DATE, Note.MODIFIED_DATE);
319 }
320
321=== modified file 'src/org/tomdroid/sync/sd/NoteHandler.java'
322--- src/org/tomdroid/sync/sd/NoteHandler.java 2011-09-05 15:05:46 +0000
323+++ src/org/tomdroid/sync/sd/NoteHandler.java 2012-04-01 04:35:23 +0000
324@@ -24,6 +24,7 @@
325 package org.tomdroid.sync.sd;
326
327 import org.tomdroid.Note;
328+import org.tomdroid.util.StringConverter;
329 import org.xml.sax.Attributes;
330 import org.xml.sax.SAXException;
331 import org.xml.sax.helpers.DefaultHandler;
332@@ -35,15 +36,20 @@
333 // position keepers
334 private boolean inTitleTag = false;
335 private boolean inLastChangeDateTag = false;
336+ private boolean inTagTag = false;
337
338 // -- Tomboy's notes XML tags names --
339 // Metadata related
340 private final static String TITLE = "title";
341 private final static String LAST_CHANGE_DATE = "last-change-date";
342+ private final static String TAGS = "tags";
343+ private final static String TAG = "tag";
344
345 // Buffers for parsed elements
346 private StringBuilder title = new StringBuilder();
347 private StringBuilder lastChangeDate = new StringBuilder();
348+ private StringBuilder tags = new StringBuilder();
349+ private StringBuilder tag;
350
351 // link to model
352 private Note note;
353@@ -61,6 +67,9 @@
354 inTitleTag = true;
355 } else if (localName.equals(LAST_CHANGE_DATE)) {
356 inLastChangeDateTag = true;
357+ } else if (localName.equals(TAG)) {
358+ inTagTag = true;
359+ tag = new StringBuilder();
360 }
361
362 }
363@@ -75,6 +84,12 @@
364 } else if (localName.equals(LAST_CHANGE_DATE)) {
365 inLastChangeDateTag = false;
366 note.setLastChangeDate(lastChangeDate.toString());
367+ } else if (localName.equals(TAGS)) {
368+ note.setTags(tags.toString());
369+ } else if (localName.equals(TAG)) {
370+ inTagTag = false;
371+ tags.append(StringConverter.encodeForTag(tag.toString()));
372+ tags.append(",");
373 }
374 }
375
376@@ -86,6 +101,8 @@
377 title.append(ch, start, length);
378 } else if (inLastChangeDateTag) {
379 lastChangeDate.append(ch, start, length);
380+ } else if (inTagTag) {
381+ tag.append(ch, start, length);
382 }
383 }
384 }
385
386=== added file 'src/org/tomdroid/util/StringConverter.java'
387--- src/org/tomdroid/util/StringConverter.java 1970-01-01 00:00:00 +0000
388+++ src/org/tomdroid/util/StringConverter.java 2012-04-01 04:35:23 +0000
389@@ -0,0 +1,51 @@
390+/*
391+ * Tomdroid
392+ * Tomboy on Android
393+ * http://www.launchpad.net/tomdroid
394+ *
395+ * Copyright 2011 Koichi Akabe <vbkaisetsu@gmail.com>
396+ *
397+ * This file is part of Tomdroid.
398+ *
399+ * Tomdroid is free software: you can redistribute it and/or modify
400+ * it under the terms of the GNU General Public License as published by
401+ * the Free Software Foundation, either version 3 of the License, or
402+ * (at your option) any later version.
403+ *
404+ * Tomdroid is distributed in the hope that it will be useful,
405+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
406+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
407+ * GNU General Public License for more details.
408+ *
409+ * You should have received a copy of the GNU General Public License
410+ * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>.
411+ */
412+package org.tomdroid.util;
413+
414+public class StringConverter {
415+ public static String encode(String text) {
416+ return text.replace("&", "&amp;")
417+ .replace("%", "&pct;")
418+ .replace("_", "&und;");
419+ }
420+
421+ public static String decode(String text) {
422+ return text.replace("&und;", "_")
423+ .replace("&pct;", "%")
424+ .replace("&amp;", "&");
425+ }
426+
427+ public static String encodeForTag(String text) {
428+ return text.replace("&", "&amp;")
429+ .replace("%", "&pct;")
430+ .replace(",", "&com;")
431+ .replace("_", "&und;");
432+ }
433+
434+ public static String decodeForTag(String text) {
435+ return text.replace("&und;", "_")
436+ .replace("&com;", ",")
437+ .replace("&pct;", "%")
438+ .replace("&amp;", "&");
439+ }
440+}

Subscribers

People subscribed via source and target branches