replace sax with psi based android ui xml parsing in plugin

This commit is contained in:
dedoz
2014-07-26 21:29:35 +04:00
committed by Yan Zhulanow
parent c24fccc659
commit 9899e78329
4 changed files with 89 additions and 24 deletions
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.plugin.android
import com.intellij.psi.XmlElementVisitor
import com.intellij.psi.PsiElement
import com.intellij.psi.xml.XmlElement
import com.intellij.psi.xml.XmlTag
class AndroidXmlVisitor(val elementCallback: (String, String)-> Unit): XmlElementVisitor() {
override fun visitElement(element: PsiElement) {
element.acceptChildren(this)
}
override fun visitXmlElement(element: XmlElement?) {
element?.acceptChildren(this)
}
override fun visitXmlTag(tag: XmlTag?) {
val idPrefix = "@+id/"
val attribute = tag?.getAttribute("android:id")
if (attribute != null && attribute.getValue() != null) {
elementCallback(attribute.getValue()!!.replace(idPrefix, ""), tag!!.getLocalName())
}
tag?.acceptChildren(this)
}
}
@@ -22,6 +22,9 @@ import com.intellij.openapi.vfs.VirtualFileManager
import java.util.ArrayList
import com.intellij.openapi.vfs.VirtualFileAdapter
import com.intellij.openapi.vfs.VirtualFileEvent
import com.intellij.psi.PsiFile
import org.jetbrains.jet.lang.resolve.android.AndroidWidget
import org.jetbrains.jet.lang.resolve.android.KotlinStringWriter
class IDEAndroidUIXmlParser(val project: Project): AndroidUIXmlParser() {
override val searchPath: String? = project.getBasePath() + "/res/layout/"
@@ -33,5 +36,11 @@ class IDEAndroidUIXmlParser(val project: Project): AndroidUIXmlParser() {
populateQueue(project)
listenerSetUp = true
}
override fun parseSingleFileImpl(file: PsiFile): String {
val ids: MutableCollection<AndroidWidget> = ArrayList()
file.accept(AndroidXmlVisitor({ id, wClass -> ids.add(AndroidWidget(id, wClass))}))
return produceKotlinProperties(KotlinStringWriter(), ids).toString()
}
}