forked from reposense/RepoSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupConfigCsvParser.java
More file actions
82 lines (68 loc) · 2.79 KB
/
GroupConfigCsvParser.java
File metadata and controls
82 lines (68 loc) · 2.79 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
80
81
82
package reposense.parser;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.apache.commons.csv.CSVRecord;
import reposense.model.FileType;
import reposense.model.GroupConfiguration;
import reposense.model.RepoLocation;
/**
* Container for the values parsed from {@code group-config.csv} file.
*/
public class GroupConfigCsvParser extends CsvParser<GroupConfiguration> {
public static final String GROUP_CONFIG_FILENAME = "group-config.csv";
/**
* Positions of the elements of a line in group-config.csv config file
*/
private static final int LOCATION_POSITION = 0;
private static final int GROUP_NAME_POSITION = 1;
private static final int FILES_GLOB_POSITION = 2;
public GroupConfigCsvParser(Path csvFilePath) throws IOException {
super(csvFilePath);
}
/**
* Gets the list of positions that are mandatory for verification.
*/
@Override
protected int[] mandatoryPositions() {
return new int[] {
LOCATION_POSITION, GROUP_NAME_POSITION, FILES_GLOB_POSITION,
};
}
/**
* Processes the csv file line by line and adds created {@code Group} into {@code results}.
*/
@Override
protected void processLine(List<GroupConfiguration> results, CSVRecord record) throws InvalidLocationException {
String location = get(record, LOCATION_POSITION);
String groupName = get(record, GROUP_NAME_POSITION);
List<String> globList = getAsList(record, FILES_GLOB_POSITION);
GroupConfiguration groupConfig = null;
groupConfig = findMatchingGroupConfiguration(results, location);
FileType group = new FileType(groupName, globList);
if (groupConfig.containsGroup(group)) {
logger.warning(String.format(
"Skipping group as %s has already been specified for the repository %s",
group.toString(), groupConfig.getLocation()));
return;
}
groupConfig.addGroup(group);
}
/**
* Gets an existing {@code GroupConfiguration} from {@code results} if {@code location} matches.
* Otherwise adds a newly created {@code GroupConfiguration} into {@code results} and returns it.
*
* @throws InvalidLocationException if {@code location} is invalid.
*/
private static GroupConfiguration findMatchingGroupConfiguration(
List<GroupConfiguration> results, String location) throws InvalidLocationException {
GroupConfiguration config = new GroupConfiguration(new RepoLocation(location));
for (GroupConfiguration groupConfig : results) {
if (groupConfig.getLocation().equals(config.getLocation())) {
return groupConfig;
}
}
results.add(config);
return config;
}
}