1010import org .jd .gui .api .API ;
1111import org .jd .gui .api .model .Container ;
1212import org .jd .gui .service .preferencespanel .MavenOrgSourceLoaderPreferencesProvider ;
13- import org .jd .gui .spi .PreferencesPanel ;
1413import org .jd .gui .spi .SourceLoader ;
1514import org .jd .gui .util .exception .ExceptionUtil ;
1615
17- import javax .swing .*;
18- import javax .swing .event .DocumentEvent ;
19- import javax .swing .event .DocumentListener ;
2016import javax .xml .stream .XMLInputFactory ;
2117import javax .xml .stream .XMLStreamConstants ;
2218import javax .xml .stream .XMLStreamReader ;
23- import javax .xml .transform .Source ;
24- import java .awt .*;
25- import java .awt .event .ActionEvent ;
26- import java .awt .event .ActionListener ;
2719import java .io .*;
2820import java .net .URL ;
29- import java .nio .file .Path ;
3021import java .security .DigestInputStream ;
3122import java .security .MessageDigest ;
3223import java .util .*;
33- import java .util .List ;
34- import java .util .regex .Pattern ;
3524import java .util .zip .ZipEntry ;
3625import java .util .zip .ZipInputStream ;
3726
@@ -55,7 +44,7 @@ public String getSource(API api, Container.Entry entry) {
5544 }
5645
5746 if (accepted (filters , entry .getPath ())) {
58- return search (entry , cache .get (entry .getContainer ().getRoot ().getParent ()));
47+ return searchSource (entry , cache .get (entry .getContainer ().getRoot ().getParent ()));
5948 }
6049 }
6150
@@ -64,8 +53,6 @@ public String getSource(API api, Container.Entry entry) {
6453
6554 @ Override
6655 public String loadSource (API api , Container .Entry entry ) {
67- boolean activated = !"false" .equals (api .getPreferences ().get (MavenOrgSourceLoaderPreferencesProvider .ACTIVATED ));
68-
6956 if (isActivated (api )) {
7057 String filters = api .getPreferences ().get (MavenOrgSourceLoaderPreferencesProvider .FILTERS );
7158
@@ -74,7 +61,7 @@ public String loadSource(API api, Container.Entry entry) {
7461 }
7562
7663 if (accepted (filters , entry .getPath ())) {
77- return search (entry , load (entry .getContainer ().getRoot ().getParent ()));
64+ return searchSource (entry , downloadSourceJarFile (entry .getContainer ().getRoot ().getParent ()));
7865 }
7966 }
8067
@@ -83,53 +70,49 @@ public String loadSource(API api, Container.Entry entry) {
8370
8471 @ Override
8572 public File loadSourceFile (API api , Container .Entry entry ) {
86- return isActivated (api ) ? load (entry ) : null ;
73+ return isActivated (api ) ? downloadSourceJarFile (entry ) : null ;
8774 }
8875
8976 private static boolean isActivated (API api ) {
9077 return !"false" .equals (api .getPreferences ().get (MavenOrgSourceLoaderPreferencesProvider .ACTIVATED ));
9178 }
9279
93- protected String search (Container .Entry entry , File file ) {
94- if (file != null ) {
95- String fileName = file .toString ().toLowerCase ();
96-
97- if (fileName .endsWith (".zip" ) || fileName .endsWith (".jar" )) {
98- byte [] buffer = new byte [1024 * 2 ];
80+ protected String searchSource (Container .Entry entry , File sourceJarFile ) {
81+ if (sourceJarFile != null ) {
82+ byte [] buffer = new byte [1024 * 2 ];
9983
100- try (ZipInputStream zis = new ZipInputStream (new BufferedInputStream (new FileInputStream (file )))) {
101- ZipEntry ze = zis .getNextEntry ();
102- String name = entry .getPath ();
84+ try (ZipInputStream zis = new ZipInputStream (new BufferedInputStream (new FileInputStream (sourceJarFile )))) {
85+ ZipEntry ze = zis .getNextEntry ();
86+ String name = entry .getPath ();
10387
104- name = name .substring (0 , name .length ()-6 ) + ".java" ; // 6 = ".class".length()
88+ name = name .substring (0 , name .length ()-6 ) + ".java" ; // 6 = ".class".length()
10589
106- while (ze != null ) {
107- if (ze .getName ().equals (name )) {
108- ByteArrayOutputStream out = new ByteArrayOutputStream ();
109- int read = zis .read (buffer );
90+ while (ze != null ) {
91+ if (ze .getName ().equals (name )) {
92+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
93+ int read = zis .read (buffer );
11094
111- while (read > 0 ) {
112- out .write (buffer , 0 , read );
113- read = zis .read (buffer );
114- }
115-
116- return new String (out .toByteArray (), "UTF-8" );
95+ while (read > 0 ) {
96+ out .write (buffer , 0 , read );
97+ read = zis .read (buffer );
11798 }
11899
119- ze = zis . getNextEntry ( );
100+ return new String ( out . toByteArray (), "UTF-8" );
120101 }
121102
122- zis .closeEntry ();
123- } catch (IOException e ) {
124- assert ExceptionUtil .printStackTrace (e );
103+ ze = zis .getNextEntry ();
125104 }
105+
106+ zis .closeEntry ();
107+ } catch (IOException e ) {
108+ assert ExceptionUtil .printStackTrace (e );
126109 }
127110 }
128111
129112 return null ;
130113 }
131114
132- protected File load (Container .Entry entry ) {
115+ protected File downloadSourceJarFile (Container .Entry entry ) {
133116 if (cache .containsKey (entry )) {
134117 return cache .get (entry );
135118 }
@@ -158,6 +141,7 @@ protected File load(Container.Entry entry) {
158141 URL searchUrl = new URL (MAVENORG_SEARCH_URL_PREFIX + sha1 + MAVENORG_SEARCH_URL_SUFFIX );
159142 boolean sourceAvailable = false ;
160143 String id = null ;
144+ String numFound = null ;
161145
162146 try (InputStream is = searchUrl .openStream ()) {
163147 XMLStreamReader reader = XMLInputFactory .newInstance ().createXMLStreamReader (is );
@@ -172,6 +156,8 @@ protected File load(Container.Entry entry) {
172156 } else {
173157 name = "str" ;
174158 }
159+ } else if ("result" .equals (reader .getLocalName ())) {
160+ numFound = reader .getAttributeValue (null , "numFound" );
175161 } else {
176162 name = "" ;
177163 }
@@ -192,18 +178,31 @@ protected File load(Container.Entry entry) {
192178 reader .close ();
193179 }
194180
195- if (sourceAvailable == false ) {
196- failed .add (entry );
197- } else {
198- // Load source
181+ String groupId =null , artifactId =null , version =null ;
182+
183+ if ("0" .equals (numFound )) {
184+ // File not indexed by Apache Solr of maven.org -> Try to found groupId, artifactId, version in 'pom.properties'
185+ Properties pomProperties = getPomProperties (entry );
186+
187+ if (pomProperties != null ) {
188+ groupId = pomProperties .getProperty ("groupId" );
189+ artifactId = pomProperties .getProperty ("artifactId" );
190+ version = pomProperties .getProperty ("version" );
191+ }
192+ } else if ("1" .equals (numFound ) && sourceAvailable ) {
199193 int index1 = id .indexOf (':' );
200194 int index2 = id .lastIndexOf (':' );
201- String groupId = id .substring (0 , index1 );
202- String artifactId = id .substring (index1 +1 , index2 );
203- String version = id .substring (index2 +1 );
195+
196+ groupId = id .substring (0 , index1 );
197+ artifactId = id .substring (index1 +1 , index2 );
198+ version = id .substring (index2 +1 );
199+ }
200+
201+ if (artifactId != null ) {
202+ // Load source
204203 String filePath = groupId .replace ('.' , '/' ) + '/' + artifactId + '/' + version + '/' + artifactId + '-' + version ;
205204 URL loadUrl = new URL (MAVENORG_LOAD_URL_PREFIX + filePath + MAVENORG_LOAD_URL_SUFFIX );
206- File tmpFile = File .createTempFile ("jd-gui.tmp." , '.' + artifactId + '- ' + version + "-sources.jar" );
205+ File tmpFile = File .createTempFile ("jd-gui.tmp." , '.' + groupId + '_' + artifactId + '_ ' + version + "-sources.jar" );
207206
208207 tmpFile .delete ();
209208 tmpFile .deleteOnExit ();
@@ -221,7 +220,45 @@ protected File load(Container.Entry entry) {
221220 }
222221 } catch (Exception e ) {
223222 assert ExceptionUtil .printStackTrace (e );
224- failed .add (entry );
223+ }
224+ }
225+
226+ failed .add (entry );
227+ return null ;
228+ }
229+
230+ private static Properties getPomProperties (Container .Entry parent ) {
231+ // Search 'META-INF/maven/*/*/pom.properties'
232+ for (Container .Entry child1 : parent .getChildren ()) {
233+ if (child1 .isDirectory () && child1 .getPath ().equals ("META-INF" )) {
234+ for (Container .Entry child2 : child1 .getChildren ()) {
235+ if (child2 .isDirectory () && child2 .getPath ().equals ("META-INF/maven" )) {
236+ if (child2 .isDirectory ()) {
237+ Collection <Container .Entry > children = child2 .getChildren ();
238+ if (children .size () == 1 ) {
239+ Container .Entry entry = children .iterator ().next ();
240+ if (entry .isDirectory ()) {
241+ children = entry .getChildren ();
242+ if (children .size () == 1 ) {
243+ entry = children .iterator ().next ();
244+ for (Container .Entry child3 : entry .getChildren ()) {
245+ if (!child3 .isDirectory () && child3 .getPath ().endsWith ("/pom.properties" )) {
246+ // Load properties
247+ try (InputStream is = child3 .getInputStream ()) {
248+ Properties properties = new Properties ();
249+ properties .load (is );
250+ return properties ;
251+ } catch (Exception e ) {
252+ assert ExceptionUtil .printStackTrace (e );
253+ }
254+ }
255+ }
256+ }
257+ }
258+ }
259+ }
260+ }
261+ }
225262 }
226263 }
227264
0 commit comments