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