-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathPathUtils.java
More file actions
85 lines (73 loc) · 2.81 KB
/
PathUtils.java
File metadata and controls
85 lines (73 loc) · 2.81 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
83
84
85
package gov.loc.repository.bagit.util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.DosFileAttributes;
import gov.loc.repository.bagit.domain.Bag;
import gov.loc.repository.bagit.domain.Version;
public final class PathUtils {
private static final String PAYLOAD_DIR_NAME = "data";
private PathUtils(){
//intentionally left blank
}
/**
* Needed to get rid of findbugs "dodgy code warnings" in regards to getting the filename of a path as a string
*
* @param path the path that you which to get the filename as a string
* @return the filename or an empty string
*/
public static String getFilename(final Path path){
String filename = "";
if(path != null){
final Path filenamePath = path.getFileName();
if(filenamePath != null){
filename = filenamePath.toString();
}
}
return filename;
}
/**
* as per https://github.com/jkunze/bagitspec/commit/152d42f6298b31a4916ea3f8f644ca4490494070 decode percent encoded filenames
* @param encoded the encoded filename
* @return the decoded filename
*/
public static String decodeFilname(final String encoded){
return encoded.replaceAll("%0A", "\n").replaceAll("%0D", "\r");
}
/**
* as per https://github.com/jkunze/bagitspec/commit/152d42f6298b31a4916ea3f8f644ca4490494070 encode any new lines or carriage returns
* @param path the path to encode
* @return the encoded filename
*/
public static String encodeFilename(final Path path){
return path.toString().replaceAll("\n", "%0A").replaceAll("\r", "%0D");
}
/**
* Due to the way that windows handles hidden files vs. *nix
* we use this method to determine if a file or folder is really hidden
* @param path the file or folder to check if hidden
* @return if the file or folder is hidden
* @throws IOException if there is an error reading the file/folder
*/
public static boolean isHidden(final Path path) throws IOException{
//cause Files.isHidden() doesn't work properly for windows if the file is a directory
if (System.getProperty("os.name").contains("Windows")){
return Files.readAttributes(path, DosFileAttributes.class).isHidden();
}
return Files.isHidden(path);
}
/**
* With bagit version 2.0 (.bagit)
* payload files are no longer in the "data" directory. This method accounts for this
* and will return the directory that contains the payload files
*
* @param bag that contains the payload files you want
* @return the directory that contains the payload files
*/
public static Path getDataDir(final Bag bag){
if(bag.getVersion().compareTo(new Version(2, 0)) >= 0){ //is it a .bagit version?
return bag.getRootDir();
}
return bag.getRootDir().resolve(PAYLOAD_DIR_NAME);
}
}