Skip to content

Commit 97e88a3

Browse files
committed
Manifest: add more static creation methods
One such allows creation from an org.scijava.util.XML object. Another enables creation from a JAR file on disk.
1 parent b9e07b1 commit 97e88a3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/main/java/org/scijava/util/Manifest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ public static Manifest getManifest(final Class<?> c) {
143143
}
144144
}
145145

146+
/**
147+
* Gets the JAR manifest associated with the given XML document. Assumes the
148+
* XML document was loaded as a resource from inside a JAR.
149+
*/
150+
public static Manifest getManifest(final XML xml) throws IOException {
151+
final String path = xml.getPath();
152+
if (path == null || !path.startsWith("file:")) return null;
153+
final int dotJAR = path.indexOf(".jar!/");
154+
return getManifest(new File(path.substring(5, dotJAR + 4)));
155+
}
156+
157+
/** Gets the JAR manifest associated with the given JAR file. */
158+
public static Manifest getManifest(final File jarFile) throws IOException {
159+
if (!jarFile.exists()) throw new FileNotFoundException();
160+
return getManifest(new URL("jar:file:" + jarFile.getAbsolutePath() + "!/"));
161+
}
162+
146163
private static Manifest getManifest(final URL jarURL) throws IOException {
147164
final JarURLConnection conn = (JarURLConnection) jarURL.openConnection();
148165
return new Manifest(conn.getManifest());

0 commit comments

Comments
 (0)