View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.utils;
20  
21  import java.io.File;
22  import java.util.Locale;
23  
24  import org.codehaus.plexus.components.io.fileselectors.FileInfo;
25  
26  /**
27   *
28   */
29  public final class AssemblyFileUtils {
30  
31      private AssemblyFileUtils() {
32          // no op
33      }
34  
35      public static String makePathRelativeTo(String path, final File basedir) {
36          if (basedir == null) {
37              return path;
38          }
39  
40          if (path == null) {
41              return null;
42          }
43  
44          path = path.trim();
45  
46          String base = basedir.getAbsolutePath();
47          if (path.startsWith(base)) {
48              path = path.substring(base.length());
49              if (path.length() > 0) {
50                  if (path.startsWith("/") || path.startsWith("\\")) {
51                      path = path.substring(1);
52                  }
53              }
54  
55              if (path.length() == 0) {
56                  path = ".";
57              }
58          }
59  
60          if (!new File(path).isAbsolute()) {
61              path = path.replace('\\', '/');
62          }
63  
64          return path;
65      }
66  
67      @SuppressWarnings("ResultOfMethodCallIgnored")
68      public static void verifyTempDirectoryAvailability(final File tempDir) {
69          if (!tempDir.exists()) {
70              tempDir.mkdirs();
71          }
72      }
73  
74      private static String normalizePath(String path) {
75          return path.replace('\\', '/');
76      }
77  
78      public static String normalizeFileInfo(FileInfo fileInfo) {
79          String name = fileInfo.getName();
80          name = normalizePath(name);
81          return name.replace(File.separatorChar, '/'); // How can this be anything but a no-op
82      }
83  
84      public static boolean isPropertyFile(String sourceName) {
85          return sourceName.toLowerCase(Locale.ENGLISH).endsWith(".properties");
86      }
87  
88      public static boolean isPropertyFile(File file) {
89          return isPropertyFile(file.getName());
90      }
91  
92      /**
93       * This method exists because {@link File#isAbsolute()} is not OS independent.
94       *
95       * In addition to the check in {@link File#isAbsolute()} we will also test for a leading '/'.
96       *
97       * @return {@code true} if {@code File#isAbsolute()} or starts with a '/'
98       */
99      public static boolean isAbsolutePath(File dir) {
100         return dir != null && (dir.isAbsolute() || dir.getPath().startsWith("\\")); // on Win* platforms
101     }
102 }