-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileSystem.java
More file actions
79 lines (70 loc) · 2.48 KB
/
Copy pathFileSystem.java
File metadata and controls
79 lines (70 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Source Code Analyzer is part of the larger Source Code Analyzer repository on https://github.com/Hayawi/SourceCodeAnalyzer
* Author: Yahya Ismail
* This project is under the MIT License so go wild
*
* FileSystem contains necessary methods to allow for File manipulation and retrieval
*/
package org.sourcecodeanalyzer;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import javax.swing.JFileChooser;
public class FileSystem {
//returns the contents of the file line by line
public static String[] fileContents(String fileURL) {
File file = new File(fileURL);
Charset charset = Charset.forName("utf-8");
ArrayList<String> contents = new ArrayList<String>();
try(BufferedReader reader = Files.newBufferedReader(file.toPath(), charset))
{
String line = null;
while ((line = reader.readLine()) != null) {
contents.add(line);
}
} catch (IOException e){
System.err.format("IOException: %s%n", e);
}
String[] a = new String[contents.size()];
for (int i = 0; i < a.length; i++){
a[i] = contents.get(i);
}
return a;
}
//Opens a file browser and returns it's path if it's a valid source file, otherwise returns "Invalid Source File"
public static String chooseFile() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
int returnVal = chooser.showOpenDialog(null);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return ("Invalid Source File");
}
if (validateFile(chooser.getSelectedFile().getName()))
return chooser.getSelectedFile().getAbsolutePath();
else
return ("Invalid Source File");
}
//helper method to validate if the source file is valid under our existing enums
private static boolean validateFile(String file){
file = file.substring(file.lastIndexOf("."));
for (SourceComments source : SourceComments.values()){
if (source.getExtension().equals(file))
return true;
}
return false;
}
//gets the enum source type based on the url extension
public static SourceComments getSourceCommentType(String fileURL){
if (!fileURL.contains("."))
return null;
fileURL = fileURL.substring(fileURL.lastIndexOf("."));
for (SourceComments source : SourceComments.values()){
if (source.getExtension().equals(fileURL))
return source;
}
return null;
}
}