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.rat.document.impl.guesser;
20  
21  import java.util.Locale;
22  
23  import org.apache.rat.api.Document;
24  
25  public class ArchiveGuesser {
26  
27      private static final String DOT = ".";
28  
29      private static final String[] ARCHIVE_EXTENSIONS = {
30          "jar", "gz",
31          "zip", "tar",
32          "bz", "bz2",
33          "rar", "war",
34          "ear", "mar",
35          "par", "xar",
36          "odb", "odf",
37          "odg", "odp",
38          "ods", "odt",
39          "har", "sar",
40          "wsr",
41      };
42  
43      /**
44       * @param document the current document.
45       * @return whether the given document is an archive.
46       */
47      public static boolean isArchive(final Document document) {
48          return isArchive(document.getName());
49      }
50  
51      /**
52       * @return Is a file by that name an archive?
53       * @param name file name to check against.
54       */
55      public static boolean isArchive(final String name) {
56          if (name == null) {return false;}
57          String nameToLower = name.toLowerCase(Locale.US);
58          for (int i = 0; i < ArchiveGuesser.ARCHIVE_EXTENSIONS.length; i++) {
59              if (nameToLower.endsWith(DOT + ArchiveGuesser.ARCHIVE_EXTENSIONS[i])) {
60                  return true;
61              }
62          }
63          return false;
64      }
65  
66  }