diff --git a/xml/impl/src/com/intellij/javaee/ExternalResourcesRootsProvider.java b/xml/impl/src/com/intellij/javaee/ExternalResourcesRootsProvider.java index d14144f4a086..d4e562525be5 100644 --- a/xml/impl/src/com/intellij/javaee/ExternalResourcesRootsProvider.java +++ b/xml/impl/src/com/intellij/javaee/ExternalResourcesRootsProvider.java @@ -21,8 +21,8 @@ final class ExternalResourcesRootsProvider extends IndexableSetContributor { ExternalResourceManagerExImpl manager = (ExternalResourceManagerExImpl)ExternalResourceManager.getInstance(); Set duplicateCheck = new HashSet<>(); Set set = new HashSet<>(); - for (Map map : manager.getStandardResources$intellij_xml_psi_impl()) { - for (ExternalResourceManagerExImpl.Resource resource : map.values()) { + for (Map map : manager.getStandardResources$intellij_xml_psi_impl()) { + for (ExternalResource resource : map.values()) { String url = resource.getResourceUrl(); if (url != null) { url = url.substring(0, url.lastIndexOf('/') + 1); diff --git a/xml/xml-psi-impl/src/com/intellij/javaee/ExternalResourceManagerExImpl.kt b/xml/xml-psi-impl/src/com/intellij/javaee/ExternalResourceManagerExImpl.kt index 9333281a18ac..808a578c38fa 100644 --- a/xml/xml-psi-impl/src/com/intellij/javaee/ExternalResourceManagerExImpl.kt +++ b/xml/xml-psi-impl/src/com/intellij/javaee/ExternalResourceManagerExImpl.kt @@ -22,7 +22,6 @@ import com.intellij.openapi.util.SystemInfo import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.openapi.vfs.VirtualFile -import com.intellij.openapi.vfs.impl.VirtualFilePointerContainerImpl.URL_ATTR import com.intellij.psi.PsiFile import com.intellij.psi.util.CachedValueProvider import com.intellij.psi.util.CachedValuesManager @@ -30,6 +29,7 @@ import com.intellij.util.ArrayUtilRt import com.intellij.util.concurrency.SynchronizedClearableLazy import com.intellij.util.concurrency.ThreadingAssertions import com.intellij.util.containers.MultiMap +import com.intellij.util.io.URLUtil import com.intellij.xml.Html5SchemaProvider import com.intellij.xml.XmlSchemaProvider import com.intellij.xml.index.XmlNamespaceIndex @@ -48,7 +48,7 @@ private val LOG = logger() private const val DEFAULT_VERSION = "" private const val CATALOG_PROPERTIES_ELEMENT = "CATALOG_PROPERTIES" -private val XSD_1_1 = ExternalResourceManagerExImpl.Resource( +private val XSD_1_1 = ExternalResource( file = "/standardSchemas/XMLSchema-1_1/XMLSchema.xsd", aClass = ExternalResourceManagerExImpl::class.java, classLoader = null, @@ -56,6 +56,7 @@ private val XSD_1_1 = ExternalResourceManagerExImpl.Resource( private const val HTML5_DOCTYPE_ELEMENT = "HTML5" +private const val URL_ATTR: @NonNls String = "url" private const val RESOURCE_ELEMENT: @NonNls String = "resource" private const val LOCATION_ATTR: @NonNls String = "location" private const val IGNORED_RESOURCE_ELEMENT: @NonNls String = "ignored-resource" @@ -104,7 +105,7 @@ open class ExternalResourceManagerExImpl : ExternalResourceManagerEx(), Persiste const val JCP_NS: @NonNls String = "http://xmlns.jcp.org/xml/ns/javaee/" const val JAKARTA_NS: @NonNls String = "https://jakarta.ee/xml/ns/jakartaee/" - fun getOrCreateMap(resources: MutableMap>, version: String?): MutableMap { + internal fun getOrCreateMap(resources: MutableMap>, version: String?): MutableMap { return resources.computeIfAbsent(version ?: DEFAULT_VERSION) { HashMap() } } @@ -117,7 +118,7 @@ open class ExternalResourceManagerExImpl : ExternalResourceManagerEx(), Persiste } } - internal open fun computeStdResources(): Map> { + internal open fun computeStdResources(): Map> { val registrar = ResourceRegistrarImpl() for (provider in StandardResourceProvider.EP_NAME.lazySequence()) { provider.registerResources(registrar) @@ -417,9 +418,13 @@ open class ExternalResourceManagerExImpl : ExternalResourceManagerEx(), Persiste incModificationCount() for (element in state.getChildren(RESOURCE_ELEMENT)) { - val url = element.getAttributeValue(URL_ATTR) - if (!url.isNullOrEmpty()) { - addSilently(url, DEFAULT_VERSION, element.getAttributeValue(LOCATION_ATTR)!!.replace('/', File.separatorChar)) + val url = element.getAttributeValue(URL_ATTR) ?: continue + if (url.isNotEmpty()) { + addSilently( + url = url, + version = DEFAULT_VERSION, + location = element.getAttributeValue(LOCATION_ATTR)!!.replace('/', File.separatorChar), + ) } } @@ -453,18 +458,14 @@ open class ExternalResourceManagerExImpl : ExternalResourceManagerEx(), Persiste incModificationCount() } - internal fun getStandardResources(): Collection> = standardResources.value.values + internal fun getStandardResources(): Collection> = standardResources.value.values override fun getDefaultHtmlDoctype(project: Project): String { val doctype = getProjectResources(project).defaultHtmlDoctype - if (XmlUtil.XHTML_URI == doctype) { - return XmlUtil.XHTML4_SCHEMA_LOCATION - } - else if (HTML5_DOCTYPE_ELEMENT == doctype) { - return Html5SchemaProvider.getHtml5SchemaLocation() - } - else { - return doctype!! + return when { + XmlUtil.XHTML_URI == doctype -> XmlUtil.XHTML4_SCHEMA_LOCATION + HTML5_DOCTYPE_ELEMENT == doctype -> Html5SchemaProvider.getHtml5SchemaLocation() + else -> doctype!! } } @@ -504,70 +505,66 @@ open class ExternalResourceManagerExImpl : ExternalResourceManagerEx(), Persiste incModificationCount() if (Html5SchemaProvider.getHtml5SchemaLocation() == defaultHtmlDoctype) { - this@ExternalResourceManagerExImpl.defaultHtmlDoctype = HTML5_DOCTYPE_ELEMENT + this.defaultHtmlDoctype = HTML5_DOCTYPE_ELEMENT } else { - this@ExternalResourceManagerExImpl.defaultHtmlDoctype = defaultHtmlDoctype + this.defaultHtmlDoctype = defaultHtmlDoctype } fireExternalResourceChanged() } +} - internal class Resource(file: String, aClass: Class<*>?, classLoader: ClassLoader?) { - private val file = file - private val classLoader = classLoader - private val aClass: Class<*>? = aClass +internal class ExternalResource(file: String, aClass: Class<*>?, classLoader: ClassLoader?) { + private val file = file + private val classLoader = classLoader + private val aClass: Class<*>? = aClass - @Volatile - private var resolvedResourcePath: String? = null + @Volatile + private var resolvedResourcePath: String? = null - constructor(file: String, baseResource: Resource) : this(file = file, aClass = baseResource.aClass, classLoader = baseResource.classLoader) + constructor(file: String, baseResource: ExternalResource) : this(file = file, aClass = baseResource.aClass, classLoader = baseResource.classLoader) - fun directoryName(): String { - val i = file.lastIndexOf('/') - return if (i > 0) file.substring(0, i) else file - } - - @Suppress("LoggingSimilarMessage") - fun getResourceUrl(): String? { - val resolvedResourcePath = resolvedResourcePath - if (resolvedResourcePath != null) { - return resolvedResourcePath - } - - val resource = if (aClass == null) classLoader!!.getResource(file) else aClass.getResource(file) - - if (resource == null) { - val message = "Cannot find standard resource. filename:${this@Resource.file} class=$aClass, classLoader:$classLoader" - if (ApplicationManager.getApplication().isUnitTestMode()) { - LOG.warn(message) - } - else { - LOG.warn(message) - } - - this@Resource.resolvedResourcePath = null - return null - } - - var path = FileUtil.unquote(resource.toString()) - // this is done by FileUtil for windows - path = path.replace('\\', '/') - this@Resource.resolvedResourcePath = path - return path - } - - override fun equals(o: Any?): Boolean { - if (this === o) return true - if (o == null || javaClass != o.javaClass) return false - - val resource = o as Resource - return classLoader == resource.classLoader && aClass == resource.aClass && file == resource.file - } - - override fun hashCode(): Int = this@Resource.file.hashCode() - - override fun toString(): String = "${this@Resource.file} for $classLoader" + fun directoryName(): String { + val i = file.lastIndexOf('/') + return if (i > 0) file.substring(0, i) else file } + + @Suppress("LoggingSimilarMessage") + fun getResourceUrl(): String? { + resolvedResourcePath?.let { + return it + } + + val resource = if (aClass == null) classLoader!!.getResource(file) else aClass.getResource(file) + if (resource == null) { + val message = "Cannot find standard resource. filename:${this@ExternalResource.file} class=$aClass, classLoader:$classLoader" + if (ApplicationManager.getApplication().isUnitTestMode()) { + LOG.warn(message) + } + else { + LOG.warn(message) + } + + resolvedResourcePath = null + return null + } + + val path = URLUtil.unescapePercentSequences(resource.toString().replace('\\', '/')) + resolvedResourcePath = path + return path + } + + override fun equals(o: Any?): Boolean { + if (this === o) return true + if (o == null || javaClass != o.javaClass) return false + + val resource = o as ExternalResource + return classLoader == resource.classLoader && aClass == resource.aClass && file == resource.file + } + + override fun hashCode(): Int = this@ExternalResource.file.hashCode() + + override fun toString(): String = "${this@ExternalResource.file} for $classLoader" } private fun getMap(resources: Map>, version: String?): MutableMap? { diff --git a/xml/xml-psi-impl/src/com/intellij/javaee/ProjectResources.java b/xml/xml-psi-impl/src/com/intellij/javaee/ProjectResources.java index b06c06b4c5ef..8501d580e593 100644 --- a/xml/xml-psi-impl/src/com/intellij/javaee/ProjectResources.java +++ b/xml/xml-psi-impl/src/com/intellij/javaee/ProjectResources.java @@ -13,7 +13,7 @@ import java.util.Map; @State(name = "ProjectResources") final class ProjectResources extends ExternalResourceManagerExImpl { @Override - public @NotNull Map<@NotNull String, @NotNull Map<@NotNull String, @NotNull Resource>> computeStdResources$intellij_xml_psi_impl() { + public @NotNull Map<@NotNull String, @NotNull Map<@NotNull String, @NotNull ExternalResource>> computeStdResources$intellij_xml_psi_impl() { return Collections.emptyMap(); } } diff --git a/xml/xml-psi-impl/src/com/intellij/javaee/ResourceRegistrarImpl.kt b/xml/xml-psi-impl/src/com/intellij/javaee/ResourceRegistrarImpl.kt index 0b1e69ee3cfb..7a7907aafbaa 100644 --- a/xml/xml-psi-impl/src/com/intellij/javaee/ResourceRegistrarImpl.kt +++ b/xml/xml-psi-impl/src/com/intellij/javaee/ResourceRegistrarImpl.kt @@ -6,7 +6,7 @@ import java.util.ArrayList import java.util.HashMap class ResourceRegistrarImpl : ResourceRegistrar { - private val resources = HashMap>() + private val resources = HashMap>() private val ignored = ArrayList() internal fun getIgnoredResources(): List = ignored @@ -27,7 +27,7 @@ class ResourceRegistrarImpl : ResourceRegistrar { classLoader: ClassLoader? ) { ExternalResourceManagerExImpl.getOrCreateMap(resources, version) - .put(resource, ExternalResourceManagerExImpl.Resource(file = fileName, aClass = aClass, classLoader = classLoader)) + .put(resource, ExternalResource(file = fileName, aClass = aClass, classLoader = classLoader)) } override fun addStdResource(resource: @NonNls String, version: @NonNls String?, fileName: @NonNls String, aClass: Class<*>?) { @@ -54,14 +54,12 @@ class ResourceRegistrarImpl : ResourceRegistrar { } fun addInternalResource(resource: @NonNls String, version: @NonNls String?, fileName: @NonNls String?, classLoader: ClassLoader) { - addStdResource( - resource = resource, - version = version, - fileName = ExternalResourceManagerEx.STANDARD_SCHEMAS.trimStart('/') + fileName, + ExternalResourceManagerExImpl.getOrCreateMap(resources, version = version).put(resource, ExternalResource( + file = ExternalResourceManagerEx.STANDARD_SCHEMAS.trimStart('/') + fileName, aClass = null, classLoader = classLoader, - ) + )) } - internal fun getResources(): Map> = resources + internal fun getResources(): Map> = resources }