1 package org.naftulin.classpathexplorer.dublicate.imlp;
2
3 import java.io.File;
4
5 /***
6 * Factory that creates subclasses of {@link AccessibleArchive} based on archives file extension.
7 * Follows GOF factory method
8 * @author henry naftulin
9 * @version 1.0
10 */
11 public class ArchiveFactory {
12
13 /***
14 * Factory that creates subclasses of {@link AccessibleArchive} based on archives file extension.
15 * At this point knows how to handle directories, zip, jar and war archives. If
16 * the file passes is non of the above (not directory and does not have the
17 * the extensions specified) IllegalArgumentException is thrown.
18 * @param file archive file
19 * @return archive representing the file passed in.
20 * @exception IllegalArgumentException when the file passed is not a directory or .jar, or .war, or .zip
21 */
22 static AccessibleArchive createArchive(File file) {
23 String archivePath = file.getPath();
24 AccessibleArchive archive = null;
25 if (archivePath.endsWith(".zip")) {
26 archive = new ZipArchive(file);
27 } else if (archivePath.endsWith(".jar")) {
28 archive = new JarArchive(file);
29 } else if (archivePath.endsWith(".war")) {
30 archive = new WarArchive(file);
31 } else if (file.isDirectory()){
32 archive = new DirectoryArchive(file);
33 } else {
34 throw new IllegalArgumentException("Unknow file " + file.getPath() + ". Valid argument is either directories, .jar, .zip and .war files");
35 }
36 return archive;
37 }
38
39 }