1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.qa.glnm.util;
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.galenframework.utils.GalenUtils;
30
31 import io.wcm.qa.glnm.exceptions.GaleniumException;
32
33
34
35
36
37
38 public final class FileHandlingUtil {
39
40 private static final Logger LOG = LoggerFactory.getLogger(FileHandlingUtil.class);
41
42 private FileHandlingUtil() {
43
44 }
45
46
47
48
49
50
51
52
53
54 public static File constructRelativeFile(File rootDirectory, File file) {
55 return new File(constructRelativePath(rootDirectory, file));
56 }
57
58
59
60
61
62
63
64
65
66 public static File constructRelativeFile(String rootPath, String filePath) {
67 return new File(constructRelativePath(rootPath, filePath));
68 }
69
70
71
72
73
74
75
76
77
78 public static String constructRelativePath(File rootDirectory, File file) {
79 if (LOG.isTraceEnabled()) {
80 LOG.trace("attempt making '" + file + "' relative to '" + rootDirectory + "'");
81 }
82 try {
83 String rootPath = rootDirectory.getCanonicalPath();
84 String filePath = file.getCanonicalPath();
85 if (LOG.isTraceEnabled()) {
86 LOG.trace("constructing path for '" + filePath + "' relative to '" + rootPath + "'");
87 }
88 String relativePath = constructRelativePath(rootPath, filePath);
89 if (LOG.isTraceEnabled()) {
90 LOG.trace("relative path: '" + relativePath + "'");
91 }
92 return relativePath;
93 }
94 catch (IOException ex) {
95 throw new GaleniumException("when constructing relative file path.", ex);
96 }
97 }
98
99
100
101
102
103
104
105
106
107 public static String constructRelativePath(String rootPath, String filePath) {
108 return StringUtils.difference(rootPath, filePath);
109 }
110
111
112
113
114
115
116
117 public static void ensureParent(File file) {
118 File parentFile = file.getParentFile();
119 if (!parentFile.isDirectory()) {
120 try {
121 if (LOG.isDebugEnabled()) {
122 LOG.debug("ensuring directory exists: " + parentFile.getPath());
123 }
124 GalenUtils.makeSureFolderExists(parentFile);
125 }
126 catch (IOException ex) {
127 throw new GaleniumException("problem when ensuring directory is present", ex);
128 }
129 }
130 }
131
132 }