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