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.guesser;
20  
21  import java.util.Arrays;
22  import java.util.Locale;
23  
24  import org.apache.rat.api.Document;
25  
26  /**
27   * A class that determines if a file is a Note file. e.g. NOTICE, README, CHANGELOG, etc.
28   */
29  public final class NoteGuesser {
30      /** The character called a dot, fullstop, or period. */
31      private static final String DOT = ".";
32  
33      /**
34       * The list of note file names.
35       */
36      private static final String[] NOTE_FILE_NAMES = {
37          "NOTICE", "LICENSE",
38          "LICENSE.TXT", "NOTICE.TXT",
39          "INSTALL", "INSTALL.TXT",
40          "README", "README.TXT",
41          "NEWS", "NEWS.TXT",
42          "AUTHOR", "AUTHOR.TXT",
43          "AUTHORS", "AUTHORS.txt",
44          "CHANGELOG", "CHANGELOG.TXT",
45          "DISCLAIMER", "DISCLAIMER.TXT",
46          "KEYS", "KEYS.TXT",
47          "RELEASE-NOTES", "RELEASE-NOTES.TXT",
48          "RELEASE_NOTES", "RELEASE_NOTES.TXT",
49          "UPGRADE", "UPGRADE.TXT",
50          "STATUS", "STATUS.TXT",
51          "THIRD_PARTY_NOTICES", "THIRD_PARTY_NOTICES.TXT",
52          "COPYRIGHT", "COPYRIGHT.TXT",
53          "BUILDING", "BUILDING.TXT",
54          "BUILD", "BUILT.TXT",
55          "DEPENDENCIES"
56      };
57  
58      /**
59       * List of note file extensions. Extensions that indicate a file is a note file.
60       */
61      private static final String[] NOTE_FILE_EXTENSIONS = {
62          "LICENSE", "LICENSE.TXT",
63          "NOTICE", "NOTICE.TXT",
64      };
65  
66      private NoteGuesser() {
67          // do not instantiate.
68      }
69  
70      /**
71       * Determines if the document is a note.
72       *
73       * @param document the document to check.
74       * @return {@code true} if the document is a note.
75       */
76      public static boolean isNote(final Document document) {
77          if (document == null) {
78              return false;
79          }
80  
81          String normalisedName = document.getName().getShortName().toUpperCase(Locale.US);
82          if (Arrays.asList(NoteGuesser.NOTE_FILE_NAMES).contains(normalisedName)) {
83              return true;
84          }
85  
86          for (String extension : NoteGuesser.NOTE_FILE_EXTENSIONS) {
87              if (normalisedName.endsWith(DOT + extension)) {
88                  return true;
89              }
90          }
91  
92          return false;
93      }
94  
95  }