Ignore some XML tags

This commit is contained in:
Yan Zhulanow
2015-03-25 17:15:09 +03:00
parent 938248bd29
commit 2af538e854
9 changed files with 18 additions and 13 deletions
@@ -33,6 +33,8 @@ public object AndroidConst {
val ID_USAGE_PREFIX = "@id/" val ID_USAGE_PREFIX = "@id/"
val CLEAR_FUNCTION_NAME = "clearFindViewByIdCache" val CLEAR_FUNCTION_NAME = "clearFindViewByIdCache"
val IGNORED_XML_WIDGET_TYPES = setOf("requestFocus", "merge", "tag", "check")
} }
public fun nameToIdDeclaration(name: String): String = AndroidConst.ID_DECLARATION_PREFIX + name public fun nameToIdDeclaration(name: String): String = AndroidConst.ID_DECLARATION_PREFIX + name
@@ -49,7 +51,11 @@ public fun isResourceIdUsage(str: String?): Boolean = str?.startsWith(AndroidCon
public fun isResourceDeclarationOrUsage(id: String?): Boolean = isResourceIdDeclaration(id) || isResourceIdUsage(id) public fun isResourceDeclarationOrUsage(id: String?): Boolean = isResourceIdDeclaration(id) || isResourceIdUsage(id)
public fun isWidgetTypeIgnored(xmlType: String): Boolean {
return (xmlType.isEmpty() || xmlType in AndroidConst.IGNORED_XML_WIDGET_TYPES)
}
public fun getRealWidgetType(xmlType: String): String = when (xmlType) { public fun getRealWidgetType(xmlType: String): String = when (xmlType) {
"fragment", "include", "merge" -> "View" "fragment", "include" -> "View"
else -> xmlType else -> xmlType
} }
@@ -31,6 +31,7 @@ class AndroidXmlHandler(private val elementCallback: (String, String) -> Unit) :
} }
override fun startElement(uri: String, localName: String, qName: String, attributes: Attributes) { override fun startElement(uri: String, localName: String, qName: String, attributes: Attributes) {
if (isWidgetTypeIgnored(localName)) return
val attributesMap = attributes.toMap() val attributesMap = attributes.toMap()
val idAttribute = attributesMap[AndroidConst.ID_ATTRIBUTE_NO_NAMESPACE] val idAttribute = attributesMap[AndroidConst.ID_ATTRIBUTE_NO_NAMESPACE]
val widgetType = getRealWidgetType(attributesMap[AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE] ?: localName) val widgetType = getRealWidgetType(attributesMap[AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE] ?: localName)
@@ -12,12 +12,6 @@ val Activity.includeTag: ft<View, View?>
val Fragment.includeTag: ft<View, View?> val Fragment.includeTag: ft<View, View?>
get() = getView().findViewById(0) : View get() = getView().findViewById(0) : View
val Activity.mergeTag: ft<View, View?>
get() = findViewById(0) : View
val Fragment.mergeTag: ft<View, View?>
get() = getView().findViewById(0) : View
val Activity.fragmentTag: ft<View, View?> val Activity.fragmentTag: ft<View, View?>
get() = findViewById(0) : View get() = findViewById(0) : View
@@ -9,9 +9,6 @@ import kotlin.internal.flexible.ft
val View.includeTag: ft<View, View?> val View.includeTag: ft<View, View?>
get() = findViewById(0) : View get() = findViewById(0) : View
val View.mergeTag: ft<View, View?>
get() = findViewById(0) : View
val View.fragmentTag: ft<View, View?> val View.fragmentTag: ft<View, View?>
get() = findViewById(0) : View get() = findViewById(0) : View
@@ -0,0 +1 @@
package android.appwidget
@@ -0,0 +1 @@
package android.inputmethodservice
@@ -0,0 +1 @@
package android.opengl
@@ -0,0 +1 @@
package android.webkit
@@ -35,14 +35,17 @@ class AndroidXmlVisitor(val elementCallback: (String, String, XmlAttribute) -> U
} }
override fun visitXmlTag(tag: XmlTag?) { override fun visitXmlTag(tag: XmlTag?) {
val localName = tag?.getLocalName() ?: ""
if (isWidgetTypeIgnored(localName)) return
val idAttribute = tag?.getAttribute(AndroidConst.ID_ATTRIBUTE) val idAttribute = tag?.getAttribute(AndroidConst.ID_ATTRIBUTE)
if (idAttribute != null) { if (idAttribute != null) {
val idAttributeValue = idAttribute.getValue() val idAttributeValue = idAttribute.getValue()
if (idAttributeValue != null) { if (idAttributeValue != null) {
val classAttributeValue = tag?.getAttribute(AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE)?.getValue() val classAttributeValue = tag?.getAttribute(AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE)?.getValue()
val xmlType = classAttributeValue ?: tag?.getLocalName() val xmlType = classAttributeValue ?: localName
val widgetType = xmlType?.let { getRealWidgetType(it) } val widgetType = xmlType.let { getRealWidgetType(it) }
if (widgetType != null && isResourceDeclarationOrUsage(idAttributeValue)) { if (isResourceDeclarationOrUsage(idAttributeValue)) {
val name = idToName(idAttributeValue) val name = idToName(idAttributeValue)
if (name != null) elementCallback(name, widgetType, idAttribute) if (name != null) elementCallback(name, widgetType, idAttribute)
} }