usability improvement
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / ClassGraph.java
index 27eff03..62e0911 100755 (executable)
@@ -12,10 +12,13 @@ package eu.svjatoslav.inspector.java.structure;
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import eu.svjatoslav.commons.file.CommonPathResolver;
+import eu.svjatoslav.commons.string.WildCardMatcher;
 import eu.svjatoslav.inspector.java.methods.Clazz;
 import eu.svjatoslav.inspector.java.methods.ProjectScanner;
 
@@ -26,7 +29,14 @@ public class ClassGraph {
         */
        private final Map<String, ClassDescriptor> fullyQualifiedNameToClassMap = new HashMap<String, ClassDescriptor>();
 
-       private final Filter filter = new Filter();
+       private final List<String> blacklistClassPatterns = new ArrayList<String>();
+
+       private final List<String> whitelistClassPatterns = new ArrayList<String>();
+
+       private String targetDirectory = CommonPathResolver.getDesktopDirectory()
+                       .getAbsolutePath() + "/";
+
+       private boolean keepDotFile;
 
        public ClassGraph() {
        }
@@ -69,33 +79,8 @@ public class ClassGraph {
                        }
        }
 
-       /**
-        * @param resultFileName
-        *            file name for the generated graph. Existing file with the same
-        *            name will be overwritten.
-        */
-       public void generateGraph(final String resultFileName) {
-               generateGraph(resultFileName, false);
-       }
-
-       /**
-        * @param resultFileName
-        *            file name for the generated graph. File extension will be
-        *            added automatically. Existing file with the same name will be
-        *            overwritten.
-        *
-        * @param keepDotFile
-        *            if set to <code>true</code> then intermediary GraphViz DOT
-        *            file will be kept.
-        */
-
-       public void generateGraph(final String resultFileName,
-                       final boolean keepDotFile) {
-
-               final String desktopPath = CommonPathResolver.getDesktopDirectory()
-                               .getAbsolutePath() + "/";
-
-               generateGraph(desktopPath, resultFileName, keepDotFile);
+       public void blacklistClassPattern(final String pattern) {
+               blacklistClassPatterns.add(pattern);
        }
 
        /**
@@ -112,11 +97,7 @@ public class ClassGraph {
         *            file will be kept.
         */
 
-       public void generateGraph(String targetDirectory,
-                       final String resultFileName, final boolean keepDotFile) {
-
-               if (!targetDirectory.endsWith("/"))
-                       targetDirectory += "/";
+       public void generateGraph(final String resultFileName) {
 
                final String dotFilePath = targetDirectory + resultFileName + ".dot";
                final String imageFilePath = targetDirectory + resultFileName + ".png";
@@ -132,17 +113,15 @@ public class ClassGraph {
                        // execute GraphViz to visualize graph
                        try {
                                Runtime.getRuntime()
-                                               .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
-                                                               imageFilePath }).waitFor();
+                               .exec(new String[] { "dot", "-Tpng", dotFilePath, "-o",
+                                               imageFilePath }).waitFor();
                        } catch (final InterruptedException e) {
                        } finally {
                        }
 
-                       if (!keepDotFile) {
+                       if (!keepDotFile)
                                // delete dot file
-                               final File dotFile = new File(dotFilePath);
-                               dotFile.delete();
-                       }
+                               new File(dotFilePath).delete();
                } catch (final IOException e) {
                        System.err.println(e);
                }
@@ -165,10 +144,6 @@ public class ClassGraph {
                return resultStr;
        }
 
-       public Filter getFilter() {
-               return filter;
-       }
-
        /**
         * @param clazz
         *            class that shall be added to graph
@@ -197,12 +172,49 @@ public class ClassGraph {
        /**
         * Hide orphaned class that have no references
         */
-       public void hideOrphanedClasses() {
+       public ClassGraph hideOrphanedClasses() {
 
                for (final ClassDescriptor classDescriptor : fullyQualifiedNameToClassMap
                                .values())
                        classDescriptor.hideClassIfNoReferences();
 
+               return this;
+       }
+
+       protected boolean isClassShown(final String className) {
+               for (final String pattern : blacklistClassPatterns)
+                       if (WildCardMatcher.match(className, pattern))
+                               return false;
+
+               if (!whitelistClassPatterns.isEmpty()) {
+                       for (final String pattern : whitelistClassPatterns)
+                               if (WildCardMatcher.match(className, pattern))
+                                       return true;
+                       return false;
+               }
+
+               return true;
+       }
+
+       public ClassGraph setKeepDotFile(final boolean keepDotFile) {
+               this.keepDotFile = keepDotFile;
+
+               return this;
+       }
+
+       public ClassGraph setTargetDirectory(String directoryPath) {
+               if (!directoryPath.endsWith("/"))
+                       directoryPath += "/";
+
+               targetDirectory = directoryPath;
+
+               return this;
+       }
+
+       public ClassGraph whitelistClassPattern(final String pattern) {
+               whitelistClassPatterns.add(pattern);
+
+               return this;
        }
 
 }