/*
* 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;
import com.android.inputmethod.latin.Constants;
import android.text.TextUtils;
/**
* The string parser of codesArray specification for
* codePointInHex[,codePoint2InHex]*(|outputTextCodePointInHex[,outputTextCodePoint2InHex]*)? **/ // TODO: Write unit tests for this class. public final class CodesArrayParser { // Constants for parsing. private static final char COMMA = ','; private static final String VERTICAL_BAR_STRING = "\\|"; private static final String COMMA_STRING = ","; private static final int BASE_HEX = 16; private CodesArrayParser() { // This utility class is not publicly instantiable. } private static String getLabelSpec(final String codesArraySpec) { final String[] strs = codesArraySpec.split(VERTICAL_BAR_STRING, -1); if (strs.length <= 1) { return codesArraySpec; } return strs[0]; } public static String parseLabel(final String codesArraySpec) { final String labelSpec = getLabelSpec(codesArraySpec); final StringBuilder sb = new StringBuilder(); for (final String codeInHex : labelSpec.split(COMMA_STRING)) { final int codePoint = Integer.parseInt(codeInHex, BASE_HEX); sb.appendCodePoint(codePoint); } return sb.toString(); } private static String getCodeSpec(final String codesArraySpec) { final String[] strs = codesArraySpec.split(VERTICAL_BAR_STRING, -1); if (strs.length <= 1) { return codesArraySpec; } return TextUtils.isEmpty(strs[1]) ? strs[0] : strs[1]; } // codesArraySpec consists of: //