Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RAT-98: Added FSInfo to handle file system differences #421

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions apache-rat-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,10 @@
<artifactId>groovy-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion apache-rat-core/src/it/java/org/apache/rat/ReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static Stream<Arguments> args() throws RatException {
@Override
public void report(Document document) {
if (!document.isIgnored()) {
String[] tokens = document.getName().tokenize(document.getName().localized());
String[] tokens = DocumentName.FSInfo.getDefault().tokenize(document.getName().localized());
results.add(Arguments.of(tokens[1], document));
}
}
Expand Down
12 changes: 6 additions & 6 deletions apache-rat-core/src/main/java/org/apache/rat/api/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public enum Type {
}

/** The path matcher used by this document */
protected final DocumentNameMatcher nameExcluder;
protected final DocumentNameMatcher nameMatcher;
/** The metadata for this document */
private final MetaData metaData;
/** The fully qualified name of this document */
Expand All @@ -61,11 +61,11 @@ public enum Type {
/**
* Creates an instance.
* @param name the native NameSet of the resource.
* @param nameExcluder the document name matcher to filter directories/files.
* @param nameMatcher the document name matcher to filter directories/files.
*/
protected Document(final DocumentName name, final DocumentNameMatcher nameExcluder) {
protected Document(final DocumentName name, final DocumentNameMatcher nameMatcher) {
this.name = name;
this.nameExcluder = nameExcluder;
this.nameMatcher = nameMatcher;
this.metaData = new MetaData();
}

Expand All @@ -81,8 +81,8 @@ public final DocumentName getName() {
* Gets the file filter this document was created with.
* @return the file filter this document was created with.
*/
public final DocumentNameMatcher getNameExcluder() {
return nameExcluder;
public final DocumentNameMatcher getNameMatcher() {
return nameMatcher;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ public DocumentNameMatcher getNameMatcher(final DocumentName basedir) {
.addTo(new ArrayList<>());

if (!incl.isEmpty()) {
inclMatchers.add(new DocumentNameMatcher("included patterns", MatchPatterns.from(incl), basedir));
inclMatchers.add(new DocumentNameMatcher("included patterns", MatchPatterns.from(basedir.getDirectorySeparator(), incl), basedir));
}
if (!excl.isEmpty()) {
exclMatchers.add(new DocumentNameMatcher("excluded patterns", MatchPatterns.from(excl), basedir));
exclMatchers.add(new DocumentNameMatcher("excluded patterns", MatchPatterns.from(basedir.getDirectorySeparator(), excl), basedir));
}

if (!includedPaths.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,18 @@ private static void verifyFile(final File file) {
throw new ConfigurationException(format("%s is not a valid file.", file));
}
}

/**
* Tokenizes the string based on the directory separator.
* @param source the source to tokenize
* @param from the directory separator for the source.
* @param to the directory separator for the result.
* @return the source string with the separators converted.
*/
public static String convertSeparator(final String source, final String from, final String to) {
if (StringUtils.isEmpty(source) || from.equals(to)) {
return source;
}
return String.join(to, source.split("\\Q" + from + "\\E"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

Expand Down Expand Up @@ -49,7 +50,7 @@ public final class MatchPattern {

private final char[][] tokenizedChar;

private MatchPattern(final String source, final String separator) {
MatchPattern(final String source, final String separator) {
regexPattern = SelectorUtils.isRegexPrefixedPattern(source)
? source.substring(
SelectorUtils.REGEX_HANDLER_PREFIX.length(),
Expand Down Expand Up @@ -116,7 +117,7 @@ public boolean startsWith(final String string) {

@Override
public String toString() {
return source;
return Arrays.asList(tokenized).toString();
}

public String source() {
Expand All @@ -140,8 +141,4 @@ static char[][] tokenizePathToCharArray(final String path, final String separato
}
return tokenizedNameChar;
}

public static MatchPattern fromString(final String source) {
return new MatchPattern(source, File.separator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@SuppressWarnings({"checkstyle:RegexpSingleLine", "checkstyle:JavadocVariable"})
/**
Expand All @@ -41,15 +42,15 @@ private MatchPatterns(final MatchPattern[] patterns) {

@Override
public String toString() {
return source();
return Arrays.stream(patterns).map(MatchPattern::toString).collect(Collectors.toList()).toString();
}

public String source() {
List<String> sources = new ArrayList<>();
for (MatchPattern pattern : patterns) {
sources.add(pattern.source());
}
return "[" + String.join(", ", sources) + "]";
return Arrays.stream(patterns).map(MatchPattern::source).collect(Collectors.toList()).toString();
}

public Iterable<MatchPattern> patterns() {
return Arrays.asList(patterns);
}

/**
Expand Down Expand Up @@ -83,36 +84,23 @@ public boolean matches(final String name, final char[][] tokenizedNameChar, fina
return false;
}

public Predicate<String> asPredicate(final boolean isCaseSensitive) {
return name -> matches(name, isCaseSensitive);
}

public boolean matchesPatternStart(final String name, final boolean isCaseSensitive) {
for (MatchPattern includesPattern : patterns) {
if (includesPattern.matchPatternStart(name, isCaseSensitive)) {
return true;
}
}
return false;
}

public static MatchPatterns from(final String... sources) {
public static MatchPatterns from(final String separator, final String... sources) {
final int length = sources.length;
MatchPattern[] result = new MatchPattern[length];
for (int i = 0; i < length; i++) {
result[i] = MatchPattern.fromString(sources[i]);
result[i] = new MatchPattern(sources[i], separator);
}
return new MatchPatterns(result);
}

public static MatchPatterns from(final Iterable<String> strings) {
return new MatchPatterns(getMatchPatterns(strings));
public static MatchPatterns from(final String separator, final Iterable<String> strings) {
return new MatchPatterns(getMatchPatterns(separator, strings));
}

private static MatchPattern[] getMatchPatterns(final Iterable<String> items) {
private static MatchPattern[] getMatchPatterns(final String separator, final Iterable<String> items) {
List<MatchPattern> result = new ArrayList<>();
for (String string : items) {
result.add(MatchPattern.fromString(string));
result.add(new MatchPattern(string, separator));
}
return result.toArray(new MatchPattern[0]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
*/
package org.apache.rat.document;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;

public class ArchiveEntryName extends DocumentName {
/** Then name of the document that contains this entry */
private final DocumentName archiveFileName;

private static DocumentName.Builder prepareBuilder(final DocumentName archiveFileName, final String archiveEntryName) {
String root = archiveFileName.getName() + "#";
FSInfo fsInfo = new FSInfo("archiveEntry", "/", true, Collections.singletonList(root));
return DocumentName.builder(fsInfo)
.setRoot(root)
.setBaseName(root + "/")
.setName(archiveEntryName);
}
public ArchiveEntryName(final DocumentName archiveFileName, final String archiveEntryName) {
super(prepareBuilder(archiveFileName, archiveEntryName));
this.archiveFileName = archiveFileName;
}

@Override
public File asFile() {
return archiveFileName.asFile();
}

@Override
public Path asPath() {
return Paths.get(archiveFileName.asPath().toString(), "#", super.asPath().toString());
}

@Override
public DocumentName resolve(final String child) {
return new ArchiveEntryName(this.archiveFileName, super.resolve(child).localized());
}

@Override
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;
}
}
Loading
Loading