Use common library to read file content.
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / methods / ProjectScanner.java
1 /*
2  * JavaInspect - Utility to visualize java software
3  * Copyright (C) 2013-2020, 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 eu.svjatoslav.commons.file.FilePathParser;
13 import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 public class ProjectScanner {
23
24     public final List<JavaFile> javaFiles = new ArrayList<>();
25     private final File scanPath;
26     Map<File, Project> projects = new HashMap<>();
27
28     public ProjectScanner(final File projectPath) {
29         scanPath = projectPath;
30         parse();
31     }
32
33     public List<Clazz> getAllClasses() {
34         final List<Clazz> result = new ArrayList<>();
35
36         for (final JavaFile file : javaFiles)
37             result.addAll(file.classes);
38
39         return result;
40     }
41
42     public void parse() {
43
44         if (!scanPath.exists())
45             System.out.println("Path not found: " + scanPath);
46
47         if (!scanPath.canRead())
48             System.out.println("Cannot read path: " + scanPath);
49
50         if (scanPath.isDirectory())
51             parseDirectory(scanPath);
52
53         if (scanPath.isFile())
54             parseFile(scanPath);
55     }
56
57     public void parseDirectory(final File file) {
58
59         File[] filesList = file.listFiles();
60         if (filesList == null) throw new RuntimeException("Cannot scan directory: " + file);
61
62         for (final File subFile : filesList) {
63
64             if (subFile.isFile())
65                 parseFile(subFile);
66
67             if (subFile.isDirectory())
68                 parseDirectory(subFile);
69         }
70     }
71
72     public void parseFile(final File file) {
73         final String fileExtension = FilePathParser.getFileExtension(file);
74         if ("java".equalsIgnoreCase(fileExtension))
75             try {
76                 final JavaFile javaFile = new JavaFile(file);
77                 javaFiles.add(javaFile);
78             } catch (final IOException e) {
79                 System.out.println("Error parsing file: " + file.toString()
80                         + ": " + e.toString());
81             } catch (final InvalidSyntaxException e) {
82                 System.out.println("Syntax error occured while parsing file: "
83                         + file.toString() + ": " + e.toString());
84             }
85     }
86
87 }