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.commandline;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.commons.cli.Converter;
25  import org.apache.commons.lang3.tuple.Pair;
26  import org.apache.rat.ConfigurationException;
27  import org.apache.rat.document.DocumentName;
28  import org.apache.rat.report.claim.ClaimStatistic;
29  
30  import static java.lang.String.format;
31  
32  /**
33   * Customized converters for Arg processing.
34   */
35  public final class Converters {
36  
37      private Converters() {
38          // do not instantiate
39      }
40  
41      /**
42       * Creates a File with fully qualified name.
43       */
44      public static final FileConverter FILE_CONVERTER = new FileConverter();
45  
46      /**
47       * Converts the Converter pattern into a Converter, count pair.
48       */
49      public static final Converter<Pair<ClaimStatistic.Counter, Integer>, ConfigurationException> COUNTER_CONVERTER = arg -> {
50          String[] parts = arg.split(":");
51          try {
52              ClaimStatistic.Counter counter = ClaimStatistic.Counter.valueOf(parts[0].toUpperCase());
53              Integer limit = Integer.parseInt(parts[1]);
54              return Pair.of(counter, limit);
55          } catch (NumberFormatException e) {
56              throw new ConfigurationException(format("'%s' is not a valid integer", parts[1]), e);
57          } catch (IllegalArgumentException e) {
58              throw new ConfigurationException(format("'%s' is not a valid Counter", parts[0]), e);
59          }
60      };
61  
62      /**
63       * A converter that can handle relative or absolute files.
64       */
65      public static final class FileConverter implements Converter<File, NullPointerException> {
66          /** The working directory to resolve relative files against. */
67          private DocumentName workingDirectory;
68  
69          /**
70           * The constructor.
71           */
72          private FileConverter() {
73              // private construction only.
74          }
75  
76          /**
77           * Sets the working directory for the conversion.
78           * @param workingDirectory current working directory
79           */
80          public void setWorkingDirectory(final DocumentName workingDirectory) {
81              this.workingDirectory = workingDirectory;
82          }
83  
84          /**
85           * Applies the conversion function to the specified file name.
86           * @param fileName the file name to create a file from.
87           * @return a File.
88           * @throws NullPointerException if {@code fileName} is null.
89           */
90          public File apply(final String fileName) throws NullPointerException {
91              File file = new File(fileName);
92              // is this a relative file?
93              if (!fileName.startsWith(File.separator)) {
94                  // check for a root provided (e.g. C:\\)"
95                  if (!DocumentName.FSInfo.getDefault().rootFor(fileName).isPresent()) {
96                      // no root, resolve against workingDirectory
97                      file = new File(workingDirectory.resolve(fileName).getName()).getAbsoluteFile();
98                  }
99              }
100             try {
101                 return file.getCanonicalFile();
102             } catch (IOException e) {
103                 return file.getAbsoluteFile();
104             }
105         }
106     }
107 }