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