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
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.Option;
25 import org.apache.commons.cli.ParseException;
26 import org.apache.rat.ReportConfiguration;
27 import org.apache.rat.document.DocumentName;
28 import org.apache.rat.ui.UIOptionCollection;
29 import org.apache.rat.utils.DefaultLog;
30
31 import static java.lang.String.format;
32
33 /**
34 * Provides the context necessary to process various arguments.
35 * @since 0.17
36 */
37 public final class ArgumentContext {
38 /** The report configuration that is being built */
39 private final ReportConfiguration configuration;
40 /** The command line that is building the configuration */
41 private final CommandLine commandLine;
42 /** The directory from which relative file names will be resolved */
43 private final DocumentName workingDirectory;
44
45 /**
46 * Creates a context with the specified configuration.
47 * @param workingDirectory the directory from which relative file names will be resolved.
48 * @param configuration The configuration that is being built.
49 * @param commandLine The command line that is building the configuration.
50 */
51 public ArgumentContext(final File workingDirectory, final ReportConfiguration configuration, final CommandLine commandLine) {
52 this.workingDirectory = DocumentName.builder(workingDirectory).build();
53 this.commandLine = commandLine;
54 this.configuration = configuration;
55 }
56
57 /**
58 * Creates a context with an empty configuration.
59 * @param workingDirectory The directory from which to resolve relative file names.
60 * @param commandLine The command line.
61 */
62 public ArgumentContext(final File workingDirectory, final CommandLine commandLine) {
63 this(workingDirectory, new ReportConfiguration(), commandLine);
64 }
65
66 /**
67 * Process the arguments specified in this context.
68 */
69 public void processArgs(final UIOptionCollection<?> uiOptionCollection) {
70 Arg.processArgs(this, uiOptionCollection);
71 }
72
73 /**
74 * Gets the configuration.
75 * @return The configuration that is being built.
76 */
77 public ReportConfiguration getConfiguration() {
78 return configuration;
79 }
80
81 /**
82 * Gets the command line.
83 * @return The command line that is driving the configuration.
84 */
85 public CommandLine getCommandLine() {
86 return commandLine;
87 }
88
89 /**
90 * Gets the directory name from which relative file names will be resolved.
91 * @return The directory name from which relative file names will be resolved.
92 */
93 public DocumentName getWorkingDirectory() {
94 return workingDirectory;
95 }
96
97 /**
98 * Logs a ParseException as a warning.
99 * @param exception the parse exception to log
100 * @param opt the option being processed
101 * @param defaultValue The default value the option is being set to.
102 */
103 public void logParseException(final ParseException exception, final Option opt, final Object defaultValue) {
104 DefaultLog.getInstance().warn(format("Invalid %s specified: %s ", opt, commandLine.getOptionValue(opt)));
105 DefaultLog.getInstance().warn(format("%s set to: %s", opt, defaultValue));
106 DefaultLog.getInstance().debug(exception);
107 }
108 }