How to I get all xml files in resource
Answered
I need to look for all XML files in 'resource' how to do it properly.
1. I use FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, XmlFileType.INSTANCE, GlobalSearchScope.allScope(project))
That returns all XML files, but how can I find only certain XML first they should be in resources, second it should have a certain doctype
<!DOCTYPE post PUBLIC "//GOOGLE/ >
2. should I save them in memory as a Map (or whatever type) to keep indexes of files for fast looking needed XML or perform search exact XML every time using FileBaseIndex and iterate them again and again
Please sign in to leave a comment.
1. You need to restrict search to specific directories by using proper SearchScope:
model = com.intellij.openapi.roots.ModuleRootManager#getInstance(currentModule)
resourceRoots = model#getSourceRoots(JavaResourceRootType.RESOURCE)
scope = GlobalSearchScopesCore.directoriesScope(module.getProject(), true, resourceRoots.toArray(VirtualFile.EMPTY_ARRAY))
You can use API like com.intellij.psi.search.FileTypeIndex#processFiles(com.intellij.openapi.fileTypes.FileType, com.intellij.util.Processor<? super com.intellij.openapi.vfs.VirtualFile>, com.intellij.psi.search.GlobalSearchScope) to perform the filtering according to DocType or any other criteria.
obtaining DocType:
com.intellij.psi.xml.XmlFile#getDocument -> com.intellij.psi.xml.XmlDocument#getProlog -> com.intellij.psi.xml.XmlProlog#getDoctype
2. This cannot be answered without understanding exact use case and taking some actual performance measurements. Some generic recommendations can be found here https://plugins.jetbrains.com/docs/intellij/performance.html