View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Convenience methods for file and path handling.
35   *
36   * @since 1.0.0
37   */
38  public final class FileHandlingUtil {
39  
40    private static final Logger LOG = LoggerFactory.getLogger(FileHandlingUtil.class);
41  
42    private FileHandlingUtil() {
43      // do not instantiate
44    }
45  
46    /**
47     * <p>constructRelativeFile.</p>
48     *
49     * @param rootDirectory to be relative to
50     * @param file to get relative path for
51     * @return file with relative path
52     * @since 3.0.0
53     */
54    public static File constructRelativeFile(File rootDirectory, File file) {
55      return new File(constructRelativePath(rootDirectory, file));
56    }
57  
58    /**
59     * <p>constructRelativeFile.</p>
60     *
61     * @param rootPath to be relative to
62     * @param filePath to make relative
63     * @return file with relative path
64     * @since 3.0.0
65     */
66    public static File constructRelativeFile(String rootPath, String filePath) {
67      return new File(constructRelativePath(rootPath, filePath));
68    }
69  
70    /**
71     * <p>constructRelativePath.</p>
72     *
73     * @param rootDirectory to be relative to
74     * @param file to get relative path for
75     * @return relative path for file
76     * @since 3.0.0
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    * <p>constructRelativePath.</p>
101    *
102    * @param rootPath to be relative to
103    * @param filePath to make relative
104    * @return file with relative path
105    * @since 3.0.0
106    */
107   public static String constructRelativePath(String rootPath, String filePath) {
108     return StringUtils.difference(rootPath, filePath);
109   }
110 
111   /**
112    * <p>ensureParent.</p>
113    *
114    * @param file will have an existing parent directory on success
115    * @since 3.0.0
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 }