1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.keyboard.internal; 18 19 import com.android.inputmethod.latin.Constants; 20 21 import android.text.TextUtils; 22 23 /** 24 * The string parser of codesArray specification for <GridRows />. The attribute codesArray is an 25 * array of string. 26 * Each element of the array defines a key label by specifying a code point as a hexadecimal string. 27 * A key label may consist of multiple code points separated by comma. 28 * Each element of the array optionally can have an output text definition after vertical bar 29 * marker. An output text may consist of multiple code points separated by comma. 30 * The format of the codesArray element should be: 31 * <pre> 32 * codePointInHex[,codePoint2InHex]*(|outputTextCodePointInHex[,outputTextCodePoint2InHex]*)? 33 * </pre> 34 */ 35 // TODO: Write unit tests for this class. 36 public final class CodesArrayParser { 37 // Constants for parsing. 38 private static final char COMMA = ','; 39 private static final String VERTICAL_BAR_STRING = "\\|"; 40 private static final String COMMA_STRING = ","; 41 private static final int BASE_HEX = 16; 42 43 private CodesArrayParser() { 44 // This utility class is not publicly instantiable. 45 } 46 47 private static String getLabelSpec(final String codesArraySpec) { 48 final String[] strs = codesArraySpec.split(VERTICAL_BAR_STRING, -1); 49 if (strs.length <= 1) { 50 return codesArraySpec; 51 } 52 return strs[0]; 53 } 54 55 public static String parseLabel(final String codesArraySpec) { 56 final String labelSpec = getLabelSpec(codesArraySpec); 57 final StringBuilder sb = new StringBuilder(); 58 for (final String codeInHex : labelSpec.split(COMMA_STRING)) { 59 final int codePoint = Integer.parseInt(codeInHex, BASE_HEX); 60 sb.appendCodePoint(codePoint); 61 } 62 return sb.toString(); 63 } 64 65 private static String getCodeSpec(final String codesArraySpec) { 66 final String[] strs = codesArraySpec.split(VERTICAL_BAR_STRING, -1); 67 if (strs.length <= 1) { 68 return codesArraySpec; 69 } 70 return TextUtils.isEmpty(strs[1]) ? strs[0] : strs[1]; 71 } 72 73 // codesArraySpec consists of: 74 // <label>|<code0>,<code1>,...|<minSupportSdkVersion> 75 public static int getMinSupportSdkVersion(final String codesArraySpec) { 76 final String[] strs = codesArraySpec.split(VERTICAL_BAR_STRING, -1); 77 if (strs.length <= 2) { 78 return 0; 79 } 80 try { 81 return Integer.parseInt(strs[2]); 82 } catch (NumberFormatException e) { 83 return 0; 84 } 85 } 86 87 public static int parseCode(final String codesArraySpec) { 88 final String codeSpec = getCodeSpec(codesArraySpec); 89 if (codeSpec.indexOf(COMMA) < 0) { 90 return Integer.parseInt(codeSpec, BASE_HEX); 91 } 92 return Constants.CODE_OUTPUT_TEXT; 93 } 94 95 public static String parseOutputText(final String codesArraySpec) { 96 final String codeSpec = getCodeSpec(codesArraySpec); 97 if (codeSpec.indexOf(COMMA) < 0) { 98 return null; 99 } 100 final StringBuilder sb = new StringBuilder(); 101 for (final String codeInHex : codeSpec.split(COMMA_STRING)) { 102 final int codePoint = Integer.parseInt(codeInHex, BASE_HEX); 103 sb.appendCodePoint(codePoint); 104 } 105 return sb.toString(); 106 } 107 } 108