diff --git a/java/java-impl/src/com/intellij/jarFinder/MavenCentralSourceSearcher.java b/java/java-impl/src/com/intellij/jarFinder/MavenCentralSourceSearcher.java index 7e4ed5639904..32a7c892e47e 100644 --- a/java/java-impl/src/com/intellij/jarFinder/MavenCentralSourceSearcher.java +++ b/java/java-impl/src/com/intellij/jarFinder/MavenCentralSourceSearcher.java @@ -1,11 +1,10 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.jarFinder; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.vfs.VirtualFile; import org.jdom.Element; -import org.jdom.JDOMException; -import org.jdom.xpath.XPath; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -35,8 +34,7 @@ public class MavenCentralSourceSearcher extends SourceSearcher { url += "g:%22" + groupId + "%22%20AND%20"; } url += "a:%22" + artifactId + "%22%20AND%20v:%22" + version + "%22%20AND%20l:%22sources%22"; - @SuppressWarnings("unchecked") - List artifactList = (List)XPath.newInstance("/response/result/doc/str[@name='g']").selectNodes(readDocumentCancelable(indicator, url)); + List artifactList = findElements("./result/doc/str[@name='g']", readElementCancelable(indicator, url)); if (artifactList.isEmpty()) { return null; } @@ -54,10 +52,6 @@ public class MavenCentralSourceSearcher extends SourceSearcher { return null; } } - catch (JDOMException e) { - LOG.warn(e); - throw new SourceSearchException("Failed to parse response from server. See log for more details."); - } catch (IOException e) { indicator.checkCanceled(); // Cause of IOException may be canceling of operation. diff --git a/java/java-impl/src/com/intellij/jarFinder/SonatypeSourceSearcher.java b/java/java-impl/src/com/intellij/jarFinder/SonatypeSourceSearcher.java index 76d713055a4a..c3b114094e2b 100644 --- a/java/java-impl/src/com/intellij/jarFinder/SonatypeSourceSearcher.java +++ b/java/java-impl/src/com/intellij/jarFinder/SonatypeSourceSearcher.java @@ -1,11 +1,10 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.jarFinder; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.vfs.VirtualFile; import org.jdom.Element; -import org.jdom.JDOMException; -import org.jdom.xpath.XPath; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -37,13 +36,12 @@ public class SonatypeSourceSearcher extends SourceSearcher { url += ("&g=" + groupId); } - List artifactList = (List)XPath.newInstance("/searchNGResponse/data/artifact").selectNodes(readDocumentCancelable(indicator, url)); + List artifactList = findElements("./data/artifact", readElementCancelable(indicator, url)); if (artifactList.isEmpty()) { return null; } Element element; - if (artifactList.size() == 1) { element = artifactList.get(0); } @@ -52,9 +50,7 @@ public class SonatypeSourceSearcher extends SourceSearcher { return null; } - List artifactHintList = - (List)XPath.newInstance("artifactHits/artifactHit/artifactLinks/artifactLink/classifier[text()='sources']/../../..") - .selectNodes(element); + List artifactHintList = findElements("artifactHits/artifactHit/artifactLinks/artifactLink/classifier[text()='sources']/../../..", element); if (artifactHintList.isEmpty()) { return null; } @@ -68,10 +64,6 @@ public class SonatypeSourceSearcher extends SourceSearcher { artifactId + "&v=" + version + "&e=jar&c=sources"; } - catch (JDOMException e) { - LOG.warn(e); - throw new SourceSearchException("Failed to parse response from server. See log for more details."); - } catch (IOException e) { indicator.checkCanceled(); // Cause of IOException may be canceling of operation. diff --git a/java/java-impl/src/com/intellij/jarFinder/SourceSearcher.java b/java/java-impl/src/com/intellij/jarFinder/SourceSearcher.java index 93dfc22d37c8..a55577635340 100644 --- a/java/java-impl/src/com/intellij/jarFinder/SourceSearcher.java +++ b/java/java-impl/src/com/intellij/jarFinder/SourceSearcher.java @@ -9,11 +9,14 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.io.HttpRequests; import org.jdom.Element; import org.jdom.JDOMException; +import org.jdom.filter2.Filters; +import org.jdom.xpath.XPathFactory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.util.Enumeration; +import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -21,9 +24,15 @@ import java.util.jar.JarFile; * @author Sergey Evdokimov */ public abstract class SourceSearcher { - private static final String MAVEN_POM_ENTRY_PREFIX = "META-INF/maven/"; + @NotNull + protected static List findElements(@NotNull String expression, @NotNull Element element) { + return XPathFactory.instance() + .compile(expression, Filters.element()) + .evaluate(element); + } + /** * @param indicator * @param artifactId @@ -52,7 +61,7 @@ public abstract class SourceSearcher { } @NotNull - protected static Element readDocumentCancelable(final ProgressIndicator indicator, String url) throws IOException { + protected static Element readElementCancelable(final ProgressIndicator indicator, String url) throws IOException { return HttpRequests.request(url) .accept("application/xml") .connect(new HttpRequests.RequestProcessor() { @@ -88,7 +97,6 @@ public abstract class SourceSearcher { } class SourceSearchException extends Exception { - SourceSearchException(String message) { super(message); }