Changed license to LGPLv3.
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / methods / ProjectScanner.java
1 /*
2  * JavaInspect - Utility to visualize java software
3  * Copyright (C) 2013-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.inspector.java.methods;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import eu.svjatoslav.commons.file.FilePathParser;
20 import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
21
22 public class ProjectScanner {
23
24         private final File scanPath;
25
26         Map<File, Project> projects = new HashMap<File, Project>();
27         public List<JavaFile> javaFiles = new ArrayList<JavaFile>();
28
29         public ProjectScanner(final File projectPath) {
30                 scanPath = projectPath;
31                 parse();
32         }
33
34         public List<Clazz> getAllClasses() {
35                 final List<Clazz> result = new ArrayList<Clazz>();
36
37                 for (final JavaFile file : javaFiles)
38                         for (final Clazz clazz : file.classes)
39                                 result.add(clazz);
40
41                 return result;
42         }
43
44         public void parse() {
45
46                 if (!scanPath.exists())
47                         System.out.println("Path not found: " + scanPath);
48
49                 if (!scanPath.canRead())
50                         System.out.println("Cannot read path: " + scanPath);
51
52                 if (scanPath.isDirectory())
53                         parseDirectory(scanPath);
54
55                 if (scanPath.isFile())
56                         parseFile(scanPath);
57         }
58
59         public void parseDirectory(final File file) {
60
61                 for (final File subFile : file.listFiles()) {
62
63                         if (subFile.isFile())
64                                 parseFile(subFile);
65
66                         if (subFile.isDirectory())
67                                 parseDirectory(subFile);
68                 }
69         }
70
71         public void parseFile(final File file) {
72                 final String fileExtension = FilePathParser.getFileExtension(file);
73                 if ("java".equalsIgnoreCase(fileExtension))
74                         try {
75                                 final JavaFile javaFile = new JavaFile(file);
76                                 javaFiles.add(javaFile);
77                         } catch (final IOException e) {
78                                 System.out.println("Error parsing file: " + file.toString()
79                                                 + ": " + e.toString());
80                         } catch (final InvalidSyntaxException e) {
81                                 System.out.println("Syntax error occured while parsing file: "
82                                                 + file.toString() + ": " + e.toString());
83                         }
84         }
85
86 }