-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically map scancode data to NormalizedLicenses (#231)
* Core implementation of automatic mapping of scancode generated RawLicenses to NormalizedLicenses (#230) * completion of own license info (cherry picked from commit 94359b68a42a554846d6ba66a60cad4f942ef864) * Update documentation * add ignorelist * change logic for detecting if dataStatus is WITH_ISSUES to use regex list * Refactoring of regex list processing * some cleanup * fixed spelling issues
- Loading branch information
Showing
15 changed files
with
832 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
core/src/main/java/com/devonfw/tools/solicitor/common/RegexListPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package com.devonfw.tools.solicitor.common; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Encapsulates a list of regular expression {@link Pattern}s and allows checking if a string matches any of those | ||
* patterns. | ||
*/ | ||
public class RegexListPredicate implements Predicate<String> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RegexListPredicate.class); | ||
|
||
private Pattern[] patterns = new Pattern[0]; | ||
|
||
/** | ||
* The constructor. | ||
*/ | ||
public RegexListPredicate() { | ||
|
||
super(); | ||
} | ||
|
||
/** | ||
* Checks if the given argument matches any of the predefined regular expression patterns. | ||
* | ||
* @param t the string to be checked | ||
* @param debugLogTemplate template to be used for creating a debug log message if the argument matches. This template | ||
* should have two placeholders <code>{}</code> which will be filled with the value of the argument and the | ||
* matching pattern. If this parameter is set to <code>null</code> then no debug logging will be done. | ||
* @return <code>true</code> if the argument matches any of the patterns, <code>false</code> otherwise. | ||
*/ | ||
public boolean test(String t, String debugLogTemplate) { | ||
|
||
if (t == null) { | ||
return false; | ||
} | ||
for (Pattern p : this.patterns) { | ||
if (p.matcher(t).matches()) { | ||
if (LOG.isDebugEnabled() && debugLogTemplate != null) { | ||
LOG.debug(debugLogTemplate, t, p.toString()); | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Tests the predicate without any debug logging. | ||
* | ||
* @param t the argument to be tested | ||
* @return <code>true</code> if the argument matches any of the patterns, <code>false</code> otherwise. | ||
* @see #test(String, String) | ||
* @see Predicate#test(Object) | ||
*/ | ||
@Override | ||
public boolean test(String t) { | ||
|
||
return test(t, null); | ||
} | ||
|
||
/** | ||
* Sets the regular expressions to be tested by this object. | ||
* | ||
* @param regexes Array of strings, each being a valid regular expression expression as defined in {@link Pattern}. | ||
* @throws PatternSyntaxException If any of the expressions have invalid syntax | ||
*/ | ||
public void setRegexes(String[] regexes) { | ||
|
||
if (regexes != null) { | ||
this.patterns = new Pattern[regexes.length]; | ||
for (int i = 0; i < regexes.length; i++) { | ||
this.patterns[i] = Pattern.compile(regexes[i]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the list of configured regular expressions as a String | ||
* | ||
* @return The regexes as a single string, separated via comma. | ||
*/ | ||
public String getRegexesAsString() { | ||
|
||
List<String> patternStrings = new ArrayList(); | ||
for (Pattern p : this.patterns) { | ||
patternStrings.add(p.toString()); | ||
} | ||
return String.join(",", patternStrings); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.