From b3ef034b5805dbf1fd6ec9e7c2cacbccef18bd7c Mon Sep 17 00:00:00 2001 From: roswalt Date: Thu, 13 Jun 2019 15:19:30 +0200 Subject: [PATCH] Add kar file support (Karaf Archive) https://karaf.apache.org/manual/latest/kar --- .../jd/gui/model/container/KarContainer.java | 21 +++++++++ .../KarContainerFactoryProvider.java | 43 +++++++++++++++++++ .../fileloader/KarFileLoaderProvider.java | 24 +++++++++++ .../indexer/ZipFileIndexerProvider.java | 2 +- .../ZipFileSourceSaverProvider.java | 2 +- .../KarFileTreeNodeFactoryProvider.java | 33 ++++++++++++++ .../services/org.jd.gui.spi.ContainerFactory | 1 + .../services/org.jd.gui.spi.FileLoader | 1 + .../services/org.jd.gui.spi.TreeNodeFactory | 1 + 9 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 services/src/main/java/org/jd/gui/model/container/KarContainer.java create mode 100644 services/src/main/java/org/jd/gui/service/container/KarContainerFactoryProvider.java create mode 100644 services/src/main/java/org/jd/gui/service/fileloader/KarFileLoaderProvider.java create mode 100644 services/src/main/java/org/jd/gui/service/treenode/KarFileTreeNodeFactoryProvider.java diff --git a/services/src/main/java/org/jd/gui/model/container/KarContainer.java b/services/src/main/java/org/jd/gui/model/container/KarContainer.java new file mode 100644 index 00000000..10712e04 --- /dev/null +++ b/services/src/main/java/org/jd/gui/model/container/KarContainer.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2008-2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.gui.model.container; + +import org.jd.gui.api.API; +import org.jd.gui.api.model.Container; + +import java.nio.file.Path; + +public class KarContainer extends GenericContainer { + public KarContainer(API api, Container.Entry parentEntry, Path rootPath) { + super(api, parentEntry, rootPath); + } + + public String getType() { return "kar"; } +} diff --git a/services/src/main/java/org/jd/gui/service/container/KarContainerFactoryProvider.java b/services/src/main/java/org/jd/gui/service/container/KarContainerFactoryProvider.java new file mode 100644 index 00000000..3d60071f --- /dev/null +++ b/services/src/main/java/org/jd/gui/service/container/KarContainerFactoryProvider.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2008-2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.gui.service.container; + +import org.jd.gui.api.API; +import org.jd.gui.api.model.Container; +import org.jd.gui.model.container.KarContainer; +import org.jd.gui.spi.ContainerFactory; +import org.jd.gui.util.exception.ExceptionUtil; + +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; + +public class KarContainerFactoryProvider implements ContainerFactory { + @Override + public String getType() { return "kar"; } + + @Override + public boolean accept(API api, Path rootPath) { + if (rootPath.toUri().toString().toLowerCase().endsWith(".kar!/")) { + return true; + } else { + // Extension: accept uncompressed KAR file containing a folder 'repository' + try { + return rootPath.getFileSystem().provider().getScheme().equals("file") && Files.exists(rootPath.resolve("repository")); + } catch (InvalidPathException e) { + assert ExceptionUtil.printStackTrace(e); + return false; + } + } + } + + @Override + public Container make(API api, Container.Entry parentEntry, Path rootPath) { + return new KarContainer(api, parentEntry, rootPath); + } +} diff --git a/services/src/main/java/org/jd/gui/service/fileloader/KarFileLoaderProvider.java b/services/src/main/java/org/jd/gui/service/fileloader/KarFileLoaderProvider.java new file mode 100644 index 00000000..f3eeffca --- /dev/null +++ b/services/src/main/java/org/jd/gui/service/fileloader/KarFileLoaderProvider.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2008-2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.gui.service.fileloader; + +import org.jd.gui.api.API; + +import java.io.File; + +public class KarFileLoaderProvider extends ZipFileLoaderProvider { + protected static final String[] EXTENSIONS = { "kar" }; + + @Override public String[] getExtensions() { return EXTENSIONS; } + @Override public String getDescription() { return "Karaf archive files (*.kar)"; } + + @Override + public boolean accept(API api, File file) { + return file.exists() && file.isFile() && file.canRead() && file.getName().toLowerCase().endsWith(".kar"); + } +} diff --git a/services/src/main/java/org/jd/gui/service/indexer/ZipFileIndexerProvider.java b/services/src/main/java/org/jd/gui/service/indexer/ZipFileIndexerProvider.java index d544bfa6..2363f52b 100644 --- a/services/src/main/java/org/jd/gui/service/indexer/ZipFileIndexerProvider.java +++ b/services/src/main/java/org/jd/gui/service/indexer/ZipFileIndexerProvider.java @@ -14,7 +14,7 @@ public class ZipFileIndexerProvider extends AbstractIndexerProvider { - @Override public String[] getSelectors() { return appendSelectors("*:file:*.zip", "*:file:*.jar", "*:file:*.war", "*:file:*.ear", "*:file:*.aar", "*:file:*.jmod"); } + @Override public String[] getSelectors() { return appendSelectors("*:file:*.zip", "*:file:*.jar", "*:file:*.war", "*:file:*.ear", "*:file:*.aar", "*:file:*.jmod", "*:file:*.kar"); } @Override public void index(API api, Container.Entry entry, Indexes indexes) { diff --git a/services/src/main/java/org/jd/gui/service/sourcesaver/ZipFileSourceSaverProvider.java b/services/src/main/java/org/jd/gui/service/sourcesaver/ZipFileSourceSaverProvider.java index 9015cc97..4836a341 100644 --- a/services/src/main/java/org/jd/gui/service/sourcesaver/ZipFileSourceSaverProvider.java +++ b/services/src/main/java/org/jd/gui/service/sourcesaver/ZipFileSourceSaverProvider.java @@ -22,7 +22,7 @@ public class ZipFileSourceSaverProvider extends DirectorySourceSaverProvider { - @Override public String[] getSelectors() { return appendSelectors("*:file:*.zip", "*:file:*.jar", "*:file:*.war", "*:file:*.ear", "*:file:*.aar", "*:file:*.jmod"); } + @Override public String[] getSelectors() { return appendSelectors("*:file:*.zip", "*:file:*.jar", "*:file:*.war", "*:file:*.ear", "*:file:*.aar", "*:file:*.jmod", "*:file:*.kar"); } @Override public void save(API api, SourceSaver.Controller controller, SourceSaver.Listener listener, Path rootPath, Container.Entry entry) { diff --git a/services/src/main/java/org/jd/gui/service/treenode/KarFileTreeNodeFactoryProvider.java b/services/src/main/java/org/jd/gui/service/treenode/KarFileTreeNodeFactoryProvider.java new file mode 100644 index 00000000..7a8f4f9d --- /dev/null +++ b/services/src/main/java/org/jd/gui/service/treenode/KarFileTreeNodeFactoryProvider.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2008-2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.gui.service.treenode; + +import org.jd.gui.api.API; +import org.jd.gui.api.feature.ContainerEntryGettable; +import org.jd.gui.api.feature.UriGettable; +import org.jd.gui.api.model.Container; +import org.jd.gui.view.data.TreeNodeBean; + +import javax.swing.tree.DefaultMutableTreeNode; +import java.io.File; + +public class KarFileTreeNodeFactoryProvider extends ZipFileTreeNodeFactoryProvider { + @Override public String[] getSelectors() { return appendSelectors("*:file:*.kar"); } + + @Override + @SuppressWarnings("unchecked") + public T make(API api, Container.Entry entry) { + int lastSlashIndex = entry.getPath().lastIndexOf("/"); + String label = entry.getPath().substring(lastSlashIndex+1); + String location = new File(entry.getUri()).getPath(); + T node = (T)new TreeNode(entry, new TreeNodeBean(label, "Location: " + location, ICON)); + // Add dummy node + node.add(new DefaultMutableTreeNode()); + return node; + } +} diff --git a/services/src/main/resources/META-INF/services/org.jd.gui.spi.ContainerFactory b/services/src/main/resources/META-INF/services/org.jd.gui.spi.ContainerFactory index f8a5ecc5..8522ab7a 100644 --- a/services/src/main/resources/META-INF/services/org.jd.gui.spi.ContainerFactory +++ b/services/src/main/resources/META-INF/services/org.jd.gui.spi.ContainerFactory @@ -1,4 +1,5 @@ # Order is important : 'GenericContainerFactoryProvider' must be the last +org.jd.gui.service.container.KarContainerFactoryProvider org.jd.gui.service.container.JmodContainerFactoryProvider org.jd.gui.service.container.EarContainerFactoryProvider org.jd.gui.service.container.WarContainerFactoryProvider diff --git a/services/src/main/resources/META-INF/services/org.jd.gui.spi.FileLoader b/services/src/main/resources/META-INF/services/org.jd.gui.spi.FileLoader index 28758244..8e193e41 100644 --- a/services/src/main/resources/META-INF/services/org.jd.gui.spi.FileLoader +++ b/services/src/main/resources/META-INF/services/org.jd.gui.spi.FileLoader @@ -4,6 +4,7 @@ org.jd.gui.service.fileloader.EarFileLoaderProvider org.jd.gui.service.fileloader.JarFileLoaderProvider org.jd.gui.service.fileloader.JavaFileLoaderProvider org.jd.gui.service.fileloader.JmodFileLoaderProvider +org.jd.gui.service.fileloader.KarFileLoaderProvider org.jd.gui.service.fileloader.LogFileLoaderProvider org.jd.gui.service.fileloader.WarFileLoaderProvider org.jd.gui.service.fileloader.ZipFileLoaderProvider diff --git a/services/src/main/resources/META-INF/services/org.jd.gui.spi.TreeNodeFactory b/services/src/main/resources/META-INF/services/org.jd.gui.spi.TreeNodeFactory index d9f2f9bd..6e50ae02 100644 --- a/services/src/main/resources/META-INF/services/org.jd.gui.spi.TreeNodeFactory +++ b/services/src/main/resources/META-INF/services/org.jd.gui.spi.TreeNodeFactory @@ -14,6 +14,7 @@ org.jd.gui.service.treenode.JmodFileTreeNodeFactoryProvider org.jd.gui.service.treenode.JmodPackageTreeNodeFactoryProvider org.jd.gui.service.treenode.JsonFileTreeNodeFactoryProvider org.jd.gui.service.treenode.JspFileTreeNodeFactoryProvider +org.jd.gui.service.treenode.KarFileTreeNodeFactoryProvider org.jd.gui.service.treenode.ManifestFileTreeNodeFactoryProvider org.jd.gui.service.treenode.MetainfDirectoryTreeNodeFactoryProvider org.jd.gui.service.treenode.MetainfServiceFileTreeNodeFactoryProvider