Home | History | Annotate | Download | only in internal
      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.common.Constants;
     20 import com.android.inputmethod.latin.common.StringUtils;
     21 
     22 import android.text.TextUtils;
     23 
     24 /**
     25  * The string parser of codesArray specification for <GridRows />. The attribute codesArray is an
     26  * array of string.
     27  * Each element of the array defines a key label by specifying a code point as a hexadecimal string.
     28  * A key label may consist of multiple code points separated by comma.
     29  * Each element of the array optionally can have an output text definition after vertical bar
     30  * marker. An output text may consist of multiple code points separated by comma.
     31  * The format of the codesArray element should be:
     32  * <pre>
     33  *   label1[,label2]*(|outputText1[,outputText2]*(|minSupportSdkVersion)?)?
     34  * </pre>
     35  */
     36 // TODO: Write unit tests for this class.
     37 public final class CodesArrayParser {
     38     // Constants for parsing.
     39     private static final char COMMA = Constants.CODE_COMMA;
     40     private static final String COMMA_REGEX = StringUtils.newSingleCodePointString(COMMA);
     41     private static final String VERTICAL_BAR_REGEX = // "\\|"
     42             new String(new char[] { Constants.CODE_BACKSLASH, Constants.CODE_VERTICAL_BAR });
     43     private static final int BASE_HEX = 16;
     44 
     45     private CodesArrayParser() {
     46      // This utility class is not publicly instantiable.
     47     }
     48 
     49     private static String getLabelSpec(final String codesArraySpec) {
     50         final String[] strs = codesArraySpec.split(VERTICAL_BAR_REGEX, -1);
     51         if (strs.length <= 1) {
     52             return codesArraySpec;
     53         }
     54         return strs[0];
     55     }
     56 
     57     public static String parseLabel(final String codesArraySpec) {
     58         final String labelSpec = getLabelSpec(codesArraySpec);
     59         final StringBuilder sb = new StringBuilder();
     60         for (final String codeInHex : labelSpec.split(COMMA_REGEX)) {
     61             final int codePoint = Integer.parseInt(codeInHex, BASE_HEX);
     62             sb.appendCodePoint(codePoint);
     63         }
     64         return sb.toString();
     65     }
     66 
     67     private static String getCodeSpec(final String codesArraySpec) {
     68         final String[] strs = codesArraySpec.split(VERTICAL_BAR_REGEX, -1);
     69         if (strs.length <= 1) {
     70             return codesArraySpec;
     71         }
     72         return TextUtils.isEmpty(strs[1]) ? strs[0] : strs[1];
     73     }
     74 
     75     public static int getMinSupportSdkVersion(final String codesArraySpec) {
     76         final String[] strs = codesArraySpec.split(VERTICAL_BAR_REGEX, -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_REGEX)) {
    102             final int codePoint = Integer.parseInt(codeInHex, BASE_HEX);
    103             sb.appendCodePoint(codePoint);
    104         }
    105         return sb.toString();
    106     }
    107 }
    108