Skip to content
Merged
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
121 changes: 83 additions & 38 deletions src/main/java/org/spdx/licenseTemplate/LicenseTextHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -100,6 +101,43 @@ public class LicenseTextHelper {
NORMALIZE_TOKENS.put("\"", "'");
NORMALIZE_TOKENS.put("merchantability", "merchantability");
}

/**
* Class to encapsulate Token iterator
*/
private static class TokenIterator {
private final String[] tokens;
private int position = 0;
private String current;

TokenIterator(String[] tokens) {
this.tokens = tokens;
this.current = getTokenAt(tokens, position++);
}

String current() {
return current;
}

boolean hasNext() {
return current != null;
}

void advance() {
current = getTokenAt(tokens, position++);
}

void skipWhile(Predicate<String> condition) {
while (current != null && condition.test(current)) {
advance();
}
}

boolean hasOnlySkippableTokensRemaining() {
skipWhile(LicenseTextHelper::canSkip);
return current == null;
}
}

private LicenseTextHelper() {
// static class
Expand Down Expand Up @@ -130,47 +168,54 @@ public static boolean isLicenseTextEquivalent(String licenseTextA, String licens
}
Map<Integer, LineColumn> tokenToLocationA = new HashMap<>();
Map<Integer, LineColumn> tokenToLocationB = new HashMap<>();
String[] licenseATokens = tokenizeLicenseText(licenseTextA,tokenToLocationA);
String[] licenseBTokens = tokenizeLicenseText(licenseTextB,tokenToLocationB);
int bTokenCounter = 0;
int aTokenCounter = 0;
String nextAToken = getTokenAt(licenseATokens, aTokenCounter++);
String nextBToken = getTokenAt(licenseBTokens, bTokenCounter++);
while (nextAToken != null) {
if (nextBToken == null) {
// end of b stream
while (canSkip(nextAToken)) {
nextAToken = getTokenAt(licenseATokens, aTokenCounter++);
}
if (nextAToken != null) {
return false; // there is more stuff in the license text B, so not equal
}
} else if (tokensEquivalent(nextAToken, nextBToken)) {
// just move onto the next set of tokens
nextAToken = getTokenAt(licenseATokens, aTokenCounter++);
nextBToken = getTokenAt(licenseBTokens, bTokenCounter++);
} else {
// see if we can skip through some B tokens to find a match
while (canSkip(nextBToken)) {
nextBToken = getTokenAt(licenseBTokens, bTokenCounter++);
}
// just to be sure, skip forward on the A license
while (canSkip(nextAToken)) {
nextAToken = getTokenAt(licenseATokens, aTokenCounter++);
}
if (!tokensEquivalent(nextAToken, nextBToken)) {
return false;
} else {
nextAToken = getTokenAt(licenseATokens, aTokenCounter++);
nextBToken = getTokenAt(licenseBTokens, bTokenCounter++);
}
return isLicenseTextEquivalent(tokenizeLicenseText(licenseTextA,tokenToLocationA),
tokenizeLicenseText(licenseTextB,tokenToLocationB));
}

/**
* Returns true if two sets of license tokens is considered a match per
* the SPDX License matching guidelines documented at spdx.org (currently <a href="https://spdx.github.io/spdx-spec/v2.3/license-matching-guidelines-and-templates/">license matching guidelines</a>)
* There are 2 unimplemented features - bullets/numbering is not considered and comments with no whitespace between text is not skipped
* @param licenseATokens normalized license tokens to compare
* @param licenseBTokens normalized license tokens to compare
* @return true if the license text is equivalent
*/
public static boolean isLicenseTextEquivalent(String[] licenseATokens, String[] licenseBTokens) {
TokenIterator iterA = new TokenIterator(licenseATokens);
TokenIterator iterB = new TokenIterator(licenseBTokens);

while (iterA.hasNext()) {
if (!iterB.hasNext()) {
return iterA.hasOnlySkippableTokensRemaining();
}

if (tokensEquivalent(iterA.current(), iterB.current())) {
iterA.advance();
iterB.advance();
} else if (!trySkipToMatch(iterA, iterB)) {
return false;
}
}
// need to make sure B is at the end
while (canSkip(nextBToken)) {
nextBToken = getTokenAt(licenseBTokens, bTokenCounter++);
return iterB.hasOnlySkippableTokensRemaining();
}

/**
* Skips any tokens that can be skipped and attempts to match the remaining tokens
// * @param iterA Token iterator for comparison
* @param iterB Token iterator for comparison
* @return true if the tokens match
*/
private static boolean trySkipToMatch(TokenIterator iterA, TokenIterator iterB) {
iterB.skipWhile(LicenseTextHelper::canSkip);
iterA.skipWhile(LicenseTextHelper::canSkip);

if (!tokensEquivalent(iterA.current(), iterB.current())) {
return false;
}
return (nextBToken == null);

iterA.advance();
iterB.advance();
return true;
}

/**
Expand Down
Loading