2013-08-26 04:09:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard.internal;
|
|
|
|
|
2014-10-23 09:37:32 +00:00
|
|
|
import com.android.inputmethod.latin.common.Constants;
|
2014-01-31 06:58:14 +00:00
|
|
|
import com.android.inputmethod.latin.utils.StringUtils;
|
2013-08-26 04:09:19 +00:00
|
|
|
|
2013-10-01 11:45:45 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
2013-08-26 04:09:19 +00:00
|
|
|
/**
|
|
|
|
* The string parser of codesArray specification for <GridRows />. The attribute codesArray is an
|
|
|
|
* array of string.
|
|
|
|
* Each element of the array defines a key label by specifying a code point as a hexadecimal string.
|
|
|
|
* A key label may consist of multiple code points separated by comma.
|
|
|
|
* Each element of the array optionally can have an output text definition after vertical bar
|
|
|
|
* marker. An output text may consist of multiple code points separated by comma.
|
|
|
|
* The format of the codesArray element should be:
|
|
|
|
* <pre>
|
2014-01-31 06:58:14 +00:00
|
|
|
* label1[,label2]*(|outputText1[,outputText2]*(|minSupportSdkVersion)?)?
|
2013-08-26 04:09:19 +00:00
|
|
|
* </pre>
|
|
|
|
*/
|
|
|
|
// TODO: Write unit tests for this class.
|
|
|
|
public final class CodesArrayParser {
|
|
|
|
// Constants for parsing.
|
2014-01-31 06:58:14 +00:00
|
|
|
private static final char COMMA = Constants.CODE_COMMA;
|
|
|
|
private static final String COMMA_REGEX = StringUtils.newSingleCodePointString(COMMA);
|
|
|
|
private static final String VERTICAL_BAR_REGEX = // "\\|"
|
|
|
|
new String(new char[] { Constants.CODE_BACKSLASH, Constants.CODE_VERTICAL_BAR });
|
2013-08-26 04:09:19 +00:00
|
|
|
private static final int BASE_HEX = 16;
|
|
|
|
|
|
|
|
private CodesArrayParser() {
|
|
|
|
// This utility class is not publicly instantiable.
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getLabelSpec(final String codesArraySpec) {
|
2014-01-31 06:58:14 +00:00
|
|
|
final String[] strs = codesArraySpec.split(VERTICAL_BAR_REGEX, -1);
|
2013-10-01 11:45:45 +00:00
|
|
|
if (strs.length <= 1) {
|
|
|
|
return codesArraySpec;
|
|
|
|
}
|
|
|
|
return strs[0];
|
2013-08-26 04:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String parseLabel(final String codesArraySpec) {
|
|
|
|
final String labelSpec = getLabelSpec(codesArraySpec);
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
2014-01-31 06:58:14 +00:00
|
|
|
for (final String codeInHex : labelSpec.split(COMMA_REGEX)) {
|
2013-08-26 04:09:19 +00:00
|
|
|
final int codePoint = Integer.parseInt(codeInHex, BASE_HEX);
|
|
|
|
sb.appendCodePoint(codePoint);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getCodeSpec(final String codesArraySpec) {
|
2014-01-31 06:58:14 +00:00
|
|
|
final String[] strs = codesArraySpec.split(VERTICAL_BAR_REGEX, -1);
|
2013-10-01 11:45:45 +00:00
|
|
|
if (strs.length <= 1) {
|
|
|
|
return codesArraySpec;
|
|
|
|
}
|
|
|
|
return TextUtils.isEmpty(strs[1]) ? strs[0] : strs[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getMinSupportSdkVersion(final String codesArraySpec) {
|
2014-01-31 06:58:14 +00:00
|
|
|
final String[] strs = codesArraySpec.split(VERTICAL_BAR_REGEX, -1);
|
2013-10-01 11:45:45 +00:00
|
|
|
if (strs.length <= 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return Integer.parseInt(strs[2]);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-26 04:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static int parseCode(final String codesArraySpec) {
|
|
|
|
final String codeSpec = getCodeSpec(codesArraySpec);
|
|
|
|
if (codeSpec.indexOf(COMMA) < 0) {
|
|
|
|
return Integer.parseInt(codeSpec, BASE_HEX);
|
|
|
|
}
|
|
|
|
return Constants.CODE_OUTPUT_TEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String parseOutputText(final String codesArraySpec) {
|
|
|
|
final String codeSpec = getCodeSpec(codesArraySpec);
|
|
|
|
if (codeSpec.indexOf(COMMA) < 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
2014-01-31 06:58:14 +00:00
|
|
|
for (final String codeInHex : codeSpec.split(COMMA_REGEX)) {
|
2013-08-26 04:09:19 +00:00
|
|
|
final int codePoint = Integer.parseInt(codeInHex, BASE_HEX);
|
|
|
|
sb.appendCodePoint(codePoint);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|