View Javadoc

1   package org.naftulin.classpathexplorer.dublicate.imlp;
2   
3   import java.io.File;
4   import java.util.Date;
5   import java.util.zip.ZipEntry;
6   
7   import org.naftulin.classpathexplorer.AccessibleResource;
8   
9   /***
10   * Represents an accessible resource: its location, last modified time, etc.
11   * Imlpements {@link AccessibleResource} interface
12   * @author henry naftulin
13   * @version 1.0
14   */
15  class AccessibleResourceImpl implements AccessibleResource {
16  	private final String path;
17  	private final String name;
18  	private final long lastModified;
19  	private final String fullPath;
20  	private final AccessibleArchive archive;
21  	
22  	/***
23  	 * Creates an accessible resource based on file, path to the file, and archive from where resource resides.
24  	 * @param file resource file
25  	 * @param path of the resource insided the archive
26  	 * @param archive where there resource resides
27  	 */
28  	AccessibleResourceImpl(File file, String path, AccessibleArchive archive) {
29  		this.archive = archive;
30  		this.path = path;
31  		this.name = file.getName();
32  		this.fullPath = file.getAbsolutePath();
33  		this.lastModified = file.lastModified();
34  	}
35  
36  	/***
37  	 * Creates an accessible resource based on entry inside zipped achive, and archive where the resource resides
38  	 * @param entry in zipped archive representing the file
39  	 * @param archive where there resource resides
40  	 */
41  	AccessibleResourceImpl(ZipEntry entry, AccessibleArchive archive) {
42  		String entryName = entry.getName();
43  		int nameIndex = entryName.lastIndexOf("/");
44  		this.archive = archive;
45  		this.path = entryName.substring(0, nameIndex+1);
46  		this.name = entryName.substring(nameIndex+1);
47  		this.fullPath = archive.getPath() + File.pathSeparator + entry.getName();
48  		this.lastModified = entry.getTime();
49  	}
50  
51  	/***
52  	 * Returns the archive the resource is located in.
53  	 * @return the archive the resource is located in.
54  	 */
55  	public AccessibleArchive getArchive() {
56  		return archive;
57  	}
58  
59  	/*** 
60  	 * Returns the path for the resource. If resource resides in an archive,
61  	 * (e.g. zip or jar) path represents a full path: path to the
62  	 * archive and path of the resource within an archive.
63  	 * @return the path for the resource.
64  	 */
65  	public String getFullPath() {
66  		return fullPath;
67  	}
68  
69  	/***
70  	 * Returns the last modified Date as long
71  	 * @return the last modified Date as long
72  	 */
73  	public long getLastModified() {
74  		return lastModified;
75  	}
76  
77  	/***
78  	 * Returns the name of the resource.
79  	 * @return the name of the resource.
80  	 */
81  	public String getName() {
82  		return name;
83  	}
84  
85  	/*** 
86  	 * Returns the path for the resource. If resource resides in an archive,
87  	 * (e.g. zip or jar) path represents a path of the resource within an
88  	 * archive.
89  	 * @return the path for the resource.
90  	 */
91  	public String getPath() {
92  		return path;
93  	}
94  	
95  	/***
96  	 * Returns a key for the resource.
97  	 * Key is used for resource registry to recognize dublicate resources.
98  	 * For archives it's a path (with the name of the archive). For
99  	 * files it's the path of the file within an archive and the file name.
100 	 * @return a key identifying a resource.
101 	 */
102 	public String getKey() {
103 		return getPath() + getName();
104 	}
105 	
106 	/***
107 	 * Returns the string representation of the resource.
108 	 * @return the string representation of the resource.
109 	 */
110 	public String toString() {
111 		StringBuffer sb = new StringBuffer(20);
112 		sb.append(path).append(name);
113 		sb.append(" last modified: ").append(new Date(lastModified));
114 		sb.append(" from archive: ").append(archive.getPath());
115 		sb.append("\n");
116 		return sb.toString();
117 	}
118 	
119 	/***
120 	 * Returns the xml representation of the resource.
121 	 * @return the xml representation of the resource.
122 	 */
123 	public String toXml() {
124 		StringBuffer sb = new StringBuffer(20);
125 		sb.append("<resource path='");
126 		sb.append(path).append("' name='").append(name).append("' ");
127 		sb.append("lastModified='").append(new Date(lastModified)).append("'");
128 		sb.append(" fromArchive='").append(archive.getPath()).append("' />\n");
129 		return sb.toString();
130 	}
131 }