Refactor KeySpecParser a bit

Change-Id: I1208746f42eb92f937812149033398d8318f9c4e
This commit is contained in:
Tadashi G. Takaoka 2014-02-05 17:39:07 +09:00
parent 258971daac
commit bc9514032a

View file

@ -56,10 +56,9 @@ public final class KeySpecParser {
return keySpec.startsWith(KeyboardIconsSet.PREFIX_ICON); return keySpec.startsWith(KeyboardIconsSet.PREFIX_ICON);
} }
private static boolean hasCode(final String keySpec) { private static boolean hasCode(final String keySpec, final int labelEnd) {
final int end = indexOfLabelEnd(keySpec, 0); if (labelEnd > 0 && labelEnd + 1 < keySpec.length()
if (end > 0 && end + 1 < keySpec.length() && keySpec.startsWith( && keySpec.startsWith(KeyboardCodesSet.PREFIX_CODE, labelEnd + 1)) {
KeyboardCodesSet.PREFIX_CODE, end + 1)) {
return true; return true;
} }
return false; return false;
@ -84,16 +83,16 @@ public final class KeySpecParser {
return sb.toString(); return sb.toString();
} }
private static int indexOfLabelEnd(final String keySpec, final int start) { private static int indexOfLabelEnd(final String keySpec) {
if (keySpec.indexOf(BACKSLASH, start) < 0) { if (keySpec.indexOf(BACKSLASH) < 0) {
final int end = keySpec.indexOf(VERTICAL_BAR, start); final int labelEnd = keySpec.indexOf(VERTICAL_BAR);
if (end == 0) { if (labelEnd == 0) {
throw new KeySpecParserError(VERTICAL_BAR + " at " + start + ": " + keySpec); throw new KeySpecParserError("Empty label");
} }
return end; return labelEnd;
} }
final int length = keySpec.length(); final int length = keySpec.length();
for (int pos = start; pos < length; pos++) { for (int pos = 0; pos < length; pos++) {
final char c = keySpec.charAt(pos); final char c = keySpec.charAt(pos);
if (c == BACKSLASH && pos + 1 < length) { if (c == BACKSLASH && pos + 1 < length) {
// Skip escape char // Skip escape char
@ -105,35 +104,47 @@ public final class KeySpecParser {
return -1; return -1;
} }
private static String getBeforeLabelEnd(final String keySpec, final int labelEnd) {
return (labelEnd < 0) ? keySpec : keySpec.substring(0, labelEnd);
}
private static String getAfterLabelEnd(final String keySpec, final int labelEnd) {
return keySpec.substring(labelEnd + /* VERTICAL_BAR */1);
}
private static void checkDoubleLabelEnd(final String keySpec, final int labelEnd) {
if (indexOfLabelEnd(getAfterLabelEnd(keySpec, labelEnd)) < 0) {
return;
}
throw new KeySpecParserError("Multiple " + VERTICAL_BAR + ": " + keySpec);
}
public static String getLabel(final String keySpec) { public static String getLabel(final String keySpec) {
if (hasIcon(keySpec)) { if (hasIcon(keySpec)) {
return null; return null;
} }
final int end = indexOfLabelEnd(keySpec, 0); final int labelEnd = indexOfLabelEnd(keySpec);
final String label = (end > 0) ? parseEscape(keySpec.substring(0, end)) final String label = parseEscape(getBeforeLabelEnd(keySpec, labelEnd));
: parseEscape(keySpec);
if (label.isEmpty()) { if (label.isEmpty()) {
throw new KeySpecParserError("Empty label: " + keySpec); throw new KeySpecParserError("Empty label: " + keySpec);
} }
return label; return label;
} }
private static String getOutputTextInternal(final String keySpec) { private static String getOutputTextInternal(final String keySpec, final int labelEnd) {
final int end = indexOfLabelEnd(keySpec, 0); if (labelEnd <= 0) {
if (end <= 0) {
return null; return null;
} }
if (indexOfLabelEnd(keySpec, end + 1) >= 0) { checkDoubleLabelEnd(keySpec, labelEnd);
throw new KeySpecParserError("Multiple " + VERTICAL_BAR + ": " + keySpec); return parseEscape(getAfterLabelEnd(keySpec, labelEnd));
}
return parseEscape(keySpec.substring(end + /* VERTICAL_BAR */1));
} }
public static String getOutputText(final String keySpec) { public static String getOutputText(final String keySpec) {
if (hasCode(keySpec)) { final int labelEnd = indexOfLabelEnd(keySpec);
if (hasCode(keySpec, labelEnd)) {
return null; return null;
} }
final String outputText = getOutputTextInternal(keySpec); final String outputText = getOutputTextInternal(keySpec, labelEnd);
if (outputText != null) { if (outputText != null) {
if (StringUtils.codePointCount(outputText) == 1) { if (StringUtils.codePointCount(outputText) == 1) {
// If output text is one code point, it should be treated as a code. // If output text is one code point, it should be treated as a code.
@ -154,14 +165,12 @@ public final class KeySpecParser {
} }
public static int getCode(final String keySpec, final KeyboardCodesSet codesSet) { public static int getCode(final String keySpec, final KeyboardCodesSet codesSet) {
if (hasCode(keySpec)) { final int labelEnd = indexOfLabelEnd(keySpec);
final int end = indexOfLabelEnd(keySpec, 0); if (hasCode(keySpec, labelEnd)) {
if (indexOfLabelEnd(keySpec, end + 1) >= 0) { checkDoubleLabelEnd(keySpec, labelEnd);
throw new KeySpecParserError("Multiple " + VERTICAL_BAR + ": " + keySpec); return parseCode(getAfterLabelEnd(keySpec, labelEnd), codesSet, CODE_UNSPECIFIED);
} }
return parseCode(keySpec.substring(end + 1), codesSet, CODE_UNSPECIFIED); final String outputText = getOutputTextInternal(keySpec, labelEnd);
}
final String outputText = getOutputTextInternal(keySpec);
if (outputText != null) { if (outputText != null) {
// If output text is one code point, it should be treated as a code. // If output text is one code point, it should be treated as a code.
// See {@link #getOutputText(String)}. // See {@link #getOutputText(String)}.
@ -171,36 +180,36 @@ public final class KeySpecParser {
return CODE_OUTPUT_TEXT; return CODE_OUTPUT_TEXT;
} }
final String label = getLabel(keySpec); final String label = getLabel(keySpec);
// Code is automatically generated for one letter label. if (label == null) {
if (StringUtils.codePointCount(label) == 1) { throw new KeySpecParserError("Empty label: " + keySpec);
return label.codePointAt(0);
} }
return CODE_OUTPUT_TEXT; // Code is automatically generated for one letter label.
return (StringUtils.codePointCount(label) == 1) ? label.codePointAt(0) : CODE_OUTPUT_TEXT;
} }
public static int parseCode(final String text, final KeyboardCodesSet codesSet, public static int parseCode(final String text, final KeyboardCodesSet codesSet,
final int defCode) { final int defCode) {
if (text == null) return defCode; if (text == null) {
return defCode;
}
if (text.startsWith(KeyboardCodesSet.PREFIX_CODE)) { if (text.startsWith(KeyboardCodesSet.PREFIX_CODE)) {
return codesSet.getCode(text.substring(KeyboardCodesSet.PREFIX_CODE.length())); return codesSet.getCode(text.substring(KeyboardCodesSet.PREFIX_CODE.length()));
} else if (text.startsWith(PREFIX_HEX)) {
return Integer.parseInt(text.substring(PREFIX_HEX.length()), 16);
} else {
return Integer.parseInt(text);
} }
if (text.startsWith(PREFIX_HEX)) {
return Integer.parseInt(text.substring(PREFIX_HEX.length()), 16);
}
return Integer.parseInt(text);
} }
public static int getIconId(final String keySpec) { public static int getIconId(final String keySpec) {
if (keySpec != null && hasIcon(keySpec)) { if (!hasIcon(keySpec)) {
final int end = keySpec.indexOf(
VERTICAL_BAR, KeyboardIconsSet.PREFIX_ICON.length());
final String name = (end < 0)
? keySpec.substring(KeyboardIconsSet.PREFIX_ICON.length())
: keySpec.substring(KeyboardIconsSet.PREFIX_ICON.length(), end);
return KeyboardIconsSet.getIconId(name);
}
return KeyboardIconsSet.ICON_UNDEFINED; return KeyboardIconsSet.ICON_UNDEFINED;
} }
final int labelEnd = indexOfLabelEnd(keySpec);
final String iconName = getBeforeLabelEnd(keySpec, labelEnd)
.substring(KeyboardIconsSet.PREFIX_ICON.length());
return KeyboardIconsSet.getIconId(iconName);
}
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static final class KeySpecParserError extends RuntimeException { public static final class KeySpecParserError extends RuntimeException {