Skip to content

Commit f3e3678

Browse files
committed
made more methods static and changed KeyValueReader from interface to static class so that methods could be better broken up
1 parent 6f9da4f commit f3e3678

7 files changed

Lines changed: 56 additions & 43 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,9 @@ verifier.isValid(bag, ignoreHiddenFiles);
9595
##### Quickly verify by payload-oxum
9696
```java
9797
boolean ignoreHiddenFiles = true;
98-
BagVerifier verifier = new BagVerifier();
9998

100-
if(verifier.canQuickVerify(bag)){
101-
verifier.quicklyVerify(bag, ignoreHiddenFiles);
99+
if(BagVerifier.canQuickVerify(bag)){
100+
BagVerifier.quicklyVerify(bag, ignoreHiddenFiles);
102101
}
103102
```
104103

src/main/java/gov/loc/repository/bagit/conformance/profile/BagitProfileDeserializer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public BagitProfile deserialize(final JsonParser p, final DeserializationContext
6161
return profile;
6262
}
6363

64-
private void parseBagitProfileInfo(final JsonNode node, final BagitProfile profile){
64+
private static void parseBagitProfileInfo(final JsonNode node, final BagitProfile profile){
6565
final JsonNode bagitProfileInfoNode = node.get("BagIt-Profile-Info");
6666
logger.debug("Parsing the BagIt-Profile-Info section");
6767

@@ -91,7 +91,7 @@ private void parseBagitProfileInfo(final JsonNode node, final BagitProfile profi
9191
}
9292

9393
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
94-
private Map<String, BagInfoRequirement> parseBagInfo(final JsonNode rootNode){
94+
private static Map<String, BagInfoRequirement> parseBagInfo(final JsonNode rootNode){
9595
final JsonNode bagInfoNode = rootNode.get("Bag-Info");
9696
logger.debug("Parsing the Bag-Info section");
9797
final Map<String, BagInfoRequirement> bagInfo = new HashMap<>();
@@ -118,7 +118,7 @@ private Map<String, BagInfoRequirement> parseBagInfo(final JsonNode rootNode){
118118
return bagInfo;
119119
}
120120

121-
private List<String> parseManifestTypesRequired(final JsonNode node){
121+
private static List<String> parseManifestTypesRequired(final JsonNode node){
122122
final JsonNode manifests = node.get("Manifests-Required");
123123

124124
final List<String> manifestTypes = new ArrayList<>();
@@ -132,7 +132,7 @@ private List<String> parseManifestTypesRequired(final JsonNode node){
132132
return manifestTypes;
133133
}
134134

135-
private List<String> parseAcceptableSerializationFormats(final JsonNode node){
135+
private static List<String> parseAcceptableSerializationFormats(final JsonNode node){
136136
final JsonNode serialiationFormats = node.get("Accept-Serialization");
137137
final List<String> serialTypes = new ArrayList<>();
138138

@@ -144,7 +144,7 @@ private List<String> parseAcceptableSerializationFormats(final JsonNode node){
144144
return serialTypes;
145145
}
146146

147-
private List<String> parseRequiredTagmanifestTypes(final JsonNode node){
147+
private static List<String> parseRequiredTagmanifestTypes(final JsonNode node){
148148
final JsonNode tagManifestsRequiredNodes = node.get("Tag-Manifests-Required");
149149
final List<String> requiredTagmanifestTypes = new ArrayList<>();
150150

@@ -156,7 +156,7 @@ private List<String> parseRequiredTagmanifestTypes(final JsonNode node){
156156
return requiredTagmanifestTypes;
157157
}
158158

159-
private List<String> parseRequiredTagFiles(final JsonNode node){
159+
private static List<String> parseRequiredTagFiles(final JsonNode node){
160160
final JsonNode tagFilesRequiredNodes = node.get("Tag-Files-Required");
161161
final List<String> requiredTagFiles = new ArrayList<>();
162162

@@ -168,7 +168,7 @@ private List<String> parseRequiredTagFiles(final JsonNode node){
168168
return requiredTagFiles;
169169
}
170170

171-
private List<String> parseAcceptableVersions(final JsonNode node){
171+
private static List<String> parseAcceptableVersions(final JsonNode node){
172172
final JsonNode acceptableVersionsNodes = node.get("Accept-BagIt-Version");
173173
final List<String> acceptableVersions = new ArrayList<>();
174174

src/main/java/gov/loc/repository/bagit/reader/BagitTextFileReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static SimpleImmutableEntry<Version, Charset> readBagitTextFile(final Pat
5858
throw new InvalidBagitFileFormatException("bagit.txt MUST contain 'BagIt-Version' AND 'Tag-File-Character-Encoding' entries!");
5959
}
6060

61-
return new SimpleImmutableEntry<Version, Charset>(parseVersion(version), encoding);
61+
return new SimpleImmutableEntry<>(parseVersion(version), encoding);
6262
}
6363

6464
/*

src/main/java/gov/loc/repository/bagit/reader/KeyValueReader.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414

1515
import gov.loc.repository.bagit.exceptions.InvalidBagMetadataException;
1616

17-
public interface KeyValueReader {
18-
Logger logger = LoggerFactory.getLogger(KeyValueReader.class);
17+
public final class KeyValueReader {
18+
private static final Logger logger = LoggerFactory.getLogger(KeyValueReader.class);
1919

20+
private KeyValueReader(){
21+
//intentionall left blank
22+
}
23+
2024
/*
2125
* Generic method to read key value pairs from the bagit files, like bagit.txt or bag-info.txt
2226
*/
@@ -33,36 +37,21 @@ public interface KeyValueReader {
3337
* @throws InvalidBagMetadataException if the file does not conform to pattern of key value
3438
*/
3539
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
36-
static List<SimpleImmutableEntry<String, String>> readKeyValuesFromFile(final Path file, final String splitRegex, final Charset charset) throws IOException, InvalidBagMetadataException{
37-
//TODO refactor into smaller methods
40+
public static List<SimpleImmutableEntry<String, String>> readKeyValuesFromFile(final Path file, final String splitRegex, final Charset charset) throws IOException, InvalidBagMetadataException{
3841
final List<SimpleImmutableEntry<String, String>> keyValues = new ArrayList<>();
3942

4043
try(final BufferedReader reader = Files.newBufferedReader(file, charset)){
4144
String line = reader.readLine();
4245
while(line != null){
43-
if(line.matches("^\\s+.*")){
44-
final SimpleImmutableEntry<String, String> oldKeyValue = keyValues.remove(keyValues.size() -1);
45-
final SimpleImmutableEntry<String, String> newKeyValue = new SimpleImmutableEntry<String, String>(oldKeyValue.getKey(), oldKeyValue.getValue() + System.lineSeparator() +line);
46-
keyValues.add(newKeyValue);
47-
48-
logger.debug("Found an indented line - merging it with key [{}]", oldKeyValue.getKey());
46+
if(line.matches("^\\s+.*") && !keyValues.isEmpty()){
47+
mergeIndentedLine(line, keyValues);
4948
}
5049
else{
51-
final String[] parts = line.split(splitRegex, 2);
52-
if(parts.length != 2){
53-
final StringBuilder message = new StringBuilder(300);
54-
message.append("Line [").append(line)
55-
.append("] does not meet the bagit specification for a bag tag file. Perhaps you meant to indent it " +
56-
"by a space or a tab? Or perhaps you didn't use a colon to separate the key from the value?" +
57-
"It must follow the form of <key>:<value> or if continuing from another line must be indented " +
58-
"by a space or a tab.");
59-
60-
throw new InvalidBagMetadataException(message.toString());
61-
}
50+
final String[] parts = checkLineFormat(line, splitRegex);
6251
final String key = parts[0].trim();
6352
final String value = parts[1].trim();
64-
logger.debug("Found key [{}] value [{}] in file [{}] using regex [{}]", key, value, file, splitRegex);
65-
keyValues.add(new SimpleImmutableEntry<String, String>(key, value));
53+
logger.debug("Found key [{}] value [{}] in file [{}] using split regex [{}]", key, value, file, splitRegex);
54+
keyValues.add(new SimpleImmutableEntry<>(key, value));
6655
}
6756

6857
line = reader.readLine();
@@ -71,4 +60,30 @@ static List<SimpleImmutableEntry<String, String>> readKeyValuesFromFile(final Pa
7160

7261
return keyValues;
7362
}
63+
64+
private static void mergeIndentedLine(final String line, final List<SimpleImmutableEntry<String, String>> keyValues){
65+
final SimpleImmutableEntry<String, String> oldKeyValue = keyValues.remove(keyValues.size() -1);
66+
final SimpleImmutableEntry<String, String> newKeyValue = new SimpleImmutableEntry<>(oldKeyValue.getKey(), oldKeyValue.getValue() + System.lineSeparator() +line);
67+
keyValues.add(newKeyValue);
68+
69+
logger.debug("Found an indented line - merging it with key [{}]", oldKeyValue.getKey());
70+
}
71+
72+
private static String[] checkLineFormat(final String line, final String splitRegex) throws InvalidBagMetadataException{
73+
final String[] parts = line.split(splitRegex, 2);
74+
75+
if(parts.length != 2){
76+
final StringBuilder message = new StringBuilder(300);
77+
message.append("Line [").append(line)
78+
.append("] does not meet the bagit specification for a bag tag file. Perhaps you meant to indent it " +
79+
"by a space or a tab? Or perhaps you didn't use a colon to separate the key from the value?" +
80+
"It must follow the form of <key>")
81+
.append(splitRegex)
82+
.append("<value> or if continuing from another line must be indented by a space or a tab.");
83+
84+
throw new InvalidBagMetadataException(message.toString());
85+
}
86+
87+
return parts;
88+
}
7489
}

src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected void finalize() throws Throwable {
9191
* @param bag the {@link Bag} object you wish to check
9292
* @return true if the bag can be quickly verified
9393
*/
94-
public boolean canQuickVerify(final Bag bag){
94+
public static boolean canQuickVerify(final Bag bag){
9595
return QuickVerifier.canQuickVerify(bag);
9696
}
9797

@@ -106,7 +106,7 @@ public boolean canQuickVerify(final Bag bag){
106106
* @throws PayloadOxumDoesNotExistException if the bag does not contain a payload-oxum.
107107
* To check, run {@link BagVerifier#canQuickVerify}
108108
*/
109-
public void quicklyVerify(final Bag bag) throws IOException, InvalidPayloadOxumException{
109+
public static void quicklyVerify(final Bag bag) throws IOException, InvalidPayloadOxumException{
110110
QuickVerifier.quicklyVerify(bag);
111111
}
112112

src/main/java/gov/loc/repository/bagit/verify/PayloadVerifier.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ private Set<Path> getAllFilesListedInManifests(final Bag bag)
138138
* Make sure all the listed files actually exist
139139
*/
140140
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
141-
private void checkAllFilesListedInManifestExist(final Set<Path> files)
142-
throws FileNotInPayloadDirectoryException, InterruptedException {//TODO
141+
private void checkAllFilesListedInManifestExist(final Set<Path> files) throws FileNotInPayloadDirectoryException, InterruptedException {
143142
final CountDownLatch latch = new CountDownLatch(files.size());
144143
final Set<Path> missingFiles = new ConcurrentSkipListSet<>();
145144

@@ -159,7 +158,7 @@ private void checkAllFilesListedInManifestExist(final Set<Path> files)
159158
/*
160159
* Make sure all files in the directory are in at least 1 manifest
161160
*/
162-
private void checkAllFilesInPayloadDirAreListedInAtLeastOneAManifest(final Set<Path> filesListedInManifests,
161+
private static void checkAllFilesInPayloadDirAreListedInAtLeastOneAManifest(final Set<Path> filesListedInManifests,
163162
final Path payloadDir, final boolean ignoreHiddenFiles) throws IOException {
164163
logger.debug("Checking if all payload files (files in {} dir) are listed in at least one manifest", payloadDir);
165164
if (Files.exists(payloadDir)) {
@@ -171,7 +170,7 @@ private void checkAllFilesInPayloadDirAreListedInAtLeastOneAManifest(final Set<P
171170
/*
172171
* as per the bagit-spec 1.0+ all files have to be listed in all manifests
173172
*/
174-
private void CheckAllFilesInPayloadDirAreListedInAllManifests(final Set<Manifest> payLoadManifests,
173+
private static void CheckAllFilesInPayloadDirAreListedInAllManifests(final Set<Manifest> payLoadManifests,
175174
final Path payloadDir, final boolean ignoreHiddenFiles) throws IOException {
176175
logger.debug("Checking if all payload files (files in {} dir) are listed in all manifests", payloadDir);
177176
if (Files.exists(payloadDir)) {

src/test/java/gov/loc/repository/bagit/verify/BagVerifierTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ public void testManifestsWithLeadingDotSlash() throws Exception{
137137
@Test
138138
public void testCanQuickVerify() throws Exception{
139139
Bag bag = reader.read(rootDir);
140-
boolean canQuickVerify = sut.canQuickVerify(bag);
140+
boolean canQuickVerify = BagVerifier.canQuickVerify(bag);
141141
assertFalse("Since " + bag.getRootDir() + " DOES NOT contain the metadata Payload-Oxum then it should return false!", canQuickVerify);
142142

143143
Path passingRootDir = Paths.get(new File("src/test/resources/bags/v0_94/bag").toURI());
144144
bag = reader.read(passingRootDir);
145-
canQuickVerify = sut.canQuickVerify(bag);
145+
canQuickVerify = BagVerifier.canQuickVerify(bag);
146146
assertTrue("Since " + bag.getRootDir() + " DOES contain the metadata Payload-Oxum then it should return true!", canQuickVerify);
147147
}
148148

@@ -151,7 +151,7 @@ public void testQuickVerify() throws Exception{
151151
Path passingRootDir = Paths.get(new File("src/test/resources/bags/v0_94/bag").toURI());
152152
Bag bag = reader.read(passingRootDir);
153153

154-
sut.quicklyVerify(bag);
154+
BagVerifier.quicklyVerify(bag);
155155
}
156156

157157
}

0 commit comments

Comments
 (0)