Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJarEntryResource;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.JarEntryDirectory;
import org.eclipse.jdt.internal.core.JarEntryFile;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;

public final class ExtUtils {

private static final String JDT_SCHEME = "jdt";

public static final String JDT_SCHEME = "jdt";
private static final String CONTENTS_AUTHORITY = "jarentry";

public static String toUri(JarEntryFile jarEntryFile) {
public static String toUri(IJarEntryResource jarEntryFile) {
IPackageFragmentRoot fragmentRoot = jarEntryFile.getPackageFragmentRoot();
try {
return new URI(JDT_SCHEME, CONTENTS_AUTHORITY, jarEntryFile.getFullPath().toPortableString(), fragmentRoot.getHandleIdentifier(), null).toASCIIString();
Expand All @@ -35,10 +43,79 @@ public static String toUri(JarEntryFile jarEntryFile) {
}
}

public static boolean isJarResourceUri(URI uri) {
return uri != null && JDT_SCHEME.equals(uri.getScheme()) && CONTENTS_AUTHORITY.equals(uri.getAuthority());
}

public static JarEntryFile findJarEntryFile(IPackageFragmentRoot packageRoot, String path) throws JavaModelException {
String[] segments = StringUtils.split(path, "/");
String packageName = StringUtils.join(Arrays.asList(segments).subList(0, segments.length - 1), '.');
IPackageFragment packageFragment = packageRoot.getPackageFragment(packageName);
if (packageFragment != null && packageFragment.exists()) {
Object[] objs = packageFragment.getNonJavaResources();
for (Object obj : objs) {
if (obj instanceof IJarEntryResource) {
IJarEntryResource child = (IJarEntryResource) obj;
if (child instanceof JarEntryFile && child.getFullPath().toPortableString().equals(path)) {
return (JarEntryFile) child;
}
}
}
}
Object[] resources = packageRoot.getNonJavaResources();

for (Object resource : resources) {
if (resource instanceof JarEntryFile) {
JarEntryFile file = (JarEntryFile) resource;
if (file.getFullPath().toPortableString().equals(path)) {
return file;
}
}
if (resource instanceof JarEntryDirectory) {
JarEntryDirectory directory = (JarEntryDirectory) resource;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the first one failed, it will not continue the search in other folders. Is this by design?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

JarEntryFile file = findFileInJar(directory, path);
if (file != null) {
return file;
}
}
}
return null;
}

public static IJarEntryResource getJarEntryResource(URI uri) throws CoreException {
if (uri == null) {
throw new NullPointerException("Cannot get jar resource from null URI.");
}
String handleId = uri.getQuery();
if (handleId == null) {
throw new NullPointerException("Invalid uri for a jar entry.");
}
IPackageFragmentRoot packageRoot = (IPackageFragmentRoot) JavaCore.create(handleId);
if (packageRoot == null) {
throw new CoreException(new Status(IStatus.ERROR, JdtlsExtActivator.PLUGIN_ID, String.format("No package root found for %s", handleId)));
}
return findJarEntryFile(packageRoot, uri.getPath());
}

public static IPath removeProjectSegment(String projectElementName, IPath path) {
if (projectElementName.equals(path.segment(0))) {
return path.removeFirstSegments(1).makeRelative();
}
return path;
}

private static JarEntryFile findFileInJar(JarEntryDirectory directory, String path) {
for (IJarEntryResource child : directory.getChildren()) {
if (child instanceof JarEntryFile && child.getFullPath().toPortableString().equals(path)) {
return (JarEntryFile) child;
}
if (child instanceof JarEntryDirectory) {
JarEntryFile file = findFileInJar((JarEntryDirectory) child, path);
if (file != null) {
return file;
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJarEntryResource;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.core.JarEntryDirectory;
import org.eclipse.jdt.internal.core.JarEntryFile;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.jdt.ls.core.internal.IContentProvider;
Expand All @@ -49,40 +44,12 @@ private String getContent(String rootId, String path, IProgressMonitor pm) {
if (packageRoot == null) {
throw new CoreException(new Status(IStatus.ERROR, JdtlsExtActivator.PLUGIN_ID, String.format("No package root found for %s", rootId)));
}
if (packageRoot instanceof JarPackageFragmentRoot) {
Object[] resources = packageRoot.getNonJavaResources();

for (Object resource : resources) {
if (resource instanceof JarEntryFile) {
JarEntryFile file = (JarEntryFile) resource;
if (file.getFullPath().toPortableString().equals(path)) {
return readFileContent(file);
}
}
if (resource instanceof JarEntryDirectory) {
JarEntryDirectory directory = (JarEntryDirectory) resource;
JarEntryFile file = findFileInJar(directory, path);
if (file != null) {
return readFileContent(file);
}
}
}
// if the file exists in the java packages
String[] segments = StringUtils.split(path, "/");
String packageName = StringUtils.join(Arrays.asList(segments).subList(0, segments.length - 1), '.');
IPackageFragment packageFragment = packageRoot.getPackageFragment(packageName);
if (packageFragment != null && packageFragment.exists()) {

Object[] objs = packageFragment.getNonJavaResources();
for (Object obj : objs) {
if (obj instanceof IJarEntryResource) {
IJarEntryResource child = (IJarEntryResource) obj;
if (child instanceof JarEntryFile && child.getFullPath().toPortableString().equals(path)) {
return readFileContent((JarEntryFile) child);
}
}

}
if (packageRoot instanceof JarPackageFragmentRoot) {
JarEntryFile fileEntry = ExtUtils.findJarEntryFile(packageRoot, path);
if (fileEntry != null) {
return readFileContent(fileEntry);
}

}
Expand All @@ -92,20 +59,7 @@ private String getContent(String rootId, String path, IProgressMonitor pm) {
return null;
}

private static JarEntryFile findFileInJar(JarEntryDirectory directory, String path) {
for (IJarEntryResource child : directory.getChildren()) {
if (child instanceof JarEntryFile && child.getFullPath().toPortableString().equals(path)) {
return (JarEntryFile) child;
}
if (child instanceof JarEntryDirectory) {
JarEntryFile file = findFileInJar((JarEntryDirectory) child, path);
if (file != null) {
return file;
}
}
}
return null;
}


private static String readFileContent(JarEntryFile file) throws CoreException {
try (InputStream stream = (file.getContents())) {
Expand Down
Loading