Fixes #35, "Save All source dose not work"
emmanue1
8 years ago
19 | 19 | |
20 | 20 | public int getFileCount(API api, Container.Entry entry); |
21 | 21 | |
22 | public void save(API api, Controller controller, Listener listener, Path path, Container.Entry entry); | |
22 | /** | |
23 | * Check parent path, build source file name, create NIO path and save the content. | |
24 | */ | |
25 | public void save(API api, Controller controller, Listener listener, Path rootPath, Container.Entry entry); | |
26 | ||
27 | /** | |
28 | * Save content: | |
29 | * <ul> | |
30 | * <li>For file, save the source content.</li> | |
31 | * <li>For directory, call 'save' for each children.</li> | |
32 | * </ul> | |
33 | */ | |
34 | public void saveContent(API api, Controller controller, Listener listener, Path rootPath, Path path, Container.Entry entry); | |
23 | 35 | |
24 | 36 | public interface Controller { |
25 | 37 | public boolean isCancelled(); |
+28
-10
21 | 21 | import javax.swing.JComponent |
22 | 22 | import javax.swing.tree.DefaultMutableTreeNode |
23 | 23 | import javax.swing.tree.DefaultTreeModel |
24 | import java.nio.file.FileSystems | |
25 | import java.nio.file.Files | |
24 | 26 | import java.nio.file.Path |
25 | 27 | |
26 | 28 | class ContainerPanelFactoryProvider implements PanelFactory { |
27 | 29 | |
28 | String[] getTypes() { ['default'] } | |
30 | @Override String[] getTypes() { ['default'] } | |
29 | 31 | |
32 | @Override | |
30 | 33 | public <T extends JComponent & UriGettable> T make(API api, Container container) { |
31 | 34 | return new ContainerPanel(api, container) |
32 | 35 | } |
52 | 55 | } |
53 | 56 | |
54 | 57 | // --- ContentIndexable --- // |
58 | @Override | |
55 | 59 | @CompileStatic |
56 | 60 | Indexes index(API api) { |
57 | 61 | // Classic map |
70 | 74 | } |
71 | 75 | // Index populating value automatically |
72 | 76 | def indexesWithDefault = new Indexes() { |
73 | void waitIndexers() {} | |
74 | Map<String, Collection> getIndex(String name) { | |
75 | mapWithDefault.get(name) | |
76 | } | |
77 | @Override void waitIndexers() {} | |
78 | @Override Map<String, Collection> getIndex(String name) { mapWithDefault.get(name) } | |
77 | 79 | } |
78 | 80 | |
79 | 81 | api.getIndexer(entry)?.index(api, entry, indexesWithDefault) |
80 | 82 | |
81 | 83 | // To prevent memory leaks, return an index without the 'populate' behaviour |
82 | 84 | return new Indexes() { |
83 | void waitIndexers() {} | |
84 | Map<String, Collection> getIndex(String name) { map.get(name) } | |
85 | @Override void waitIndexers() {} | |
86 | @Override Map<String, Collection> getIndex(String name) { map.get(name) } | |
85 | 87 | } |
86 | 88 | } |
87 | 89 | |
88 | 90 | // --- SourcesSavable --- // |
91 | @Override | |
89 | 92 | String getSourceFileName() { |
90 | 93 | def path = api.getSourceSaver(entry)?.getSourcePath(entry) |
91 | 94 | int index = path.lastIndexOf('/') |
92 | 95 | return path.substring(index+1) |
93 | 96 | } |
94 | 97 | |
95 | int getFileCount() { api.getSourceSaver(entry)?.getFileCount(api, entry) } | |
98 | @Override int getFileCount() { api.getSourceSaver(entry)?.getFileCount(api, entry) } | |
96 | 99 | |
100 | @Override | |
97 | 101 | void save(API api, Controller controller, Listener listener, Path path) { |
98 | api.getSourceSaver(entry)?.save( | |
102 | def parentPath = path.parent | |
103 | ||
104 | if (parentPath && !Files.exists(parentPath)) { | |
105 | Files.createDirectories(parentPath) | |
106 | } | |
107 | ||
108 | def uri = path.toUri() | |
109 | def archiveUri = new URI('jar:' + uri.scheme, uri.host, uri.path + '!/', null) | |
110 | def archiveFs = FileSystems.newFileSystem(archiveUri, [create: 'true']) | |
111 | def archiveRootPath = archiveFs.getPath('/') | |
112 | ||
113 | api.getSourceSaver(entry)?.saveContent( | |
99 | 114 | api, |
100 | 115 | new SourceSaver.Controller() { |
101 | 116 | boolean isCancelled() { controller.isCancelled() } |
103 | 118 | new SourceSaver.Listener() { |
104 | 119 | void pathSaved(Path p) { listener.pathSaved(p) } |
105 | 120 | }, |
106 | path, entry) | |
121 | archiveRootPath, archiveRootPath, entry | |
122 | ) | |
123 | ||
124 | archiveFs.close() | |
107 | 125 | } |
108 | 126 | } |
109 | 127 | } |
+17
-16
17 | 17 | /** |
18 | 18 | * @return local + optional external selectors |
19 | 19 | */ |
20 | String[] getSelectors() { ['*:dir:*'] + externalSelectors } | |
20 | @Override String[] getSelectors() { ['*:dir:*'] + externalSelectors } | |
21 | 21 | |
22 | String getSourcePath(Container.Entry entry) { entry.path } | |
22 | @Override String getSourcePath(Container.Entry entry) { entry.path + '.src.zip' } | |
23 | 23 | |
24 | int getFileCount(API api, Container.Entry entry) { getFileCount(api, entry.children) } | |
24 | @Override int getFileCount(API api, Container.Entry entry) { getFileCount(api, entry.children) } | |
25 | 25 | |
26 | 26 | @CompileStatic |
27 | 27 | protected int getFileCount(API api, Collection<Container.Entry> entries) { |
34 | 34 | return count |
35 | 35 | } |
36 | 36 | |
37 | void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path path, Container.Entry entry) { | |
38 | save(api, controller, listener, path, entry.children) | |
37 | @Override | |
38 | @CompileStatic | |
39 | public void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path rootPath, Container.Entry entry) { | |
40 | Path path = rootPath.resolve(entry.getPath()) | |
41 | ||
42 | Files.createDirectories(path) | |
43 | ||
44 | saveContent(api, controller, listener, rootPath, path, entry); | |
39 | 45 | } |
40 | 46 | |
47 | @Override | |
41 | 48 | @CompileStatic |
42 | protected void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path path, Collection<Container.Entry> entries) { | |
43 | Files.createDirectories(path) | |
44 | ||
45 | for (def e : entries) { | |
49 | public void saveContent(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path rootPath, Path path, Container.Entry entry) { | |
50 | for (def e : getChildren(entry)) { | |
46 | 51 | if (controller.isCancelled()) { |
47 | 52 | break |
48 | 53 | } |
49 | 54 | |
50 | def saver = api.getSourceSaver(e) | |
51 | ||
52 | if (saver) { | |
53 | def sp = saver.getSourcePath(e) | |
54 | def p = path.fileSystem.getPath(sp) | |
55 | saver.save(api, controller, listener, p, e) | |
56 | } | |
55 | api.getSourceSaver(e)?.save(api, controller, listener, rootPath, e) | |
57 | 56 | } |
58 | 57 | } |
58 | ||
59 | protected Collection<Container.Entry> getChildren(Container.Entry entry) { entry.children } | |
59 | 60 | } |
+11
-4
18 | 18 | /** |
19 | 19 | * @return local + optional external selectors |
20 | 20 | */ |
21 | String[] getSelectors() { ['*:file:*'] + externalSelectors } | |
21 | @Override String[] getSelectors() { ['*:file:*'] + externalSelectors } | |
22 | 22 | |
23 | String getSourcePath(Container.Entry entry) { entry.path } | |
23 | @Override String getSourcePath(Container.Entry entry) { entry.path } | |
24 | 24 | |
25 | int getFileCount(API api, Container.Entry entry) { 1 } | |
25 | @Override int getFileCount(API api, Container.Entry entry) { 1 } | |
26 | 26 | |
27 | @Override | |
27 | 28 | @CompileStatic |
28 | void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path path, Container.Entry entry) { | |
29 | public void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path rootPath, Container.Entry entry) { | |
30 | saveContent(api, controller, listener, rootPath, rootPath.resolve(entry.getPath()), entry); | |
31 | } | |
32 | ||
33 | @Override | |
34 | @CompileStatic | |
35 | void saveContent(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path rootPath, Path path, Container.Entry entry) { | |
29 | 36 | listener.pathSaved(path) |
30 | 37 | |
31 | 38 | entry.inputStream.withStream { InputStream is -> |
+4
-7
4 | 4 | |
5 | 5 | package org.jd.gui.service.sourcesaver |
6 | 6 | |
7 | import org.jd.gui.api.API | |
8 | 7 | import org.jd.gui.api.model.Container |
9 | import org.jd.gui.spi.SourceSaver | |
10 | 8 | import org.jd.gui.util.JarContainerEntryUtil |
11 | ||
12 | import java.nio.file.Path | |
13 | 9 | |
14 | 10 | class PackageSourceSaverProvider extends DirectorySourceSaverProvider { |
15 | 11 | /** |
16 | 12 | * @return local + optional external selectors |
17 | 13 | */ |
18 | String[] getSelectors() { ['jar:dir:*', 'war:dir:*', 'ear:dir:*'] + externalSelectors } | |
14 | @Override String[] getSelectors() { ['jar:dir:*', 'war:dir:*', 'ear:dir:*'] + externalSelectors } | |
19 | 15 | |
20 | void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path path, Container.Entry entry) { | |
21 | save(api, controller, listener, path, JarContainerEntryUtil.removeInnerTypeEntries(entry.children)) | |
16 | @Override | |
17 | protected Collection<Container.Entry> getChildren(Container.Entry entry) { | |
18 | JarContainerEntryUtil.removeInnerTypeEntries(entry.children) | |
22 | 19 | } |
23 | 20 | } |
+17
-16
19 | 19 | /** |
20 | 20 | * @return local + optional external selectors |
21 | 21 | */ |
22 | String[] getSelectors() { ['*:file:*.zip', '*:file:*.jar', '*:file:*.war', '*:file:*.ear'] + externalSelectors } | |
22 | @Override String[] getSelectors() { ['*:file:*.zip', '*:file:*.jar', '*:file:*.war', '*:file:*.ear'] + externalSelectors } | |
23 | 23 | |
24 | String getSourcePath(Container.Entry entry) { entry.path + '.src.zip' } | |
24 | @Override | |
25 | @CompileStatic | |
26 | public void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path rootPath, Container.Entry entry) { | |
27 | def sourcePath = getSourcePath(entry) | |
28 | def path = rootPath.resolve(sourcePath) | |
29 | def parentPath = path.parent | |
25 | 30 | |
26 | @CompileStatic | |
27 | void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path path, Container.Entry entry) { | |
31 | if (parentPath && !Files.exists(parentPath)) { | |
32 | Files.createDirectories(parentPath) | |
33 | } | |
34 | ||
28 | 35 | def tmpFile = File.createTempFile('jd-gui.', '.tmp.zip') |
29 | 36 | tmpFile.delete() |
30 | 37 | |
31 | def env = new HashMap<String, String>() | |
32 | env.put('create', 'true') | |
38 | def tmpFileUri = tmpFile.toURI() | |
39 | def tmpArchiveUri = new URI('jar:' + tmpFileUri.scheme, tmpFileUri.host, tmpFileUri.path + '!/', null) | |
40 | def tmpArchiveFs = FileSystems.newFileSystem(tmpArchiveUri, [create: 'true']); | |
41 | def tmpArchiveRootPath = tmpArchiveFs.getPath('/') | |
33 | 42 | |
34 | def tmpFileUri = tmpFile.toURI() | |
35 | def tmpURI = new URI('jar:' + tmpFileUri.scheme, tmpFileUri.host, tmpFileUri.path + '!/', null) | |
36 | def tmpFs = FileSystems.newFileSystem(tmpURI, env); | |
43 | saveContent(api, controller, listener, tmpArchiveRootPath, tmpArchiveRootPath, entry) | |
37 | 44 | |
38 | super.save(api, controller, listener, tmpFs.getPath('/'), entry) | |
39 | tmpFs.close() | |
45 | tmpArchiveFs.close() | |
40 | 46 | |
41 | 47 | def tmpPath = Paths.get(tmpFile.absolutePath) |
42 | def srcZipParentPath = path.parent | |
43 | ||
44 | if (srcZipParentPath && !Files.exists(srcZipParentPath)) { | |
45 | Files.createDirectories(srcZipParentPath) | |
46 | } | |
47 | 48 | |
48 | 49 | Files.move(tmpPath, path) |
49 | 50 | } |
+13
-3
4 | 4 | |
5 | 5 | package org.jd.gui.service.sourcesaver; |
6 | 6 | |
7 | import groovy.transform.CompileStatic; | |
8 | 7 | import jd.core.CoreConstants; |
9 | 8 | import jd.core.Decompiler; |
10 | 9 | import jd.core.process.DecompilerImpl; |
40 | 39 | /** |
41 | 40 | * @return local + optional external selectors |
42 | 41 | */ |
42 | @Override | |
43 | 43 | public String[] getSelectors() { |
44 | 44 | List<String> externalSelectors = getExternalSelectors(); |
45 | 45 | |
54 | 54 | } |
55 | 55 | } |
56 | 56 | |
57 | @Override | |
57 | 58 | public String getSourcePath(Container.Entry entry) { |
58 | 59 | String path = entry.getPath(); |
59 | 60 | int index = path.lastIndexOf('.'); |
61 | 62 | return prefix + ".java"; |
62 | 63 | } |
63 | 64 | |
65 | @Override | |
64 | 66 | public int getFileCount(API api, Container.Entry entry) { |
65 | 67 | if (entry.getPath().indexOf('$') == -1) { |
66 | 68 | return 1; |
69 | 71 | } |
70 | 72 | } |
71 | 73 | |
72 | public void save(API api, Controller controller, Listener listener, Path path, Container.Entry entry) { | |
74 | @Override | |
75 | public void save(API api, Controller controller, Listener listener, Path rootPath, Container.Entry entry) { | |
76 | String sourcePath = getSourcePath(entry); | |
77 | Path path = rootPath.resolve(sourcePath); | |
78 | ||
79 | saveContent(api, controller, listener, rootPath, path, entry); | |
80 | } | |
81 | ||
82 | @Override | |
83 | public void saveContent(API api, Controller controller, Listener listener, Path rootPath, Path path, Container.Entry entry) { | |
73 | 84 | try { |
74 | 85 | // Call listener |
75 | 86 | if (path.toString().indexOf('$') == -1) { |
136 | 147 | } |
137 | 148 | } |
138 | 149 | |
139 | @CompileStatic | |
140 | 150 | protected static boolean getPreferenceValue(Map<String, String> preferences, String key, boolean defaultValue) { |
141 | 151 | String v = preferences.get(key); |
142 | 152 |