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.configuration;
20  
21  import java.io.File;
22  import java.lang.reflect.Constructor;
23  import java.lang.reflect.InvocationTargetException;
24  import java.net.URI;
25  import java.util.Arrays;
26  
27  import org.apache.rat.ConfigurationException;
28  
29  /**
30   * An enumeration of the types of files that can contain the configuration
31   * information.
32   */
33  public enum Format {
34      /** An XML file */
35      XML(XMLConfigurationReader.class, "xml"),
36      /** A plain text file */
37      TXT(null, "txt", "text");
38  
39      /** The list of file suffixes that this format applies to */
40      private final String[] suffix;
41  
42      /** The constructor for the MatcherReader for this Format */
43      private Constructor<MatcherReader> matcherReader;
44      /** The constructor for the LicenseReader for this Format */
45      private Constructor<LicenseReader> licenseReader;
46  
47      @SuppressWarnings("unchecked")
48      Format(final Class<?> reader, final String... suffix) {
49          if (reader != null) {
50              try {
51                  matcherReader = MatcherReader.class.isAssignableFrom(reader)
52                          ? (Constructor<MatcherReader>) reader.getConstructor()
53                          : null;
54                  licenseReader = LicenseReader.class.isAssignableFrom(reader)
55                          ? (Constructor<LicenseReader>) reader.getConstructor()
56                          : null;
57              } catch (NoSuchMethodException | SecurityException e) {
58                  throw new ConfigurationException("Error retrieving no argument constructor for " + reader.getName(), e);
59              }
60          }
61          this.suffix = suffix;
62      }
63  
64      /**
65       * @return a new instance of MatcherReader for this format.
66       */
67      public MatcherReader matcherReader() {
68          try {
69              return matcherReader == null ? null : matcherReader.newInstance();
70          } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
71                  | InvocationTargetException e) {
72              throw new ConfigurationException("Can not instantiate MatcherReader for " + this.name(), e);
73          }
74      }
75  
76      /**
77       * @return a new instance of the LicenseReader for this format.
78       */
79      public LicenseReader licenseReader() {
80          try {
81              return licenseReader == null ? null : licenseReader.newInstance();
82          } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
83                  | InvocationTargetException e) {
84              throw new ConfigurationException("Can not instantiate LicenseReader for " + this.name(), e);
85          }
86      }
87  
88      /**
89       * Determine the {@code Format} from the file name.
90       * @param name the file name to check.
91       * @return the Format
92       */
93      public static Format from(final String name) {
94          int pos = name.lastIndexOf('.');
95          String suffix = name.substring(pos + 1);
96          for (Format f : Format.values()) {
97              if (Arrays.asList(f.suffix).contains(suffix)) {
98                  return f;
99              }
100         }
101         throw new IllegalArgumentException(String.format("No such suffix: %s", suffix));
102     }
103 
104    /**
105     * Determine the {@code Format} from a URI.
106     * @param uri the URI to check.
107     * @return the Format
108     */
109    public static Format from(final URI uri) {
110         return Format.from(uri.toString());
111     }
112 
113    /**
114     * Determine the {@code Format} from a file.
115     * @param file the File to check.
116     * @return the Format
117     */
118    public static Format from(final File file) {
119         return Format.from(file.getName());
120     }
121 }