13
13
14
14
import java .net .URI ;
15
15
import java .net .URISyntaxException ;
16
+ import java .util .Arrays ;
16
17
18
+ import org .apache .commons .lang3 .StringUtils ;
19
+ import org .eclipse .core .runtime .CoreException ;
17
20
import org .eclipse .core .runtime .IPath ;
21
+ import org .eclipse .core .runtime .IStatus ;
22
+ import org .eclipse .core .runtime .Status ;
23
+ import org .eclipse .jdt .core .IJarEntryResource ;
24
+ import org .eclipse .jdt .core .IPackageFragment ;
18
25
import org .eclipse .jdt .core .IPackageFragmentRoot ;
26
+ import org .eclipse .jdt .core .JavaCore ;
27
+ import org .eclipse .jdt .core .JavaModelException ;
28
+ import org .eclipse .jdt .internal .core .JarEntryDirectory ;
19
29
import org .eclipse .jdt .internal .core .JarEntryFile ;
20
30
import org .eclipse .jdt .ls .core .internal .JavaLanguageServerPlugin ;
21
31
22
32
public final class ExtUtils {
23
-
24
- private static final String JDT_SCHEME = "jdt" ;
25
-
33
+ public static final String JDT_SCHEME = "jdt" ;
26
34
private static final String CONTENTS_AUTHORITY = "jarentry" ;
27
35
28
- public static String toUri (JarEntryFile jarEntryFile ) {
36
+ public static String toUri (IJarEntryResource jarEntryFile ) {
29
37
IPackageFragmentRoot fragmentRoot = jarEntryFile .getPackageFragmentRoot ();
30
38
try {
31
39
return new URI (JDT_SCHEME , CONTENTS_AUTHORITY , jarEntryFile .getFullPath ().toPortableString (), fragmentRoot .getHandleIdentifier (), null ).toASCIIString ();
@@ -35,10 +43,79 @@ public static String toUri(JarEntryFile jarEntryFile) {
35
43
}
36
44
}
37
45
46
+ public static boolean isJarResourceUri (URI uri ) {
47
+ return uri != null && JDT_SCHEME .equals (uri .getScheme ()) && CONTENTS_AUTHORITY .equals (uri .getAuthority ());
48
+ }
49
+
50
+ public static JarEntryFile findJarEntryFile (IPackageFragmentRoot packageRoot , String path ) throws JavaModelException {
51
+ String [] segments = StringUtils .split (path , "/" );
52
+ String packageName = StringUtils .join (Arrays .asList (segments ).subList (0 , segments .length - 1 ), '.' );
53
+ IPackageFragment packageFragment = packageRoot .getPackageFragment (packageName );
54
+ if (packageFragment != null && packageFragment .exists ()) {
55
+ Object [] objs = packageFragment .getNonJavaResources ();
56
+ for (Object obj : objs ) {
57
+ if (obj instanceof IJarEntryResource ) {
58
+ IJarEntryResource child = (IJarEntryResource ) obj ;
59
+ if (child instanceof JarEntryFile && child .getFullPath ().toPortableString ().equals (path )) {
60
+ return (JarEntryFile ) child ;
61
+ }
62
+ }
63
+ }
64
+ }
65
+ Object [] resources = packageRoot .getNonJavaResources ();
66
+
67
+ for (Object resource : resources ) {
68
+ if (resource instanceof JarEntryFile ) {
69
+ JarEntryFile file = (JarEntryFile ) resource ;
70
+ if (file .getFullPath ().toPortableString ().equals (path )) {
71
+ return file ;
72
+ }
73
+ }
74
+ if (resource instanceof JarEntryDirectory ) {
75
+ JarEntryDirectory directory = (JarEntryDirectory ) resource ;
76
+ JarEntryFile file = findFileInJar (directory , path );
77
+ if (file != null ) {
78
+ return file ;
79
+ }
80
+ }
81
+ }
82
+ return null ;
83
+ }
84
+
85
+ public static IJarEntryResource getJarEntryResource (URI uri ) throws CoreException {
86
+ if (uri == null ) {
87
+ throw new NullPointerException ("Cannot get jar resource from null URI." );
88
+ }
89
+ String handleId = uri .getQuery ();
90
+ if (handleId == null ) {
91
+ throw new NullPointerException ("Invalid uri for a jar entry." );
92
+ }
93
+ IPackageFragmentRoot packageRoot = (IPackageFragmentRoot ) JavaCore .create (handleId );
94
+ if (packageRoot == null ) {
95
+ throw new CoreException (new Status (IStatus .ERROR , JdtlsExtActivator .PLUGIN_ID , String .format ("No package root found for %s" , handleId )));
96
+ }
97
+ return findJarEntryFile (packageRoot , uri .getPath ());
98
+ }
99
+
38
100
public static IPath removeProjectSegment (String projectElementName , IPath path ) {
39
101
if (projectElementName .equals (path .segment (0 ))) {
40
102
return path .removeFirstSegments (1 ).makeRelative ();
41
103
}
42
104
return path ;
43
105
}
106
+
107
+ private static JarEntryFile findFileInJar (JarEntryDirectory directory , String path ) {
108
+ for (IJarEntryResource child : directory .getChildren ()) {
109
+ if (child instanceof JarEntryFile && child .getFullPath ().toPortableString ().equals (path )) {
110
+ return (JarEntryFile ) child ;
111
+ }
112
+ if (child instanceof JarEntryDirectory ) {
113
+ JarEntryFile file = findFileInJar ((JarEntryDirectory ) child , path );
114
+ if (file != null ) {
115
+ return file ;
116
+ }
117
+ }
118
+ }
119
+ return null ;
120
+ }
44
121
}
0 commit comments