aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorTadashi G. Takaoka <takaoka@google.com>2012-01-20 16:19:51 +0900
committerTadashi G. Takaoka <takaoka@google.com>2012-01-20 18:46:33 +0900
commite54a4005d569cddbf8610dfd3e9afaec540fa060 (patch)
treef89e0a5383a6661d1c408a855a45f76820b4b869 /java/src
parent3eede315dc9998091477eb95e748e13ad6fd6e57 (diff)
downloadlatinime-e54a4005d569cddbf8610dfd3e9afaec540fa060.tar.gz
latinime-e54a4005d569cddbf8610dfd3e9afaec540fa060.tar.xz
latinime-e54a4005d569cddbf8610dfd3e9afaec540fa060.zip
Support @string reference in moreKeys attribute
Change-Id: If0056d0601149d2ddd0e231a81e7b2409b37fc06
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java37
-rw-r--r--java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java25
-rw-r--r--java/src/com/android/inputmethod/latin/Utils.java55
3 files changed, 91 insertions, 26 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java
index 0f84c4563..faea38941 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java
@@ -16,11 +16,13 @@
package com.android.inputmethod.keyboard.internal;
+import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.Log;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.XmlParseUtils;
import org.xmlpull.v1.XmlPullParser;
@@ -30,15 +32,13 @@ import java.util.ArrayList;
import java.util.HashMap;
public class KeyStyles {
- private static final String TAG = "KeyStyles";
+ private static final String TAG = KeyStyles.class.getSimpleName();
private static final boolean DEBUG = false;
private final HashMap<String, DeclaredKeyStyle> mStyles =
new HashMap<String, DeclaredKeyStyle>();
private static final KeyStyle EMPTY_KEY_STYLE = new EmptyKeyStyle();
- private static final char ESCAPE_CHAR = '\\';
-
public interface KeyStyle {
public String[] getTextArray(TypedArray a, int index);
public CharSequence getText(TypedArray a, int index);
@@ -75,23 +75,30 @@ public class KeyStyles {
if (!a.hasValue(index))
return null;
final CharSequence text = a.getText(index);
- return parseCsvText(text.toString());
+ return parseCsvText(text.toString(), a.getResources(), R.string.english_ime_name);
}
-
}
/* package for test */
- static String[] parseCsvText(String text) {
+ static String[] parseCsvText(String rawText, Resources res, int packageNameResId) {
+ final String text = Utils.resolveStringResource(rawText, res, packageNameResId);
final int size = text.length();
- if (size == 0) return null;
- if (size == 1) return new String[] { text };
+ if (size == 0) {
+ return null;
+ }
+ if (size == 1) {
+ return new String[] { text };
+ }
+
final StringBuilder sb = new StringBuilder();
ArrayList<String> list = null;
int start = 0;
for (int pos = 0; pos < size; pos++) {
final char c = text.charAt(pos);
if (c == ',') {
- if (list == null) list = new ArrayList<String>();
+ if (list == null) {
+ list = new ArrayList<String>();
+ }
if (sb.length() == 0) {
list.add(text.substring(start, pos));
} else {
@@ -100,24 +107,28 @@ public class KeyStyles {
}
start = pos + 1;
continue;
- } else if (c == ESCAPE_CHAR) {
+ } else if (c == Utils.ESCAPE_CHAR) {
if (start == pos) {
// Skip escape character at the beginning of the value.
start++;
pos++;
} else {
- if (start < pos && sb.length() == 0)
+ if (start < pos && sb.length() == 0) {
sb.append(text.subSequence(start, pos));
+ }
pos++;
- if (pos < size)
+ if (pos < size) {
sb.append(text.charAt(pos));
+ }
}
} else if (sb.length() > 0) {
sb.append(c);
}
}
if (list == null) {
- return new String[] { sb.length() > 0 ? sb.toString() : text.substring(start) };
+ return new String[] {
+ sb.length() > 0 ? sb.toString() : text.substring(start)
+ };
} else {
list.add(sb.length() > 0 ? sb.toString() : text.substring(start));
return list.toArray(new String[list.size()]);
diff --git a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java
index d4c85c33d..2facf8439 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java
@@ -41,11 +41,9 @@ import java.util.ArrayList;
public class MoreKeySpecParser {
private static final String TAG = MoreKeySpecParser.class.getSimpleName();
- private static final char ESCAPE_CHAR = '\\';
- private static final String LABEL_END = "|";
- private static final String PREFIX_AT = "@";
- private static final String PREFIX_ICON = PREFIX_AT + "icon/";
- private static final String PREFIX_CODE = PREFIX_AT + "integer/";
+ private static final char LABEL_END = '|';
+ private static final String PREFIX_ICON = Utils.PREFIX_AT + "icon" + Utils.SUFFIX_SLASH;
+ private static final String PREFIX_CODE = Utils.PREFIX_AT + "integer" + Utils.SUFFIX_SLASH;
private MoreKeySpecParser() {
// Intentional empty constructor for utility class.
@@ -72,14 +70,14 @@ public class MoreKeySpecParser {
}
private static String parseEscape(String text) {
- if (text.indexOf(ESCAPE_CHAR) < 0) {
+ if (text.indexOf(Utils.ESCAPE_CHAR) < 0) {
return text;
}
final int length = text.length();
final StringBuilder sb = new StringBuilder();
for (int pos = 0; pos < length; pos++) {
final char c = text.charAt(pos);
- if (c == ESCAPE_CHAR && pos + 1 < length) {
+ if (c == Utils.ESCAPE_CHAR && pos + 1 < length) {
sb.append(text.charAt(++pos));
} else {
sb.append(c);
@@ -89,7 +87,7 @@ public class MoreKeySpecParser {
}
private static int indexOfLabelEnd(String moreKeySpec, int start) {
- if (moreKeySpec.indexOf(ESCAPE_CHAR, start) < 0) {
+ if (moreKeySpec.indexOf(Utils.ESCAPE_CHAR, start) < 0) {
final int end = moreKeySpec.indexOf(LABEL_END, start);
if (end == 0) {
throw new MoreKeySpecParserError(LABEL_END + " at " + start + ": " + moreKeySpec);
@@ -99,9 +97,9 @@ public class MoreKeySpecParser {
final int length = moreKeySpec.length();
for (int pos = start; pos < length; pos++) {
final char c = moreKeySpec.charAt(pos);
- if (c == ESCAPE_CHAR && pos + 1 < length) {
+ if (c == Utils.ESCAPE_CHAR && pos + 1 < length) {
pos++;
- } else if (moreKeySpec.startsWith(LABEL_END, pos)) {
+ } else if (c == LABEL_END) {
return pos;
}
}
@@ -131,7 +129,8 @@ public class MoreKeySpecParser {
throw new MoreKeySpecParserError("Multiple " + LABEL_END + ": "
+ moreKeySpec);
}
- final String outputText = parseEscape(moreKeySpec.substring(end + LABEL_END.length()));
+ final String outputText = parseEscape(
+ moreKeySpec.substring(end + /* LABEL_END */1));
if (!TextUtils.isEmpty(outputText)) {
return outputText;
}
@@ -152,7 +151,7 @@ public class MoreKeySpecParser {
throw new MoreKeySpecParserError("Multiple " + LABEL_END + ": " + moreKeySpec);
}
final int resId = Utils.getResourceId(res,
- moreKeySpec.substring(end + LABEL_END.length() + PREFIX_AT.length()),
+ moreKeySpec.substring(end + /* LABEL_END */1 + /* PREFIX_AT */1),
R.string.english_ime_name);
final int code = res.getInteger(resId);
return code;
@@ -170,7 +169,7 @@ public class MoreKeySpecParser {
public static int getIconId(String moreKeySpec) {
if (hasIcon(moreKeySpec)) {
- int end = moreKeySpec.indexOf(LABEL_END, PREFIX_ICON.length() + 1);
+ final int end = moreKeySpec.indexOf(LABEL_END, PREFIX_ICON.length() + 1);
final String iconId = moreKeySpec.substring(PREFIX_ICON.length(), end);
try {
return Integer.valueOf(iconId);
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index d8ca409c7..bc8a1301c 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -62,6 +62,12 @@ public class Utils {
private static boolean DBG = LatinImeLogger.sDBG;
private static boolean DBG_EDIT_DISTANCE = false;
+ // Constants for resource name parsing.
+ public static final char ESCAPE_CHAR = '\\';
+ public static final char PREFIX_AT = '@';
+ public static final char SUFFIX_SLASH = '/';
+ private static final String PREFIX_STRING = PREFIX_AT + "string";
+
private Utils() {
// Intentional empty constructor for utility class.
}
@@ -802,4 +808,53 @@ public class Utils {
}
return resId;
}
+
+ public static String resolveStringResource(String text, Resources res, int packageNameResId) {
+ final int size = text.length();
+ if (size < PREFIX_STRING.length()) {
+ return text;
+ }
+
+ StringBuilder sb = null;
+ for (int pos = 0; pos < size; pos++) {
+ final char c = text.charAt(pos);
+ if (c == PREFIX_AT && text.startsWith(PREFIX_STRING, pos)) {
+ if (sb == null) {
+ sb = new StringBuilder(text.substring(0, pos));
+ }
+ final int end = Utils.searchResourceNameEnd(text, pos + PREFIX_STRING.length());
+ final String resName = text.substring(pos + 1, end);
+ final int resId = getResourceId(res, resName, packageNameResId);
+ sb.append(res.getString(resId));
+ pos = end - 1;
+ } else if (c == ESCAPE_CHAR) {
+ pos++;
+ if (sb != null) {
+ sb.append(c);
+ if (pos < size) {
+ sb.append(text.charAt(pos));
+ }
+ }
+ } else if (sb != null) {
+ sb.append(c);
+ }
+ }
+ return (sb == null) ? text : sb.toString();
+ }
+
+ private static int searchResourceNameEnd(String text, int start) {
+ final int size = text.length();
+ if (start >= size || text.charAt(start) != SUFFIX_SLASH) {
+ throw new RuntimeException("Resource name not specified");
+ }
+ for (int pos = start + 1; pos < size; pos++) {
+ final char c = text.charAt(pos);
+ // String resource name should be consisted of [a-z_0-9].
+ if ((c >= 'a' && c <= 'z') || c == '_' || (c >= '0' && c <= '9')) {
+ continue;
+ }
+ return pos;
+ }
+ return size;
+ }
}