Clean up XmlParserUtils

Change-Id: I636e8f359d47f39e61a7f77e3e686f2721779b6f
main
Tadashi G. Takaoka 2013-03-26 12:03:07 +09:00
parent 5f976d1cef
commit d17b69ec00
3 changed files with 30 additions and 27 deletions

View File

@ -301,8 +301,8 @@ public final class KeyboardLayoutSet {
final int xmlId = mResources.getIdentifier(keyboardLayoutSetName, "xml", packageName); final int xmlId = mResources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
try { try {
parseKeyboardLayoutSet(mResources, xmlId); parseKeyboardLayoutSet(mResources, xmlId);
} catch (Exception e) { } catch (final Exception e) {
throw new RuntimeException(e.getMessage() + " in " + keyboardLayoutSetName); throw new RuntimeException(e.getMessage() + " in " + keyboardLayoutSetName, e);
} }
return new KeyboardLayoutSet(mContext, mParams); return new KeyboardLayoutSet(mContext, mParams);
} }
@ -318,7 +318,7 @@ public final class KeyboardLayoutSet {
if (TAG_KEYBOARD_SET.equals(tag)) { if (TAG_KEYBOARD_SET.equals(tag)) {
parseKeyboardLayoutSetContent(parser); parseKeyboardLayoutSetContent(parser);
} else { } else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD_SET); throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD_SET);
} }
} }
} }
@ -336,14 +336,14 @@ public final class KeyboardLayoutSet {
if (TAG_ELEMENT.equals(tag)) { if (TAG_ELEMENT.equals(tag)) {
parseKeyboardLayoutSetElement(parser); parseKeyboardLayoutSetElement(parser);
} else { } else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD_SET); throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD_SET);
} }
} else if (event == XmlPullParser.END_TAG) { } else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName(); final String tag = parser.getName();
if (TAG_KEYBOARD_SET.equals(tag)) { if (TAG_KEYBOARD_SET.equals(tag)) {
break; break;
} else { } else {
throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEYBOARD_SET); throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_KEYBOARD_SET);
} }
} }
} }

View File

@ -220,7 +220,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
parseKeyboardContent(parser, false); parseKeyboardContent(parser, false);
break; break;
} else { } else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD); throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD);
} }
} }
} }
@ -321,7 +321,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_KEY_STYLE.equals(tag)) { } else if (TAG_KEY_STYLE.equals(tag)) {
parseKeyStyle(parser, skip); parseKeyStyle(parser, skip);
} else { } else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_ROW); throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_ROW);
} }
} else if (event == XmlPullParser.END_TAG) { } else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName(); final String tag = parser.getName();
@ -333,7 +333,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
|| TAG_MERGE.equals(tag)) { || TAG_MERGE.equals(tag)) {
break; break;
} else { } else {
throw new XmlParseUtils.IllegalEndTag(parser, TAG_ROW); throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_ROW);
} }
} }
} }
@ -345,10 +345,10 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
R.styleable.Keyboard); R.styleable.Keyboard);
try { try {
if (a.hasValue(R.styleable.Keyboard_horizontalGap)) { if (a.hasValue(R.styleable.Keyboard_horizontalGap)) {
throw new XmlParseUtils.IllegalAttribute(parser, "horizontalGap"); throw new XmlParseUtils.IllegalAttribute(parser, TAG_ROW, "horizontalGap");
} }
if (a.hasValue(R.styleable.Keyboard_verticalGap)) { if (a.hasValue(R.styleable.Keyboard_verticalGap)) {
throw new XmlParseUtils.IllegalAttribute(parser, "verticalGap"); throw new XmlParseUtils.IllegalAttribute(parser, TAG_ROW, "verticalGap");
} }
return new KeyboardRow(mResources, mParams, parser, mCurrentY); return new KeyboardRow(mResources, mParams, parser, mCurrentY);
} finally { } finally {
@ -373,7 +373,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_KEY_STYLE.equals(tag)) { } else if (TAG_KEY_STYLE.equals(tag)) {
parseKeyStyle(parser, skip); parseKeyStyle(parser, skip);
} else { } else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEY); throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_ROW);
} }
} else if (event == XmlPullParser.END_TAG) { } else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName(); final String tag = parser.getName();
@ -387,7 +387,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
|| TAG_MERGE.equals(tag)) { || TAG_MERGE.equals(tag)) {
break; break;
} else { } else {
throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEY); throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_ROW);
} }
} }
} }
@ -548,7 +548,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_DEFAULT.equals(tag)) { } else if (TAG_DEFAULT.equals(tag)) {
selected |= parseDefault(parser, row, selected ? true : skip); selected |= parseDefault(parser, row, selected ? true : skip);
} else { } else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEY); throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_SWITCH);
} }
} else if (event == XmlPullParser.END_TAG) { } else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName(); final String tag = parser.getName();
@ -556,7 +556,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
if (DEBUG) endTag("</%s>", TAG_SWITCH); if (DEBUG) endTag("</%s>", TAG_SWITCH);
break; break;
} else { } else {
throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEY); throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_SWITCH);
} }
} }
} }

