summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david+git@madore.org>2010-04-23 18:30:01 (GMT)
committerDavid A. Madore <david+git@madore.org>2010-04-23 18:30:01 (GMT)
commit84830a2d2c68bc63115fa94374d5ca4c344b4ca0 (patch)
treebebe502e3cab49cb9f786a578a842100a9484ccc
parent7718ef99581a6364c1acd6e94fad5b7352c2da82 (diff)
downloadUnicodeMap-84830a2d2c68bc63115fa94374d5ca4c344b4ca0.zip
UnicodeMap-84830a2d2c68bc63115fa94374d5ca4c344b4ca0.tar.gz
UnicodeMap-84830a2d2c68bc63115fa94374d5ca4c344b4ca0.tar.bz2
Store data in a SQLite database.
-rw-r--r--src/org/madore/android/unicodeMap/UnicodeCharacter.java4
-rw-r--r--src/org/madore/android/unicodeMap/UnicodeDatabase.java174
-rw-r--r--src/org/madore/android/unicodeMap/UnicodeMapActivity.java35
3 files changed, 184 insertions, 29 deletions
diff --git a/src/org/madore/android/unicodeMap/UnicodeCharacter.java b/src/org/madore/android/unicodeMap/UnicodeCharacter.java
index c499921..fb68169 100644
--- a/src/org/madore/android/unicodeMap/UnicodeCharacter.java
+++ b/src/org/madore/android/unicodeMap/UnicodeCharacter.java
@@ -6,10 +6,12 @@ public class UnicodeCharacter {
protected final int codePoint;
protected final String name;
+ protected final String category;
- public UnicodeCharacter(int codePoint, String name) {
+ public UnicodeCharacter(int codePoint, String name, String category) {
this.codePoint = codePoint;
this.name = name;
+ this.category = category;
}
public int getCodePoint() {
diff --git a/src/org/madore/android/unicodeMap/UnicodeDatabase.java b/src/org/madore/android/unicodeMap/UnicodeDatabase.java
new file mode 100644
index 0000000..9e809f2
--- /dev/null
+++ b/src/org/madore/android/unicodeMap/UnicodeDatabase.java
@@ -0,0 +1,174 @@
+package org.madore.android.unicodeMap;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.io.InputStreamReader;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.sql.SQLException;
+import android.content.Context;
+import android.content.res.AssetManager;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteStatement;
+import android.database.sqlite.SQLiteOpenHelper;
+
+public class UnicodeDatabase {
+
+ protected static final String DATABASE_NAME = "unicode.db";
+ protected static final int DATABASE_VERSION = 1;
+ protected static final String UNICODE_TABLE_NAME = "unicode";
+
+ protected static class DatabaseInit extends SQLiteOpenHelper {
+
+ final Context context;
+
+ DatabaseInit(Context context) {
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ this.context = context;
+ }
+
+ protected void parseUnicodeData(SQLiteDatabase db) {
+ AssetManager amgr = context.getAssets();
+ try {
+ BufferedReader rd
+ = new BufferedReader(new InputStreamReader(amgr.open("UnicodeData.txt"),
+ "US-ASCII"));
+ SQLiteStatement cmd
+ = db.compileStatement("INSERT INTO "+UNICODE_TABLE_NAME
+ +" VALUES ( ? , ? , ? )");
+ String line;
+ while ( ( line = rd.readLine() ) != null ) {
+ String[] fields = line.split(";");
+ int codePoint = Integer.parseInt(fields[0], 16);
+ String name = fields[1];
+ String category = fields[2];
+ if ( name.charAt(0) == '<' )
+ continue;
+ cmd.bindLong(1, codePoint);
+ cmd.bindString(2, name);
+ cmd.bindString(3, category);
+ cmd.execute();
+ }
+ } catch (UnsupportedEncodingException e) {
+ throw new AssertionError("US-ASCII encoding unsupported");
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ db.execSQL("CREATE TABLE "+UNICODE_TABLE_NAME+" ( "
+ +"id INTEGER PRIMARY KEY , "
+ +"name TEXT , "
+ +"category TEXT )");
+ parseUnicodeData(db);
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db,
+ int oldVersion, int newVersion) {
+ db.execSQL("DROP TABLE IF EXISTS "+UNICODE_TABLE_NAME);
+ onCreate(db);
+ }
+
+ }
+
+ SQLiteDatabase db;
+
+ public UnicodeDatabase(Context context) {
+ DatabaseInit dbinit = new DatabaseInit(context);
+ if ( true )
+ db = dbinit.getReadableDatabase();
+ else {
+ db = dbinit.getWritableDatabase();
+ db.execSQL("DELETE FROM "+UNICODE_TABLE_NAME);
+ dbinit.parseUnicodeData(db);
+ }
+ }
+
+ protected final static String[] queryColumns = { "id", "name", "category" };
+
+ public UnicodeCharacter getSingle(int codePoint) {
+ final Cursor c
+ = db.query(UNICODE_TABLE_NAME, queryColumns,
+ "id=?",
+ new String[] { Integer.toString(codePoint) },
+ null, null, null, null);
+ UnicodeCharacter ch;
+ if ( c.getCount() > 0 ) {
+ c.moveToFirst();
+ ch = new UnicodeCharacter(c.getInt(0),
+ c.getString(1), c.getString(2));
+ } else
+ ch = null;
+ c.close();
+ return ch;
+ }
+
+ protected final static String[] countColumns = { "count(*)" };
+
+ public int countRange(int from, int to) {
+ final Cursor c
+ = db.query(UNICODE_TABLE_NAME, countColumns,
+ "id >= ? AND id < ?",
+ new String[] { Integer.toString(from),
+ Integer.toString(to) },
+ null, null, null, null);
+ if ( c.getCount() != 1 )
+ throw new AssertionError("\"SELECT count(*)\" returned no result");
+ c.moveToFirst();
+ int cnt = c.getInt(0);
+ c.close();
+ return cnt;
+ }
+
+ private static class CursorIterable
+ implements Iterable<UnicodeCharacter> {
+ protected final Cursor c;
+ public CursorIterable(Cursor c) {
+ this.c = c;
+ }
+ public Iterator<UnicodeCharacter> iterator() {
+ final boolean hasData = c.getCount() > 0;
+ if ( hasData )
+ c.moveToFirst();
+ else
+ c.close();
+ return (new Iterator<UnicodeCharacter>() {
+ boolean hasNext = hasData;
+ public boolean hasNext() { return hasNext; }
+ public UnicodeCharacter next() {
+ if ( hasNext ) {
+ UnicodeCharacter ch
+ = new UnicodeCharacter(c.getInt(0), c.getString(1),
+ c.getString(2));
+ if ( c.isLast() ) {
+ hasNext = false;
+ c.close();
+ } else
+ c.moveToNext();
+ return ch;
+ } else
+ throw new NoSuchElementException();
+ }
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ });
+ }
+ }
+
+ public Iterable<UnicodeCharacter> getRange(int from, int to) {
+ final Cursor c
+ = db.query(UNICODE_TABLE_NAME, queryColumns,
+ "id >= ? AND id < ?",
+ new String[] { Integer.toString(from),
+ Integer.toString(to) },
+ null, null, null, null);
+ return new CursorIterable(c);
+ }
+
+}
diff --git a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java
index 6caad15..f4c7dc6 100644
--- a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java
+++ b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java
@@ -2,10 +2,6 @@ package org.madore.android.unicodeMap;
import java.util.List;
import java.util.ArrayList;
-import java.io.InputStreamReader;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
@@ -14,36 +10,19 @@ import android.widget.*;
import android.text.ClipboardManager;
import android.app.ListActivity;
-public class UnicodeMapActivity extends ListActivity {
+public final class UnicodeMapActivity extends ListActivity {
- static final List<UnicodeCharacter> list = new ArrayList<UnicodeCharacter>(12);
-
- protected void parseUnicodeData() {
- AssetManager amgr = getAssets();
- try {
- BufferedReader rd
- = new BufferedReader(new InputStreamReader(amgr.open("UnicodeData.txt"),
- "US-ASCII"));
- String line;
- while ( ( line = rd.readLine() ) != null ) {
- String[] bits = line.split(";");
- int codePoint = Integer.parseInt(bits[0], 16);
- String name = bits[1];
- if ( name.charAt(0) != '<' )
- list.add(new UnicodeCharacter(codePoint, name));
- }
- } catch (UnsupportedEncodingException e) {
- throw new AssertionError("US-ASCII encoding unsupported");
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
+ protected UnicodeDatabase db;
+ protected List<UnicodeCharacter> list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- parseUnicodeData();
+ this.db = new UnicodeDatabase(getApplicationContext());
+ this.list = new ArrayList<UnicodeCharacter>(db.countRange(0,1024));
+ for ( UnicodeCharacter ch : db.getRange(0,1024) )
+ list.add(ch);
final ClipboardManager cmgr
= (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
setListAdapter(new ArrayAdapter<UnicodeCharacter>(this, R.layout.list_item, list));