Skip to content

Commit

Permalink
Add copyright -> (c) as equivalent terms for license matching and add…
Browse files Browse the repository at this point in the history
… information on where text does not match for comparison to standard SPDX licenses

Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
  • Loading branch information
goneall committed Aug 5, 2017
1 parent 73f8cef commit 0f2b2d2
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 74 deletions.
11 changes: 6 additions & 5 deletions Test/org/spdx/compare/TestCompareTemplateOutputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ public void tearDown() throws Exception {

/**
* Test method for {@link org.spdx.compare.CompareTemplateOutputHandler#CompareTemplateOutputHandler(java.lang.String)}.
* @throws Exception
*/
@Test
public void testCompareTemplateOutputHandler() {
public void testCompareTemplateOutputHandler() throws Exception {
CompareTemplateOutputHandler ctoh = new CompareTemplateOutputHandler("test");
assertTrue(ctoh.matches());
}
Expand All @@ -75,7 +76,7 @@ public void testCompareTemplateOutputHandler() {
* @throws LicenseTemplateRuleException
*/
@Test
public void testOptionalText() throws LicenseTemplateRuleException {
public void testOptionalText() throws Exception {
String l1 = "Line 1\n";
String l2 = "Line 2\n";
String l3 = "Line 3\n";
Expand Down Expand Up @@ -127,7 +128,7 @@ public void testOptionalText() throws LicenseTemplateRuleException {
* Test method for {@link org.spdx.compare.CompareTemplateOutputHandler#textEquivalent(java.lang.String)}.
*/
@Test
public void testTextEquivalent() {
public void testTextEquivalent() throws Exception {
String l1 = "Line 1 with // skippable ## /** stuff\n";
String l1S = "Line 1 with skippable stuff\n";
String l2 = "## Line 2 with replaceable analogue cancelled stuff\n";
Expand Down Expand Up @@ -170,7 +171,7 @@ public void testTextEquivalent() {
* Test method for {@link org.spdx.compare.CompareTemplateOutputHandler#normalText(java.lang.String)}.
*/
@Test
public void testNormalText() {
public void testNormalText() throws Exception {
String line1 = "this is line one\n";
String line2 = "this line 2 is another line\n";
String line3 = "yet another third line\n";
Expand Down Expand Up @@ -213,7 +214,7 @@ public void testNormalText() {
* @throws LicenseTemplateRuleException
*/
@Test
public void testVariableRule() throws LicenseTemplateRuleException {
public void testVariableRule() throws Exception {
String line1 = "this is line one\n";
String line2 = "this line 2 is another line\n";
String line2Match = "this\\sline\\s.+another\\sline";
Expand Down
165 changes: 149 additions & 16 deletions src/org/spdx/compare/CompareTemplateOutputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
package org.spdx.compare;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -31,21 +39,130 @@
public class CompareTemplateOutputHandler implements
ILicenseTemplateOutputHandler {

class LineColumn {
private int line;
private int column;
private int len;

public LineColumn(int line, int column,int len) {
this.line = line;
this.column = column;
}

public int getLine() {
return line;
}

public void setLine(int line) {
this.line = line;
}

public int getColumn() {
return column;
}

public void setColumn(int column) {
this.column = column;
}

public int getLen() {
return len;
}

public void setLen(int len) {
this.len = len;
}
}

class DifferenceDescription {
private boolean differenceFound;
private String differenceMessage;
private List<LineColumn> differences;

public DifferenceDescription(boolean differenceFound, String differenceMessage, List<LineColumn> differences) {
this.differenceFound = differenceFound;
this.differenceMessage = differenceMessage;
this.differences = differences;
}

public boolean isDifferenceFound() {
return differenceFound;
}

public void setDifferenceFound(boolean differenceFound) {
this.differenceFound = differenceFound;
}

public String getDifferenceMessage() {
return differenceMessage;
}

public void setDifferenceMessage(String differenceMessage) {
this.differenceMessage = differenceMessage;
}

public List<LineColumn> getDifferences() {
return differences;
}

public void setDifferences(List<LineColumn> differences) {
this.differences = differences;
}

}

String compareText = "";
boolean differenceFound = false;
String[] compareTokens = new String[0];
int compareTokenCounter = 0;
String nextCompareToken = null;
String differenceExplanation = "No difference found";
List<LineColumn> differences = new ArrayList<LineColumn>();
StringBuilder optionalText = new StringBuilder();
Map<Integer, LineColumn> tokenToLocation = new HashMap<Integer, LineColumn>();


/**
* @param compareText Text to compare the parsed SPDX license template to
* @throws IOException This is not to be expected since we are using StringReaders
*/
public CompareTemplateOutputHandler(String compareText) {
public CompareTemplateOutputHandler(String compareText) throws IOException {
this.compareText = compareText;
this.compareTokens = this.compareText.split(LicenseCompareHelper.TOKEN_DELIM);
List<String> tokens = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(compareText));
int currentLine = 1;
int currentToken = 0;
String line = reader.readLine();
Pattern delimPattern = Pattern.compile(LicenseCompareHelper.TOKEN_DELIM);
while (line != null) {
Matcher lineMatcher = delimPattern.matcher(line);
int lastColMatched = 0;
while (lineMatcher.find()) {
String token = line.substring(lastColMatched, lineMatcher.start());
if (token.length() > 0) {
tokens.add(token);
tokenToLocation.put(currentToken, new LineColumn(currentLine, lastColMatched, token.length()));
currentToken++;
}
lastColMatched = lineMatcher.end();
}
if (lastColMatched < line.length()) {
String token = line.substring(lastColMatched, line.length());
tokens.add(token);
tokenToLocation.put(currentToken, new LineColumn(currentLine, lastColMatched, token.length()));
currentToken++;
}
currentLine++;
line = reader.readLine();
}
} finally {
if (reader != null) {
reader.close();
}
}
this.compareTokens = tokens.toArray(new String[tokens.size()]);
compareTokenCounter = 0;
nextCompareToken = LicenseCompareHelper.getTokenAt(compareTokens, compareTokenCounter++);
}
Expand Down Expand Up @@ -112,14 +229,23 @@ public void normalText(String text) {
if (!textEquivalent(text)) {
this.differenceFound = true;
if (this.nextCompareToken == null) {
this.differenceExplanation = "End of compare text encountered before the end of the license template";
LineColumn lastLineColumn = tokenToLocation.get(this.compareTokens.length-1);
// create a zero length location at the end of the file
addDifference("End of compare text encountered before the end of the license template",
new LineColumn(lastLineColumn.getLine(), lastLineColumn.getColumn()+lastLineColumn.getLen(),0));
} else {
this.differenceExplanation = "Difference found starting at token #"+
String.valueOf(this.compareTokenCounter)+"\""+
this.nextCompareToken+"\".";
addDifference("Difference found in normal text",tokenToLocation.get(this.compareTokenCounter));
}
}
}

private void addDifference(String msg, LineColumn location) {
this.differenceExplanation = msg + " starting at line #"+
String.valueOf(location.getLine())+ " column #" +
String.valueOf(location.getColumn())+"\""+
this.nextCompareToken+"\".";
this.differences.add(location);
}

/* (non-Javadoc)
* @see org.spdx.licenseTemplate.ILicenseTemplateOutputHandler#variableRule(org.spdx.licenseTemplate.LicenseTemplateRule)
Expand All @@ -134,18 +260,14 @@ public void variableRule(LicenseTemplateRule rule) {
Matcher matcher = matchPattern.matcher(remainingText);
if (!matcher.find()) {
this.differenceFound = true;
this.differenceExplanation = "Variable text rule "+rule.getName()+
" did not match the compare text starting at token #"+
String.valueOf(this.compareTokenCounter)+"\""+
this.nextCompareToken+"\".";
addDifference("Variable text rule "+rule.getName()+" did not match the compare text",
tokenToLocation.get(this.compareTokenCounter));
} else if (matcher.start() > 0) {
this.differenceFound = true;
this.differenceExplanation = "Extra text \""+
addDifference("Extra text \""+
remainingText.substring(0, matcher.start()) +
"\" found before the variable text rule "+rule.getName()+
" starting at token #"+
String.valueOf(this.compareTokenCounter)+"\""+
this.nextCompareToken+"\".";
"\" found before the variable text rule "+rule.getName(),
tokenToLocation.get(this.compareTokenCounter));
} else {
// advance the token counter
String textAfterMatch = remainingText.substring(matcher.end()).trim();
Expand All @@ -164,7 +286,8 @@ public void variableRule(LicenseTemplateRule rule) {
if (!this.nextCompareToken.equals(tokensAfterMatch[0]) &&
(tokensAfterMatch.length > 1 && !this.nextCompareToken.equals(tokensAfterMatch[1]))) {
this.differenceFound = true;
this.differenceExplanation = "Missmatched text found after end of variable rule" + rule.getName();
addDifference("Missmatched text found after end of variable rule" + rule.getName(),
tokenToLocation.get(this.nextCompareToken));
}
}
}
Expand Down Expand Up @@ -212,11 +335,14 @@ public void endOptional(LicenseTemplateRule rule) {
String saveNextComparisonToken = nextCompareToken;
int saveCompareTokenCounter = compareTokenCounter;
String saveDifferenceExplanation = this.differenceExplanation;
List<LineColumn> saveDifferences = new ArrayList<LineColumn>();
Collections.copy(saveDifferences, this.differences);
if (!textEquivalent(this.optionalText.toString())) {
// reset counters
this.nextCompareToken = saveNextComparisonToken;
this.compareTokenCounter = saveCompareTokenCounter;
this.differenceExplanation = saveDifferenceExplanation;
Collections.copy(this.differences, saveDifferences);
}
}

Expand All @@ -226,5 +352,12 @@ public void endOptional(LicenseTemplateRule rule) {
public boolean matches() {
return !differenceFound;
}

/**
* @return details on the differences found
*/
public DifferenceDescription getDifferences() {
return new DifferenceDescription(differenceFound, this.differenceExplanation, this.differences);
}

}
Loading

0 comments on commit 0f2b2d2

Please sign in to comment.