View File

@ -30,50 +30,53 @@ public final class XmlParseUtils {
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static class ParseException extends XmlPullParserException { public static class ParseException extends XmlPullParserException {
public ParseException(String msg, XmlPullParser parser) { public ParseException(final String msg, final XmlPullParser parser) {
super(msg + " at " + parser.getPositionDescription()); super(msg + " at " + parser.getPositionDescription());
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static final class IllegalStartTag extends ParseException { public static final class IllegalStartTag extends ParseException {
public IllegalStartTag(XmlPullParser parser, String parent) { public IllegalStartTag(final XmlPullParser parser, final String tag, final String parent) {
super("Illegal start tag " + parser.getName() + " in " + parent, parser); super("Illegal start tag " + tag + " in " + parent, parser);
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static final class IllegalEndTag extends ParseException { public static final class IllegalEndTag extends ParseException {
public IllegalEndTag(XmlPullParser parser, String parent) { public IllegalEndTag(final XmlPullParser parser, final String tag, final String parent) {
super("Illegal end tag " + parser.getName() + " in " + parent, parser); super("Illegal end tag " + tag + " in " + parent, parser);
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static final class IllegalAttribute extends ParseException { public static final class IllegalAttribute extends ParseException {
public IllegalAttribute(XmlPullParser parser, String attribute) { public IllegalAttribute(final XmlPullParser parser, final String tag,
super("Tag " + parser.getName() + " has illegal attribute " + attribute, parser); final String attribute) {
super("Tag " + tag + " has illegal attribute " + attribute, parser);
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static final class NonEmptyTag extends ParseException{ public static final class NonEmptyTag extends ParseException{
public NonEmptyTag(String tag, XmlPullParser parser) { public NonEmptyTag(final XmlPullParser parser, final String tag) {
super(tag + " must be empty tag", parser); super(tag + " must be empty tag", parser);
} }
} }
public static void checkEndTag(String tag, XmlPullParser parser) public static void checkEndTag(final String tag, final XmlPullParser parser)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName())) if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName()))
return; return;
throw new NonEmptyTag(tag, parser); throw new NonEmptyTag(parser, tag);
} }
public static void checkAttributeExists(TypedArray attr, int attrId, String attrName, public static void checkAttributeExists(final TypedArray attr, final int attrId,
String tag, XmlPullParser parser) throws XmlPullParserException { final String attrName, final String tag, final XmlPullParser parser)
if (attr.hasValue(attrId)) throws XmlPullParserException {
if (attr.hasValue(attrId)) {
return; return;
}
throw new ParseException( throw new ParseException(
"No " + attrName + " attribute found in <" + tag + "/>", parser); "No " + attrName + " attribute found in <" + tag + "/>", parser);
} }