[maven] IDEA-360609 convert MavenDomUtil to kotlin

GitOrigin-RevId: b99befa6b0e340431a8f9cd36de53077c013ad6a
This commit is contained in:
Dmitry Kichinsky
2025-02-11 17:00:26 +00:00
committed by intellij-monorepo-bot
parent 5614cf3547
commit 399629f76c
@@ -1,489 +1,524 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.idea.maven.dom;
package org.jetbrains.idea.maven.dom
import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlElement;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xml.*;
import com.intellij.util.xml.reflect.DomCollectionChildDescription;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.dom.model.*;
import org.jetbrains.idea.maven.model.MavenConstants;
import org.jetbrains.idea.maven.model.MavenCoordinate;
import org.jetbrains.idea.maven.model.MavenId;
import org.jetbrains.idea.maven.model.MavenResource;
import org.jetbrains.idea.maven.plugins.groovy.MavenGroovyPomCompletionContributor;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
import org.jetbrains.idea.maven.server.MavenDistribution;
import org.jetbrains.idea.maven.server.MavenDistributionsCache;
import org.jetbrains.idea.maven.utils.MavenLog;
import org.jetbrains.idea.maven.utils.MavenUtil;
import com.intellij.lang.properties.IProperty
import com.intellij.lang.properties.psi.PropertiesFile
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.util.Comparing
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.Pair
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.xml.XmlFile
import com.intellij.psi.xml.XmlTag
import com.intellij.testFramework.LightVirtualFile
import com.intellij.util.ObjectUtils
import com.intellij.util.containers.ContainerUtil
import com.intellij.util.xml.*
import org.jetbrains.idea.maven.dom.model.*
import org.jetbrains.idea.maven.model.MavenConstants
import org.jetbrains.idea.maven.model.MavenCoordinate
import org.jetbrains.idea.maven.model.MavenId
import org.jetbrains.idea.maven.model.MavenResource
import org.jetbrains.idea.maven.plugins.groovy.MavenGroovyPomCompletionContributor
import org.jetbrains.idea.maven.project.MavenProject
import org.jetbrains.idea.maven.project.MavenProjectsManager
import org.jetbrains.idea.maven.server.MavenDistribution
import org.jetbrains.idea.maven.server.MavenDistributionsCache
import org.jetbrains.idea.maven.utils.MavenLog
import org.jetbrains.idea.maven.utils.MavenUtil.isPomFileName
import java.util.regex.Pattern
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class MavenDomUtil {
private static final Key<Pair<Long, Set<VirtualFile>>> FILTERED_RESOURCES_ROOTS_KEY = Key.create("MavenDomUtil.FILTERED_RESOURCES_ROOTS");
object MavenDomUtil {
private val FILTERED_RESOURCES_ROOTS_KEY = Key.create<Pair<Long?, MutableSet<VirtualFile?>?>?>("MavenDomUtil.FILTERED_RESOURCES_ROOTS")
// see http://maven.apache.org/settings.html
private static final Set<String> SUBTAGS_IN_SETTINGS_FILE = ContainerUtil.newHashSet("localRepository", "interactiveMode",
"usePluginRegistry", "offline", "pluginGroups",
"servers", "mirrors", "proxies", "profiles",
"activeProfiles");
private static final Pattern XML_TAG_NAME_PATTERN = Pattern.compile("(\\S*)\\[(\\d*)\\]\\z");
private val SUBTAGS_IN_SETTINGS_FILE: MutableSet<String?> = ContainerUtil.newHashSet<String?>("localRepository", "interactiveMode",
"usePluginRegistry", "offline",
"pluginGroups",
"servers", "mirrors", "proxies", "profiles",
"activeProfiles")
private val XML_TAG_NAME_PATTERN: Pattern = Pattern.compile("(\\S*)\\[(\\d*)\\]\\z")
public static boolean isMavenFile(PsiFile file) {
return isProjectFile(file) || isProfilesFile(file) || isSettingsFile(file);
@JvmStatic
fun isMavenFile(file: PsiFile): Boolean {
return isProjectFile(file) || isProfilesFile(file) || isSettingsFile(file)
}
public static boolean isProjectFile(PsiFile file) {
if (!(file instanceof XmlFile)) return false;
@JvmStatic
fun isProjectFile(file: PsiFile?): Boolean {
if (file !is XmlFile) return false
XmlTag rootTag = ((XmlFile)file).getRootTag();
if (rootTag == null || !"project".equals(rootTag.getName())) return false;
val rootTag = file.getRootTag()
if (rootTag == null || "project" != rootTag.getName()) return false
String xmlns = rootTag.getAttributeValue("xmlns");
val xmlns = rootTag.getAttributeValue("xmlns")
if (xmlns != null && xmlns.startsWith("http://maven.apache.org/POM/")) {
return true;
return true
}
return MavenUtil.isPomFileName(file.getName());
return isPomFileName(file.getName())
}
public static @Nullable @NlsSafe String getXmlProjectModelVersion(PsiFile file) {
if (!(file instanceof XmlFile)) return null;
@JvmStatic
fun getXmlProjectModelVersion(file: PsiFile?): @NlsSafe String? {
if (file !is XmlFile) return null
XmlTag rootTag = ((XmlFile)file).getRootTag();
if (rootTag == null || !"project".equals(rootTag.getName())) return null;
val rootTag = file.getRootTag()
if (rootTag == null || "project" != rootTag.getName()) return null
return rootTag.getSubTagText("modelVersion");
return rootTag.getSubTagText("modelVersion")
}
public static boolean isProfilesFile(PsiFile file) {
if (!(file instanceof XmlFile)) return false;
@JvmStatic
fun isProfilesFile(file: PsiFile?): Boolean {
if (file !is XmlFile) return false
return MavenConstants.PROFILES_XML.equals(file.getName());
return MavenConstants.PROFILES_XML == file.getName()
}
public static @Nullable @NlsSafe String getXmlSettingsNameSpace(PsiFile file) {
if (!(file instanceof XmlFile)) return null;
@JvmStatic
fun getXmlSettingsNameSpace(file: PsiFile?): @NlsSafe String? {
if (file !is XmlFile) return null
XmlTag rootTag = ((XmlFile)file).getRootTag();
if (rootTag == null || !"settings".equals(rootTag.getName())) return null;
val rootTag = file.getRootTag()
if (rootTag == null || "settings" != rootTag.getName()) return null
return rootTag.getAttributeValue("xmlns");
return rootTag.getAttributeValue("xmlns")
}
public static boolean isSettingsFile(PsiFile file) {
if (!(file instanceof XmlFile)) return false;
@JvmStatic
fun isSettingsFile(file: PsiFile?): Boolean {
if (file !is XmlFile) return false
XmlTag rootTag = ((XmlFile)file).getRootTag();
if (rootTag == null || !"settings".equals(rootTag.getName())) return false;
val rootTag = file.getRootTag()
if (rootTag == null || "settings" != rootTag.getName()) return false
String xmlns = rootTag.getAttributeValue("xmlns");
val xmlns = rootTag.getAttributeValue("xmlns")
if (xmlns != null) {
return xmlns.contains("maven");
return xmlns.contains("maven")
}
boolean hasTag = false;
var hasTag = false
for (PsiElement e = rootTag.getFirstChild(); e != null; e = e.getNextSibling()) {
if (e instanceof XmlTag) {
if (SUBTAGS_IN_SETTINGS_FILE.contains(((XmlTag)e).getName())) return true;
hasTag = true;
var e = rootTag.getFirstChild()
while (e != null) {
if (e is XmlTag) {
if (SUBTAGS_IN_SETTINGS_FILE.contains(e.getName())) return true
hasTag = true
}
e = e.getNextSibling()
}
return !hasTag;
return !hasTag
}
public static boolean isMavenFile(PsiElement element) {
return isMavenFile(element.getContainingFile());
@JvmStatic
fun isMavenFile(element: PsiElement): Boolean {
val file = element.getContainingFile() ?: return false
return isMavenFile(file)
}
public static @Nullable Module findContainingMavenizedModule(@NotNull PsiFile psiFile) {
VirtualFile file = psiFile.getVirtualFile();
if (file == null) return null;
fun findContainingMavenizedModule(psiFile: PsiFile): Module? {
val file = psiFile.getVirtualFile()
if (file == null) return null
Project project = psiFile.getProject();
val project = psiFile.getProject()
MavenProjectsManager manager = MavenProjectsManager.getInstance(project);
if (!manager.isMavenizedProject()) return null;
val manager = MavenProjectsManager.getInstance(project)
if (!manager.isMavenizedProject) return null
ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
val index = ProjectRootManager.getInstance(project).getFileIndex()
Module module = index.getModuleForFile(file);
if (module == null || !manager.isMavenizedModule(module)) return null;
return module;
val module = index.getModuleForFile(file)
if (module == null || !manager.isMavenizedModule(module)) return null
return module
}
public static boolean isMavenProperty(PsiElement target) {
XmlTag tag = PsiTreeUtil.getParentOfType(target, XmlTag.class, false);
if (tag == null) return false;
return DomUtil.findDomElement(tag, MavenDomProperties.class) != null;
@JvmStatic
fun isMavenProperty(target: PsiElement?): Boolean {
val tag = PsiTreeUtil.getParentOfType<XmlTag?>(target, XmlTag::class.java, false)
if (tag == null) return false
return DomUtil.findDomElement<MavenDomProperties?>(tag, MavenDomProperties::class.java) != null
}
public static String calcRelativePath(VirtualFile parent, VirtualFile child) {
String result = FileUtil.getRelativePath(parent.getPath(), child.getPath(), '/');
@JvmStatic
fun calcRelativePath(parent: VirtualFile, child: VirtualFile): String {
var result = FileUtil.getRelativePath(parent.getPath(), child.getPath(), '/')
if (result == null) {
MavenLog.LOG.warn("cannot calculate relative path for\nparent: " + parent + "\nchild: " + child);
result = child.getPath();
MavenLog.LOG.warn("cannot calculate relative path for\nparent: $parent\nchild: $child")
result = child.getPath()
}
return FileUtil.toSystemIndependentName(result);
return FileUtil.toSystemIndependentName(result)
}
public static MavenDomParent updateMavenParent(MavenDomProjectModel mavenModel, MavenProject parentProject) {
MavenDomParent result = mavenModel.getMavenParent();
@JvmStatic
fun updateMavenParent(mavenModel: MavenDomProjectModel, parentProject: MavenProject): MavenDomParent {
val result = mavenModel.getMavenParent()
VirtualFile pomFile = DomUtil.getFile(mavenModel).getVirtualFile();
Project project = mavenModel.getManager().getProject();
val pomFile = DomUtil.getFile(mavenModel).getVirtualFile()
val project = mavenModel.getManager().getProject()
MavenId parentId = parentProject.getMavenId();
result.getGroupId().setStringValue(parentId.getGroupId());
result.getArtifactId().setStringValue(parentId.getArtifactId());
result.getVersion().setStringValue(parentId.getVersion());
val parentId = parentProject.mavenId
result.getGroupId().setStringValue(parentId.groupId)
result.getArtifactId().setStringValue(parentId.artifactId)
result.getVersion().setStringValue(parentId.version)
if (!Comparing.equal(pomFile.getParent().getParent(), parentProject.getDirectoryFile())
|| !FileUtil.namesEqual(MavenConstants.POM_XML, parentProject.getFile().getName())) {
result.getRelativePath().setValue(PsiManager.getInstance(project).findFile(parentProject.getFile()));
if (!Comparing.equal<VirtualFile?>(pomFile.getParent().getParent(), parentProject.directoryFile)
|| !FileUtil.namesEqual(MavenConstants.POM_XML, parentProject.file.getName())
) {
result.getRelativePath().setValue(PsiManager.getInstance(project).findFile(parentProject.file))
}
return result;
return result
}
public static @Nullable VirtualFile getVirtualFile(@NotNull DomElement element) {
PsiFile psiFile = DomUtil.getFile(element);
return doGetVirtualFile(psiFile);
@JvmStatic
fun getVirtualFile(element: DomElement): VirtualFile? {
val psiFile: PsiFile = DomUtil.getFile(element)
return doGetVirtualFile(psiFile)
}
public static @Nullable VirtualFile getVirtualFile(@NotNull PsiElement element) {
PsiFile psiFile = element.getContainingFile();
return doGetVirtualFile(psiFile);
@JvmStatic
fun getVirtualFile(element: PsiElement): VirtualFile? {
val psiFile = element.getContainingFile()
return doGetVirtualFile(psiFile)
}
private static @Nullable VirtualFile doGetVirtualFile(PsiFile psiFile) {
if (psiFile == null) return null;
psiFile = psiFile.getOriginalFile();
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile instanceof LightVirtualFile) {
virtualFile = ObjectUtils.chooseNotNull(psiFile.getUserData(MavenGroovyPomCompletionContributor.ORIGINAL_POM_FILE), virtualFile);
private fun doGetVirtualFile(psiFile: PsiFile?): VirtualFile? {
var psiFile = psiFile
if (psiFile == null) return null
psiFile = psiFile.getOriginalFile()
var virtualFile = psiFile.getVirtualFile()
if (virtualFile is LightVirtualFile) {
virtualFile = ObjectUtils.chooseNotNull<VirtualFile>(
psiFile.getUserData<VirtualFile?>(MavenGroovyPomCompletionContributor.ORIGINAL_POM_FILE), virtualFile)
}
return virtualFile;
return virtualFile
}
public static @Nullable MavenProject findProject(@NotNull MavenDomProjectModel projectDom) {
XmlElement element = projectDom.getXmlElement();
if (element == null) return null;
@JvmStatic
fun findProject(projectDom: MavenDomProjectModel): MavenProject? {
val element = projectDom.getXmlElement()
if (element == null) return null
VirtualFile file = getVirtualFile(element);
if (file == null) return null;
MavenProjectsManager manager = MavenProjectsManager.getInstance(element.getProject());
return manager.findProject(file);
val file = getVirtualFile(element)
if (file == null) return null
val manager = MavenProjectsManager.getInstance(element.getProject())
return manager.findProject(file)
}
public static @Nullable MavenProject findContainingProject(@NotNull DomElement element) {
PsiElement psi = element.getXmlElement();
return psi == null ? null : findContainingProject(psi);
@JvmStatic
fun findContainingProject(element: DomElement): MavenProject? {
val psi: PsiElement? = element.getXmlElement()
return if (psi == null) null else findContainingProject(psi)
}
public static @Nullable MavenProject findContainingProject(@NotNull PsiElement element) {
VirtualFile file = getVirtualFile(element);
if (file == null) return null;
MavenProjectsManager manager = MavenProjectsManager.getInstance(element.getProject());
return manager.findContainingProject(file);
@JvmStatic
fun findContainingProject(element: PsiElement): MavenProject? {
val file = getVirtualFile(element)
if (file == null) return null
val manager = MavenProjectsManager.getInstance(element.getProject())
return manager.findContainingProject(file)
}
public static @Nullable MavenDomProjectModel getMavenDomProjectModel(@NotNull Project project, @NotNull VirtualFile file) {
return getMavenDomModel(project, file, MavenDomProjectModel.class);
@JvmStatic
fun getMavenDomProjectModel(project: Project, file: VirtualFile): MavenDomProjectModel? {
return getMavenDomModel<MavenDomProjectModel>(project, file, MavenDomProjectModel::class.java)
}
public static @Nullable MavenDomProfiles getMavenDomProfilesModel(@NotNull Project project, @NotNull VirtualFile file) {
MavenDomProfilesModel model = getMavenDomModel(project, file, MavenDomProfilesModel.class);
if (model != null) return model.getProfiles();
return getMavenDomModel(project, file, MavenDomProfiles.class); // try old-style model
@JvmStatic
fun getMavenDomProfilesModel(project: Project, file: VirtualFile): MavenDomProfiles? {
val model = getMavenDomModel<MavenDomProfilesModel>(project, file, MavenDomProfilesModel::class.java)
if (model != null) return model.getProfiles()
return getMavenDomModel<MavenDomProfiles>(project, file, MavenDomProfiles::class.java) // try an old-style model
}
public static @Nullable <T extends MavenDomElement> T getMavenDomModel(@NotNull Project project,
@NotNull VirtualFile file,
@NotNull Class<T> clazz) {
if (!file.isValid()) return null;
PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
if (psiFile == null) return null;
return getMavenDomModel(psiFile, clazz);
@JvmStatic
fun <T : MavenDomElement?> getMavenDomModel(
project: Project,
file: VirtualFile,
clazz: Class<T>
): T? {
if (!file.isValid()) return null
val psiFile = PsiManager.getInstance(project).findFile(file)
if (psiFile == null) return null
return getMavenDomModel<T>(psiFile, clazz)
}
public static @Nullable <T extends MavenDomElement> T getMavenDomModel(@NotNull PsiFile file, @NotNull Class<T> clazz) {
DomFileElement<T> fileElement = getMavenDomFile(file, clazz);
return fileElement == null ? null : fileElement.getRootElement();
@JvmStatic
fun <T : MavenDomElement?> getMavenDomModel(file: PsiFile, clazz: Class<T>): T? {
val fileElement = getMavenDomFile<T>(file, clazz)
return fileElement?.getRootElement()
}
private static @Nullable <T extends MavenDomElement> DomFileElement<T> getMavenDomFile(@NotNull PsiFile file, @NotNull Class<T> clazz) {
if (!(file instanceof XmlFile)) return null;
return DomManager.getDomManager(file.getProject()).getFileElement((XmlFile)file, clazz);
private fun <T : MavenDomElement?> getMavenDomFile(file: PsiFile, clazz: Class<T>): DomFileElement<T?>? {
if (file !is XmlFile) return null
return DomManager.getDomManager(file.getProject()).getFileElement<T?>(file, clazz)
}
public static @Nullable XmlTag findTag(@NotNull DomElement domElement, @NotNull String path) {
List<String> elements = StringUtil.split(path, ".");
if (elements.isEmpty()) return null;
@JvmStatic
fun findTag(domElement: DomElement, path: String): XmlTag? {
val elements = StringUtil.split(path, ".")
if (elements.isEmpty()) return null
Pair<String, Integer> nameAndIndex = translateTagName(elements.get(0));
String name = nameAndIndex.first;
Integer index = nameAndIndex.second;
var nameAndIndex = translateTagName(elements[0])
var name = nameAndIndex!!.first
var index = nameAndIndex.second
XmlTag result = domElement.getXmlTag();
if (result == null || !name.equals(result.getName())) return null;
result = getIndexedTag(result, index);
var result = domElement.getXmlTag()
if (result == null || name != result.getName()) return null
result = getIndexedTag(result, index)
for (String each : elements.subList(1, elements.size())) {
nameAndIndex = translateTagName(each);
name = nameAndIndex.first;
index = nameAndIndex.second;
for (each in elements.subList(1, elements.size)) {
nameAndIndex = translateTagName(each)
name = nameAndIndex!!.first
index = nameAndIndex.second
result = result.findFirstSubTag(name);
if (result == null) return null;
result = getIndexedTag(result, index);
result = result!!.findFirstSubTag(name)
if (result == null) return null
result = getIndexedTag(result, index)
}
return result;
return result
}
private static Pair<String, Integer> translateTagName(String text) {
String tagName = text.trim();
Integer index = null;
private fun translateTagName(text: String): Pair<String, Int?>? {
var tagName = text.trim { it <= ' ' }
var index: Int? = null
Matcher matcher = XML_TAG_NAME_PATTERN.matcher(tagName);
val matcher = XML_TAG_NAME_PATTERN.matcher(tagName)
if (matcher.find()) {
tagName = matcher.group(1);
tagName = matcher.group(1)
try {
index = Integer.parseInt(matcher.group(2));
index = matcher.group(2).toInt()
}
catch (NumberFormatException e) {
return null;
catch (_: NumberFormatException) {
return null
}
}
return Pair.create(tagName, index);
return Pair.create<String?, Int?>(tagName, index)
}
private static XmlTag getIndexedTag(XmlTag parent, Integer index) {
if (index == null) return parent;
private fun getIndexedTag(parent: XmlTag, index: Int?): XmlTag? {
if (index == null) return parent
XmlTag[] children = parent.getSubTags();
if (index < 0 || index >= children.length) return null;
return children[index];
val children = parent.getSubTags()
if (index < 0 || index >= children.size) return null
return children[index]
}
public static @Nullable PropertiesFile getPropertiesFile(@NotNull Project project, @NotNull VirtualFile file) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
if (!(psiFile instanceof PropertiesFile)) return null;
return (PropertiesFile)psiFile;
@JvmStatic
fun getPropertiesFile(project: Project, file: VirtualFile): PropertiesFile? {
val psiFile = PsiManager.getInstance(project).findFile(file)
if (psiFile !is PropertiesFile) return null
return psiFile as PropertiesFile
}
public static @Nullable IProperty findProperty(@NotNull Project project, @NotNull VirtualFile file, @NotNull String propName) {
PropertiesFile propertiesFile = getPropertiesFile(project, file);
return propertiesFile == null ? null : propertiesFile.findPropertyByKey(propName);
@JvmStatic
fun findProperty(project: Project, file: VirtualFile, propName: String): IProperty? {
val propertiesFile = getPropertiesFile(project, file)
return propertiesFile?.findPropertyByKey(propName)
}
public static @Nullable PsiElement findPropertyValue(@NotNull Project project, @NotNull VirtualFile file, @NotNull String propName) {
IProperty prop = findProperty(project, file, propName);
return prop == null ? null : prop.getPsiElement().getFirstChild().getNextSibling().getNextSibling();
fun findPropertyValue(project: Project, file: VirtualFile, propName: String): PsiElement? {
val prop = findProperty(project, file, propName)
return prop?.getPsiElement()?.getFirstChild()?.getNextSibling()?.getNextSibling()
}
private static @Nullable Set<VirtualFile> getFilteredResourcesRoots(@NotNull MavenProject mavenProject) {
Pair<Long, Set<VirtualFile>> cachedValue = mavenProject.getCachedValue(FILTERED_RESOURCES_ROOTS_KEY);
private fun getFilteredResourcesRoots(mavenProject: MavenProject): MutableSet<VirtualFile?>? {
var cachedValue = mavenProject.getCachedValue<Pair<Long?, MutableSet<VirtualFile?>?>?>(FILTERED_RESOURCES_ROOTS_KEY)
if (cachedValue == null || cachedValue.first != VirtualFileManager.getInstance().getModificationCount()) {
Set<VirtualFile> set = null;
var set: MutableSet<VirtualFile?>? = null
for (MavenResource resource : ContainerUtil.concat(mavenProject.getResources(), mavenProject.getTestResources())) {
if (!resource.isFiltered()) continue;
for (resource in ContainerUtil.concat<MavenResource>(mavenProject.resources, mavenProject.testResources)) {
if (!resource.isFiltered) continue
String resourceDirectory = resource.getDirectory();
VirtualFile resourceDir = LocalFileSystem.getInstance().findFileByPath(resourceDirectory);
if (resourceDir == null) continue;
val resourceDirectory = resource.directory
val resourceDir = LocalFileSystem.getInstance().findFileByPath(resourceDirectory)
if (resourceDir == null) continue
if (set == null) {
set = new HashSet<>();
set = HashSet<VirtualFile?>()
}
set.add(resourceDir);
set.add(resourceDir)
}
if (set == null) {
set = Collections.emptySet();
set = mutableSetOf<VirtualFile?>()
}
cachedValue = Pair.create(VirtualFileManager.getInstance().getModificationCount(), set);
mavenProject.putCachedValue(FILTERED_RESOURCES_ROOTS_KEY, cachedValue);
cachedValue = Pair.create<Long?, MutableSet<VirtualFile?>?>(VirtualFileManager.getInstance().getModificationCount(), set)
mavenProject.putCachedValue<Pair<Long?, MutableSet<VirtualFile?>?>?>(FILTERED_RESOURCES_ROOTS_KEY, cachedValue)
}
return cachedValue.second;
return cachedValue.second
}
public static boolean isFilteredResourceFile(PsiElement element) {
PsiFile psiFile = element.getContainingFile();
VirtualFile file = doGetVirtualFile(psiFile);
if (file == null) return false;
@JvmStatic
fun isFilteredResourceFile(element: PsiElement): Boolean {
val psiFile = element.getContainingFile()
val file = doGetVirtualFile(psiFile)
if (file == null) return false
MavenProjectsManager manager = MavenProjectsManager.getInstance(psiFile.getProject());
MavenProject mavenProject = manager.findContainingProject(file);
if (mavenProject == null) return false;
val manager = MavenProjectsManager.getInstance(psiFile!!.getProject())
val mavenProject = manager.findContainingProject(file)
if (mavenProject == null) return false
Set<VirtualFile> filteredRoots = getFilteredResourcesRoots(mavenProject);
val filteredRoots = getFilteredResourcesRoots(mavenProject)
if (!filteredRoots.isEmpty()) {
for (VirtualFile f = file.getParent(); f != null; f = f.getParent()) {
if (!filteredRoots!!.isEmpty()) {
var f = file.getParent()
while (f != null) {
if (filteredRoots.contains(f)) {
return true;
return true
}
f = f.getParent()
}
}
return false;
return false
}
public static List<DomFileElement<MavenDomProjectModel>> collectProjectModels(Project p) {
return DomService.getInstance().getFileElements(MavenDomProjectModel.class, p, GlobalSearchScope.projectScope(p));
@JvmStatic
fun collectProjectModels(p: Project): MutableList<DomFileElement<MavenDomProjectModel?>?> {
return DomService.getInstance().getFileElements<MavenDomProjectModel?>(MavenDomProjectModel::class.java, p,
GlobalSearchScope.projectScope(p))
}
public static @NotNull MavenId describe(PsiFile psiFile) {
MavenDomProjectModel model = getMavenDomModel(psiFile, MavenDomProjectModel.class);
@JvmStatic
fun describe(psiFile: PsiFile): MavenId {
val model = getMavenDomModel<MavenDomProjectModel>(psiFile, MavenDomProjectModel::class.java)
if (model == null) {
return new MavenId(null, null, null);
return MavenId(null, null, null)
}
String groupId = model.getGroupId().getStringValue();
String artifactId = model.getArtifactId().getStringValue();
String version = model.getVersion().getStringValue();
var groupId = model.getGroupId().getStringValue()
val artifactId = model.getArtifactId().getStringValue()
var version = model.getVersion().getStringValue()
if (groupId == null) {
groupId = model.getMavenParent().getGroupId().getStringValue();
groupId = model.getMavenParent().getGroupId().getStringValue()
}
if (version == null) {
version = model.getMavenParent().getVersion().getStringValue();
version = model.getMavenParent().getVersion().getStringValue()
}
return new MavenId(groupId, artifactId, version);
return MavenId(groupId, artifactId, version)
}
public static @NotNull MavenDomDependency createDomDependency(MavenDomProjectModel model,
@Nullable Editor editor,
final @NotNull MavenId id) {
return createDomDependency(model.getDependencies(), editor, id);
@JvmStatic
fun createDomDependency(
model: MavenDomProjectModel,
editor: Editor?,
id: MavenId
): MavenDomDependency {
return createDomDependency(model.getDependencies(), editor, id)
}
public static @NotNull MavenDomDependency createDomDependency(MavenDomDependencies dependencies,
@Nullable Editor editor,
final @NotNull MavenCoordinate id) {
MavenDomDependency dep = createDomDependency(dependencies, editor);
@JvmStatic
fun createDomDependency(
dependencies: MavenDomDependencies,
editor: Editor?,
id: MavenCoordinate
): MavenDomDependency {
val dep = createDomDependency(dependencies, editor)
dep.getGroupId().setStringValue(id.getGroupId());
dep.getArtifactId().setStringValue(id.getArtifactId());
dep.getVersion().setStringValue(id.getVersion());
dep.getGroupId().setStringValue(id.getGroupId())
dep.getArtifactId().setStringValue(id.getArtifactId())
dep.getVersion().setStringValue(id.getVersion())
return dep;
return dep
}
public static @NotNull MavenDomDependency createDomDependency(@NotNull MavenDomProjectModel model, @Nullable Editor editor) {
return createDomDependency(model.getDependencies(), editor);
@JvmStatic
fun createDomDependency(model: MavenDomProjectModel, editor: Editor?): MavenDomDependency {
return createDomDependency(model.getDependencies(), editor)
}
public static @NotNull MavenDomDependency createDomDependency(@NotNull MavenDomDependencies dependencies, @Nullable Editor editor) {
int index = getCollectionIndex(dependencies, editor);
@JvmStatic
fun createDomDependency(dependencies: MavenDomDependencies, editor: Editor?): MavenDomDependency {
val index = getCollectionIndex(dependencies, editor)
if (index >= 0) {
DomCollectionChildDescription childDescription = dependencies.getGenericInfo().getCollectionChildDescription("dependency");
val childDescription = dependencies.getGenericInfo().getCollectionChildDescription("dependency")
if (childDescription != null) {
DomElement element = childDescription.addValue(dependencies, index);
if (element instanceof MavenDomDependency) {
return (MavenDomDependency)element;
val element = childDescription.addValue(dependencies, index)
if (element is MavenDomDependency) {
return element
}
}
}
return dependencies.addDependency();
return dependencies.addDependency()
}
public static int getCollectionIndex(final @NotNull MavenDomDependencies dependencies, final @Nullable Editor editor) {
fun getCollectionIndex(dependencies: MavenDomDependencies, editor: Editor?): Int {
if (editor != null) {
int offset = editor.getCaretModel().getOffset();
val offset = editor.getCaretModel().offset
List<MavenDomDependency> dependencyList = dependencies.getDependencies();
val dependencyList = dependencies.getDependencies()
for (int i = 0; i < dependencyList.size(); i++) {
MavenDomDependency dependency = dependencyList.get(i);
XmlElement xmlElement = dependency.getXmlElement();
for (i in dependencyList.indices) {
val dependency = dependencyList[i]
val xmlElement = dependency.getXmlElement()
if (xmlElement != null && xmlElement.getTextRange().getStartOffset() >= offset) {
return i;
if (xmlElement != null && xmlElement.getTextRange().startOffset >= offset) {
return i
}
}
}
return -1;
return -1
}
public static @NotNull String getProjectName(MavenDomProjectModel model) {
MavenProject mavenProject = findProject(model);
@JvmStatic
fun getProjectName(model: MavenDomProjectModel): String {
val mavenProject = findProject(model)
if (mavenProject != null) {
return mavenProject.getDisplayName();
return mavenProject.displayName
}
else {
String name = model.getName().getStringValue();
if (!StringUtil.isEmptyOrSpaces(name)) {
return name;
val name = model.getName().getStringValue()
if (!name.isNullOrBlank()) {
return name
}
else {
return "pom.xml"; // ?
return "pom.xml" // ?
}
}
}
public static @Nullable String getMavenVersion(@Nullable VirtualFile file, @NotNull Project project) {
VirtualFile directory = file == null ? null : file.getParent();
MavenDistribution distribution;
@JvmStatic
fun getMavenVersion(file: VirtualFile?, project: Project): String? {
val directory = file?.getParent()
val distribution: MavenDistribution?
if (directory == null) {
distribution = MavenDistributionsCache.getInstance(project).getSettingsDistribution();
distribution = MavenDistributionsCache.getInstance(project).getSettingsDistribution()
}
else {
distribution = MavenDistributionsCache.getInstance(project).getMavenDistribution(directory.getPath());
distribution = MavenDistributionsCache.getInstance(project).getMavenDistribution(directory.getPath())
}
return distribution.getVersion();
return distribution.version
}
public static boolean isAtLeastMaven4(VirtualFile file, Project project) {
return StringUtil.compareVersionNumbers(getMavenVersion(file, project), "4") >= 0;
@JvmStatic
fun isAtLeastMaven4(file: VirtualFile?, project: Project): Boolean {
return StringUtil.compareVersionNumbers(getMavenVersion(file, project), "4") >= 0
}
}