Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions apache-rat-core/src/main/java/org/apache/rat/commandline/Arg.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ public enum Arg {
.addOption(Option.builder().longOpt("config").hasArgs().argName("File")
.desc("File names for system configuration.")
.converter(Converters.FILE_CONVERTER)
.type(File.class)
.type(DocumentName.class)
.build())
.addOption(Option.builder().longOpt("licenses").hasArgs().argName("File")
.desc("File names for system configuration.")
.deprecated(DeprecatedAttributes.builder().setSince("0.17").setForRemoval(true).setDescription(StdMsgs.useMsg("--config")).get())
.converter(Converters.FILE_CONVERTER)
.type(File.class)
.type(DocumentName.class)
.build()),
Arg::doNotExecute
),
Expand Down Expand Up @@ -177,7 +177,7 @@ public enum Arg {
LICENSES_APPROVED_FILE(new OptionGroup().addOption(Option.builder().longOpt("licenses-approved-file").hasArg().argName("File")
.desc("Name of file containing comma separated lists of approved License IDs.")
.converter(Converters.FILE_CONVERTER)
.type(File.class)
.type(DocumentName.class)
.build()),
(context, selected) ->
context.getConfiguration().addApprovedLicenseIds(processArrayFile(context, selected))),
Expand All @@ -198,7 +198,7 @@ public enum Arg {
FAMILIES_APPROVED_FILE(new OptionGroup().addOption(Option.builder().longOpt("license-families-approved-file").hasArg().argName("File")
.desc("Name of file containing comma separated lists of approved family IDs.")
.converter(Converters.FILE_CONVERTER)
.type(File.class)
.type(DocumentName.class)
.build()),
(context, selected) -> context.getConfiguration().addApprovedLicenseCategories(processArrayFile(context, selected))
),
Expand Down Expand Up @@ -247,7 +247,7 @@ public enum Arg {
.desc("Name of file containing comma separated lists of denied license IDs. " +
"These license families will be removed from the list of approved licenses. " +
"Once license families are removed they can not be added back.")
.type(File.class)
.type(DocumentName.class)
.converter(Converters.FILE_CONVERTER)
.build()),
(context, selected) -> context.getConfiguration().removeApprovedLicenseCategories(processArrayFile(context, selected))),
Expand Down Expand Up @@ -294,12 +294,12 @@ public enum Arg {
"File names that do not start with '/' are relative to the directory where the " +
"argument is located.")
.converter(Converters.FILE_CONVERTER)
.type(File.class)
.type(DocumentName.class)
.build()),
(context, selected) -> {
File[] files = getParsedOptionValues(selected, context.getCommandLine());
for (File f : files) {
context.getConfiguration().addSource(f);
DocumentName[] documentNames = getParsedOptionValues(selected, context.getCommandLine());
for (DocumentName documentName : documentNames) {
context.getConfiguration().addSource(documentName.asFile());
}
}),

