From 24d914f8b2135f8c334e68667fc5fd44fabe318a Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 11 Mar 2016 19:49:05 +0100 Subject: [PATCH] [platform] Ivy source provider untied from Properties plugin --- java/idea-ui/idea-ui.iml | 1 - .../jarFinder/IvyAttachSourceProvider.java | 69 ++++++++++--------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/java/idea-ui/idea-ui.iml b/java/idea-ui/idea-ui.iml index 3103d8f1202f..c039e952c519 100644 --- a/java/idea-ui/idea-ui.iml +++ b/java/idea-ui/idea-ui.iml @@ -14,6 +14,5 @@ - \ No newline at end of file diff --git a/java/idea-ui/src/com/intellij/jarFinder/IvyAttachSourceProvider.java b/java/idea-ui/src/com/intellij/jarFinder/IvyAttachSourceProvider.java index 0173f161cec9..f6f3c9b04610 100644 --- a/java/idea-ui/src/com/intellij/jarFinder/IvyAttachSourceProvider.java +++ b/java/idea-ui/src/com/intellij/jarFinder/IvyAttachSourceProvider.java @@ -15,8 +15,6 @@ */ package com.intellij.jarFinder; -import com.intellij.lang.properties.IProperty; -import com.intellij.lang.properties.psi.PropertiesFile; import com.intellij.notification.Notification; import com.intellij.notification.NotificationType; import com.intellij.openapi.diagnostic.Logger; @@ -25,6 +23,7 @@ import com.intellij.openapi.roots.OrderRootType; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.JarFileSystem; +import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; import com.intellij.util.ArrayUtil; @@ -32,9 +31,11 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; +import java.io.StringReader; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Properties; /** * @author Sergey Evdokimov @@ -42,52 +43,37 @@ import java.util.List; public class IvyAttachSourceProvider extends AbstractAttachSourceProvider { private static final Logger LOG = Logger.getInstance(IvyAttachSourceProvider.class); - @Nullable - private static String extractUrl(PropertiesFile properties, String artifactName) { - String prefix = "artifact:" + artifactName + "#source#jar#"; - - for (IProperty property : properties.getProperties()) { - String key = property.getUnescapedKey(); - if (key != null && key.startsWith(prefix) && key.endsWith(".location")) { - return property.getUnescapedValue(); - } - } - - return null; - } - @NotNull @Override - public Collection getActions(List orderEntries, final PsiFile psiFile) { + public Collection getActions(List orderEntries, PsiFile psiFile) { VirtualFile jar = getJarByPsiFile(psiFile); if (jar == null) return Collections.emptyList(); VirtualFile jarsDir = jar.getParent(); if (jarsDir == null || !jarsDir.getName().equals("jars")) return Collections.emptyList(); - String jarNameWithoutExt = jar.getNameWithoutExtension(); - - final VirtualFile artifactDir = jarsDir.getParent(); + VirtualFile artifactDir = jarsDir.getParent(); if (artifactDir == null) return Collections.emptyList(); + String jarNameWithoutExt = jar.getNameWithoutExtension(); String artifactName = artifactDir.getName(); - if (!jarNameWithoutExt.startsWith(artifactName) - || !jarNameWithoutExt.substring(artifactName.length()).startsWith("-")) { + if (!jarNameWithoutExt.startsWith(artifactName) || !jarNameWithoutExt.substring(artifactName.length()).startsWith("-")) { return Collections.emptyList(); } String version = jarNameWithoutExt.substring(artifactName.length() + 1); + //noinspection SpellCheckingInspection VirtualFile propertiesFile = artifactDir.findChild("ivydata-" + version + ".properties"); if (propertiesFile == null) return Collections.emptyList(); - final Library library = getLibraryFromOrderEntriesList(orderEntries); + Library library = getLibraryFromOrderEntriesList(orderEntries); if (library == null) return Collections.emptyList(); - final String sourceFileName = artifactName + '-' + version + "-sources.jar"; + String sourceFileName = artifactName + '-' + version + "-sources.jar"; - final VirtualFile sources = artifactDir.findChild("sources"); + VirtualFile sources = artifactDir.findChild("sources"); if (sources != null) { VirtualFile srcFile = sources.findChild(sourceFileName); if (srcFile != null) { @@ -101,10 +87,7 @@ public class IvyAttachSourceProvider extends AbstractAttachSourceProvider { } } - PsiFile propertiesFileFile = psiFile.getManager().findFile(propertiesFile); - if (!(propertiesFileFile instanceof PropertiesFile)) return Collections.emptyList(); - - final String url = extractUrl((PropertiesFile)propertiesFileFile, artifactName); + String url = extractUrl(propertiesFile, artifactName); if (StringUtil.isEmptyOrSpaces(url)) return Collections.emptyList(); return Collections.singleton(new DownloadSourcesAction(psiFile.getProject(), "Downloading Ivy Sources", url) { @@ -122,14 +105,32 @@ public class IvyAttachSourceProvider extends AbstractAttachSourceProvider { addSourceFile(JarFileSystem.getInstance().getJarRootForLocalFile(srcFile), library); } catch (IOException e) { - new Notification(myMessageGroupId, - "IO Error", - "Failed to save " + artifactDir.getPath() + "/sources/" + sourceFileName, - NotificationType.ERROR) - .notify(myProject); + String message = "Failed to save " + artifactDir.getPath() + "/sources/" + sourceFileName; + new Notification(myMessageGroupId, "IO Error", message, NotificationType.ERROR).notify(myProject); LOG.warn(e); } } }); } + + @Nullable + private static String extractUrl(VirtualFile properties, String artifactName) { + String prefix = "artifact:" + artifactName + "#source#jar#"; + + try { + Properties p = new Properties(); + p.load(new StringReader(VfsUtilCore.loadText(properties))); + for (Object o : p.keySet()) { + String key = o.toString(); + if (key != null && key.startsWith(prefix) && key.endsWith(".location")) { + return p.getProperty(key); + } + } + } + catch (Exception e) { + LOG.debug(properties.getPath(), e); + } + + return null; + } } \ No newline at end of file