Fix WrongIdFormat exception on Android XML widgets with non-@+id/ format

This commit is contained in:
Yan Zhulanow
2015-03-10 22:01:03 +03:00
parent 07381b765f
commit be954f2c27
7 changed files with 51 additions and 7 deletions
@@ -32,14 +32,12 @@ public object AndroidConst {
val ID_USAGE_PREFIX = "@id/"
}
class WrongIdFormat(id: String) : Exception("Id \"$id\" has wrong format")
public fun nameToIdDeclaration(name: String): String = AndroidConst.ID_DECLARATION_PREFIX + name
public fun idToName(id: String): String {
public fun idToName(id: String): String? {
return if (isResourceIdDeclaration(id)) id.replace(AndroidConst.ID_DECLARATION_PREFIX, "")
else if (isResourceIdUsage(id)) id.replace(AndroidConst.ID_USAGE_PREFIX, "")
else throw WrongIdFormat(id)
else null
}
public fun isResourceIdDeclaration(str: String?): Boolean = str?.startsWith(AndroidConst.ID_DECLARATION_PREFIX) ?: false
@@ -37,7 +37,10 @@ class AndroidXmlHandler(
val attributesMap = attributes.toMap()
val idAttr = attributesMap[AndroidConst.ID_ATTRIBUTE_NO_NAMESPACE]
val classNameAttr = attributesMap[AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE] ?: localName
if (isResourceDeclarationOrUsage(idAttr)) elementCallback(idToName(idAttr), classNameAttr)
if (isResourceDeclarationOrUsage(idAttr)) {
val name = idToName(idAttr)
if (name != null) elementCallback(name, classNameAttr)
}
}
override fun endElement(uri: String?, localName: String, qName: String) {