View Javadoc

1   package org.naftulin.classpathexplorer.dublicate.imlp;
2   
3   import java.io.File;
4   import java.io.Serializable;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import org.naftulin.classpathexplorer.AccessibleResource;
9   
10  
11  /***
12   * Finds dublicate resource in the class path (unless a different path is provided). 
13   * Most methods use class path to discover resources; to specify the path use
14   * {@link #getDublicateResources(String[])} method.
15   * 
16   * @author henry naftulin
17   * @version 1.0
18   */
19  public class DuplicateResourcesReport implements Serializable {
20  	private static final long serialVersionUID = 1L;
21  	private String[] accesptedExtensions;
22  	private static final AccessibleResource[] EMPTY_RESOURCES = new AccessibleResource[0];
23  	
24  	/***
25  	 * Returns the extensions that are accepted and only file ending with those ending will be reported to in dublicate reported. 
26  	 * @return the extensions that are accepted and only file ending with those ending will be reported to in dublicate reported.
27  	 */
28  	public String[] getAccesptedExtensions() {
29  		return (String[]) accesptedExtensions.clone();
30  	}
31  	
32  	/***
33  	 * Sets the extension that are accepted and only file ending with those ending will be reported to in dublicate reported.
34  	 * @param acceptedExtensions file ending with those ending will be reported to in dublicate reported.
35  	 */
36  	public void setAccesptedExtensions(String[] acceptedExtensions) {
37  		this.accesptedExtensions = (String[])acceptedExtensions.clone();
38  	}
39  	
40  	/***
41  	 * Returns dublicate resources.
42  	 * @return dublicate resources.
43  	 */
44  	public AccessibleResource[] getDublicateResources() {
45  		ResourceRegistry registry = init();
46  		AccessibleResource[] retArray = doGetDublicateResources(registry);
47  		
48  		return retArray;
49  	}
50  
51  	/***
52  	 * Returns dublicate resources given initialized resource registry.
53  	 * @param registry initialized resource registry.
54  	 * @return dublicate resources.
55  	 */
56  	private AccessibleResource[] doGetDublicateResources(ResourceRegistry registry) {
57  		AccessibleResource[] retArray = EMPTY_RESOURCES;
58  		AccessibleResource[] dups = registry.getDublicatedResources();
59  		if (accesptedExtensions != null && accesptedExtensions.length >0) {
60  			List list = new ArrayList(dups.length);
61  			boolean acceptValue;
62  			int j;
63  			for(int i=0; i < dups.length; ++i) {
64  				acceptValue = false;
65  				j=0;
66  				if (dups[i].getName()!= null) {
67  					while(j<accesptedExtensions.length && !acceptValue) {
68  						acceptValue |= dups[i].getName().endsWith(accesptedExtensions[j]);
69  						++j;
70  					}
71  				}
72  				if (acceptValue) {
73  					list.add(dups[i]);
74  				}
75  			}
76  			retArray = new AccessibleResource[list.size()];
77  			retArray = (AccessibleResource[]) list.toArray(retArray);
78  		} else {
79  			retArray = dups;
80  		}
81  		return retArray;
82  	}
83  	
84  	/***
85  	 * Returns dublicate resources as string.
86  	 * @return dublicate resources as string.
87  	 */
88  	public String getDublicateResourcesAsStringReport() {
89  		AccessibleResource[] dups = getDublicateResources();
90  		StringBuffer buf = new StringBuffer(1000);
91  		for(int i=0; i < dups.length; ++i) {
92  			buf.append(dups[i]).append("\n");
93  		}
94  		return buf.toString();
95  	}
96  	
97  	/***
98  	 * Returns dublicate resources as xml string. 
99  	 * @return dublicate resources as xml string.
100 	 */
101 	public String getDublicateResourcesAsXmlReport() {
102 		AccessibleResource[] dups = getDublicateResources();
103 		StringBuffer buf = new StringBuffer(1000);
104 		buf.append("<dublicateResources>\n");
105 		for(int i=0; i < dups.length; ++i) {
106 			buf.append(dups[i].toXml()).append("\n");
107 		}
108 		buf.append("</dublicateResources>");
109 		return buf.toString();
110 	}
111 	
112 	/***
113 	 * Returns dublicate resources given archives path. Archives path points to either 
114 	 * directories or .jar and .zip files.
115 	 * @param given archives path.
116 	 * @return dublicate resources
117 	 */
118 	public AccessibleResource[] getDublicateResources(String[] archivesPath) {
119 		ResourceRegistry registry = ResourceRegistry.getInstance();
120 		registry.clearRegistry();
121 		registry.discoverResources(archivesPath);
122 		AccessibleResource[] retArray = doGetDublicateResources(registry);
123 		
124 		return retArray;
125 	}
126 	
127 	/***
128 	 * Returns the initialized resource registry based on the class path.
129 	 * Package protected method for easy testing.
130 	 * @return initialized registry based on the class path
131 	 */
132 	ResourceRegistry init() {
133 		ResourceRegistry registry = ResourceRegistry.getInstance();
134 		registry.clearRegistry();
135 		String classPath = System.getProperty("java.class.path");
136 		String[] classPathArr = classPath.split(File.pathSeparator);
137 		registry.discoverResources(classPathArr);
138 		return registry;
139 	}
140 	
141 }