Expand Down Expand Up @@ -340,9 +340,9 @@ public enum Arg {
.build()),
(context, selected) -> {
try {
File excludeFileName = context.getCommandLine().getParsedOptionValue(selected);
DocumentName excludeFileName = context.getCommandLine().getParsedOptionValue(selected);
if (excludeFileName != null) {
context.getConfiguration().addExcludedPatterns(ExclusionUtils.asIterable(excludeFileName, "#"));
context.getConfiguration().addExcludedPatterns(ExclusionUtils.asIterable(excludeFileName.asFile(), "#"));
}
} catch (Exception e) {
throw ConfigurationException.from(e);
Expand Down Expand Up @@ -422,9 +422,9 @@ public enum Arg {
.build()),
(context, selected) -> {
try {
File includeFileName = context.getCommandLine().getParsedOptionValue(selected);
DocumentName includeFileName = context.getCommandLine().getParsedOptionValue(selected);
if (includeFileName != null) {
context.getConfiguration().addIncludedPatterns(ExclusionUtils.asIterable(includeFileName, "#"));
context.getConfiguration().addIncludedPatterns(ExclusionUtils.asIterable(includeFileName.asFile(), "#"));
}
} catch (Exception e) {
throw ConfigurationException.from(e);
Expand Down Expand Up @@ -605,22 +605,23 @@ public enum Arg {
.addOption(Option.builder().option("o").longOpt("out").hasArg().argName("File")
.desc("Define the output file where to write a report to.")
.deprecated(DeprecatedAttributes.builder().setSince("0.17").setForRemoval(true).setDescription(StdMsgs.useMsg("--output-file")).get())
.type(File.class)
.type(DocumentName.class)
.converter(Converters.FILE_CONVERTER)
.build())
.addOption(Option.builder().longOpt("output-file").hasArg().argName("File")
.desc("Define the output file where to write a report to.")
.type(File.class)
.type(DocumentName.class)
.converter(Converters.FILE_CONVERTER)
.build()),
(context, selected) -> {
try {
File file = context.getCommandLine().getParsedOptionValue(selected);
File parent = file.getParentFile();
DocumentName documentName = context.getCommandLine().getParsedOptionValue(selected);
File document = documentName.asFile();
File parent = document.getParentFile();
if (!parent.mkdirs() && !parent.isDirectory()) {
DefaultLog.getInstance().error("Could not create report parent directory " + file);
DefaultLog.getInstance().error("Could not create report parent directory " + documentName);
}
context.getConfiguration().setOut(file);
context.getConfiguration().setOut(document);
} catch (ParseException e) {
// we write to system out by default.
context.logParseException(e, selected, "System.out");
Expand Down Expand Up @@ -821,7 +822,8 @@ private static List<String> processArrayArg(final ArgumentContext context, final
* @return Option as a file.
*/
private static File commandLineFile(final ArgumentContext context, final Option selected) throws ParseException {
return context.getCommandLine().getParsedOptionValue(selected);
DocumentName documentName = context.getCommandLine().getParsedOptionValue(selected);
return documentName.asFile();
}

/**
Expand Down Expand Up @@ -859,9 +861,9 @@ private static void processConfigurationArgs(final ArgumentContext context, fina

optionCollection.getSelected(CONFIGURATION).ifPresent(
selected -> {
File[] files = getParsedOptionValues(selected, context.getCommandLine());
for (File file : files) {
defaultBuilder.add(file);
DocumentName[] documentNames = getParsedOptionValues(selected, context.getCommandLine());
for (DocumentName documentName : documentNames) {
defaultBuilder.add(documentName.asFile());
}
});
optionCollection.getSelected(CONFIGURATION_NO_DEFAULTS).ifPresent(selected -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/
package org.apache.rat.commandline;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;

import org.apache.commons.cli.Converter;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -73,14 +72,15 @@ private Converters() {
/**
* A converter that can handle relative or absolute files.
*/
public static final class FileConverter implements Converter<File, NullPointerException> {
public static final class FileConverter implements Converter<DocumentName, NullPointerException> {
/** The working directory to resolve relative files against. */
private DocumentName workingDirectory;

/**
* The constructor.
* visible for testing
*/
private FileConverter() {
FileConverter() {
// private construction only.
}

Expand All @@ -95,24 +95,27 @@ public void setWorkingDirectory(final DocumentName workingDirectory) {
/**
* Applies the conversion function to the specified file name.
* @param fileName the file name to create a file from.
* @return a File.
* @return the DocumentName
* @throws NullPointerException if {@code fileName} is null.
*/
public File apply(final String fileName) throws NullPointerException {
File file = new File(fileName);
// is this a relative file?
if (!fileName.startsWith(File.separator)) {
// check for a root provided (e.g. C:\\)"
if (!DocumentName.FSInfo.getDefault().rootFor(fileName).isPresent()) {
// no root, resolve against workingDirectory
file = new File(workingDirectory.resolve(fileName).getName()).getAbsoluteFile();
public DocumentName apply(final String fileName) throws NullPointerException {
DocumentName.FSInfo fsInfo = workingDirectory.fsInfo();
DocumentName.Builder builder = DocumentName.builder(fsInfo);
String normalizedFileName = fsInfo.normalize(fileName.trim());

Optional<String> root = fsInfo.rootFor(normalizedFileName);
builder.setRoot(root.orElse(workingDirectory.getRoot()));
if (fsInfo.startsWithRootOrSeparator(normalizedFileName)) {
if (normalizedFileName.startsWith(workingDirectory.getName()) ||
normalizedFileName.startsWith(workingDirectory.getName().substring(workingDirectory.getRoot().length()))) {
builder.setBaseName(workingDirectory.getBaseDocumentName());
} else {
builder.setBaseName(fsInfo.dirSeparator());
}
} else {
builder.setBaseName(workingDirectory);
}
try {
return file.getCanonicalFile();
} catch (IOException e) {
return file.getAbsoluteFile();
}
return builder.setName(normalizedFileName).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,33 @@
import java.nio.file.Paths;
import java.util.Collections;

/**
* The DocumentName for an ArchiveEntry.
*/
public class ArchiveEntryName extends DocumentName {
/** The name of the document that contains this entry. */
private final DocumentName archiveFileName;

/**
* Sets the builder so that a propery DocumentName is constructed.
* @param archiveFileName the archvie file DocumentName
* @param archiveEntryName the entry name
* @return the DocumentName.Builder for the archive entry.
*/
private static DocumentName.Builder prepareBuilder(final DocumentName archiveFileName, final String archiveEntryName) {
String root = archiveFileName.getName() + "#";
String root = archiveFileName.getName() + "#/";
FSInfo fsInfo = new FSInfo("archiveEntry", "/", true, Collections.singletonList(root));
return DocumentName.builder(fsInfo)
.setRoot(root)
.setBaseName(root + "/")
.setBaseName("/")
.setName(archiveEntryName);
}

/**
* Constucts an archive file name from an archive file document name and an entry name.
* @param archiveFileName the archive file document name.
* @param archiveEntryName the archive entry name.
*/
public ArchiveEntryName(final DocumentName archiveFileName, final String archiveEntryName) {
super(prepareBuilder(archiveFileName, archiveEntryName));
this.archiveFileName = archiveFileName;
Expand All @@ -60,25 +75,10 @@ public String getBaseName() {
return archiveFileName.getName() + "#";
}

@Override
boolean startsWithRootOrSeparator(final String candidate, final String root, final String separator) {
return super.startsWithRootOrSeparator(candidate, root, separator);
}

@Override
public String localized(final String dirSeparator) {
String superLocal = super.localized(dirSeparator);
superLocal = superLocal.substring(superLocal.lastIndexOf("#") + 1);
return archiveFileName.localized(dirSeparator) + "#" + superLocal;
}

@Override
public boolean equals(final Object other) {
return super.equals(other);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
Loading