1+ package de .fraunhofer .iem .swan .io .doc ;
2+
3+ import edu .stanford .nlp .util .StringUtils ;
4+
5+ import java .io .File ;
6+ import java .io .FileInputStream ;
7+ import java .io .IOException ;
8+ import java .util .*;
9+ import java .util .jar .JarEntry ;
10+ import java .util .jar .JarInputStream ;
11+
12+ public class SourceJarEvaluator {
13+
14+ /**
15+ * Returns list of packages in a JAR file or directory.
16+ *
17+ * @param jarFile JAR file or directory
18+ * @return array list of package names
19+ */
20+ public static ArrayList <String > getPackages (File jarFile ) {
21+
22+ Set <String > packages = new HashSet <>();
23+
24+ if (jarFile .getName ().endsWith (".jar" )) {
25+
26+ try {
27+ JarInputStream jarInputStream = new JarInputStream (new FileInputStream (jarFile .getAbsolutePath ()));
28+
29+ JarEntry jarEntry ;
30+ while ((jarEntry = jarInputStream .getNextJarEntry ()) != null ) {
31+
32+ if (jarEntry .getName ().contains ("META-INF" ))
33+ continue ;
34+
35+ if (jarEntry .isDirectory () || jarEntry .getName ().endsWith (".java" ))
36+ packages .add (jarEntry .getName ().replace ("/" , "." ).replaceAll ("(\\ .|\\ s)+$" , "" ));
37+ }
38+
39+ if (packages .size () > 0 ) {
40+ return new ArrayList <>(packages );
41+ }
42+ } catch (IOException e ) {
43+ e .printStackTrace ();
44+ }
45+ } else if (jarFile .isDirectory ()) {
46+
47+ for (File sub : Objects .requireNonNull (jarFile .listFiles ())) {
48+ //FileUtils.listFilesAndDirs(file, new NotFileFilter(TrueFileFilter.INSTANCE), DirectoryFileFilter.DIRECTORY)) {
49+
50+ String subDir = sub .getPath ().replace (jarFile .getPath (), "" );
51+
52+ if (subDir .length () > 1 ) {
53+ packages .add (subDir
54+ .replace ("/" , "." )
55+ .substring (1 ));
56+ }
57+ }
58+ return new ArrayList <>(packages );
59+ }
60+ return new ArrayList <>();
61+ }
62+
63+ public static String getRootPackages (File jarFile ) {
64+
65+ return StringUtils .join (getRootPackages (getPackages (jarFile )), ":" );
66+
67+ }
68+
69+ /**
70+ * Returns root packages from given list of packages.
71+ *
72+ * @param packages
73+ * @return
74+ */
75+ public static ArrayList <String > getRootPackages (ArrayList <String > packages ) {
76+
77+ ArrayList <String > rootPackages = new ArrayList <>();
78+
79+ Collections .sort (packages );
80+ String currentRoot = packages .get (0 );
81+ rootPackages .add (currentRoot );
82+
83+ for (String pack : packages ) {
84+
85+ /* if (!pack.contains(currentRoot)) {
86+ currentRoot = pack;
87+ }*/
88+
89+ if (pack .length () >= currentRoot .length () && !currentRoot .equals (pack .substring (0 , currentRoot .length ()))) {
90+ rootPackages .add (pack );
91+ currentRoot = pack ;
92+ }
93+ }
94+ return rootPackages ;
95+ }
96+ }
0 commit comments