Skip to content

Commit 3aca0d2

Browse files
committed
Intermediate checkin - working compare for version 2 spec
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent da6c36c commit 3aca0d2

18 files changed

Lines changed: 1533 additions & 1140 deletions

src/main/java/org/spdx/library/LicenseInfoFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public static ExpandedLicensingListedLicense getListedLicenseById(String license
6666
* @return SPDX listed license in SPDX spec version 2.X format or null if the ID is not in the SPDX license list
6767
* @throws InvalidSPDXAnalysisException
6868
*/
69-
public static SpdxListedLicense getListedLicenseV2ByIdCompat(String licenseId)throws InvalidSPDXAnalysisException {
70-
return ListedLicenses.getListedLicenses().getListedLicenseV2ById(licenseId);
69+
public static SpdxListedLicense getListedLicenseByIdCompatV2(String licenseId)throws InvalidSPDXAnalysisException {
70+
return ListedLicenses.getListedLicenses().getListedLicenseByIdCompatV2(licenseId);
7171
}
7272

7373
/**
@@ -92,7 +92,7 @@ public static SpdxListedLicense getListedLicenseV2ByIdCompat(String licenseId)th
9292
* @throws InvalidLicenseStringException
9393
* @throws DefaultStoreNotInitialized
9494
*/
95-
public static AnyLicenseInfo parseSPDXLicenseStringV2(String licenseString, @Nullable IModelStore store,
95+
public static AnyLicenseInfo parseSPDXLicenseStringCompatV2(String licenseString, @Nullable IModelStore store,
9696
@Nullable String documentUri, @Nullable IModelCopyManager copyManager) throws InvalidLicenseStringException, DefaultStoreNotInitialized {
9797
if (Objects.isNull(store)) {
9898
store = DefaultModelStore.getDefaultModelStore();
@@ -196,8 +196,8 @@ public static SimpleLicensingAnyLicenseInfo parseSPDXLicenseString(String licens
196196
* @throws InvalidLicenseStringException
197197
* @throws DefaultStoreNotInitialized
198198
*/
199-
public static AnyLicenseInfo parseSPDXLicenseV2String(String licenseString) throws InvalidLicenseStringException, DefaultStoreNotInitialized {
200-
return parseSPDXLicenseStringV2(licenseString, null, null, null);
199+
public static AnyLicenseInfo parseSPDXLicenseStringCompatV2(String licenseString) throws InvalidLicenseStringException, DefaultStoreNotInitialized {
200+
return parseSPDXLicenseStringCompatV2(licenseString, null, null, null);
201201
}
202202

203203

@@ -247,7 +247,7 @@ public static ExpandedLicensingListedLicenseException getListedExceptionById(Str
247247
* @throws InvalidSPDXAnalysisException
248248
*/
249249
public static ListedLicenseException getListedExceptionV2ById(String id) throws InvalidSPDXAnalysisException {
250-
return ListedLicenses.getListedLicenses().getListedExceptionV2ById(id);
250+
return ListedLicenses.getListedLicenses().getListedExceptionByIdCompatV2(id);
251251
}
252252

253253
/**

src/main/java/org/spdx/library/ListedLicenses.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public boolean isSpdxListedExceptionId(String exceptionId) {
199199
* @return an SPDX spec version 2 SPDX listed license or null if the ID is not in the SPDX license list
200200
* @throws InvalidSPDXAnalysisException
201201
*/
202-
public SpdxListedLicense getListedLicenseV2ById(String licenseId) throws InvalidSPDXAnalysisException {
202+
public SpdxListedLicense getListedLicenseByIdCompatV2(String licenseId) throws InvalidSPDXAnalysisException {
203203
return (SpdxListedLicense)SpdxModelFactory.createModelObjectV2(this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, licenseId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null);
204204
}
205205

@@ -208,7 +208,7 @@ public SpdxListedLicense getListedLicenseV2ById(String licenseId) throws Invalid
208208
* @return an SPDX spec version 2 SPDX listed license exception or null if the ID is not in the SPDX license list
209209
* @throws InvalidSPDXAnalysisException
210210
*/
211-
public ListedLicenseException getListedExceptionV2ById(String exceptionId) throws InvalidSPDXAnalysisException {
211+
public ListedLicenseException getListedExceptionByIdCompatV2(String exceptionId) throws InvalidSPDXAnalysisException {
212212
return (ListedLicenseException)SpdxModelFactory.createModelObjectV2(this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, exceptionId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null);
213213
}
214214

@@ -275,7 +275,7 @@ public IModelStore getLicenseModelStore() {
275275
return this.licenseStoreV3;
276276
}
277277

278-
public IModelStore getLicenseModelStoreV2() {
278+
public IModelStore getLicenseModelStoreCompatV2() {
279279
return this.licenseStoreV2;
280280
}
281281

src/main/java/org/spdx/library/SpdxModelFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public static Object getExternalElement(IModelStore store, String uri,
217217
* @throws InvalidSPDXAnalysisException
218218
*/
219219
public static Stream<?> getSpdxObjects(IModelStore store, @Nullable IModelCopyManager copyManager,
220-
String typeFilter, String objectUriPrefixFilter) throws InvalidSPDXAnalysisException {
220+
@Nullable String typeFilter, @Nullable String objectUriPrefixFilter) throws InvalidSPDXAnalysisException {
221221
Objects.requireNonNull(store, "Store must not be null");
222222
return store.getAllItems(objectUriPrefixFilter, typeFilter).map(tv -> {
223223
//TODO: Change this a null namespace and filtering on anonomous or startswith document URI - this will catch the anon. types

src/main/java/org/spdx/storage/simple/InMemSpdxStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public void removeProperty(String objectUri, PropertyDescriptor propertyDescript
367367
}
368368

369369
@Override
370-
public Stream<TypedValue> getAllItems(@Nullable String nameSpace, String typeFilter)
370+
public Stream<TypedValue> getAllItems(@Nullable String nameSpace, @Nullable String typeFilter)
371371
throws InvalidSPDXAnalysisException {
372372
Iterator<StoredTypedItem> valueIter = typedValueMap.values().iterator();
373373
List<TypedValue> allItems = new ArrayList<>();

src/main/java/org/spdx/utility/compare/CompareTemplateOutputHandler.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.spdx.licenseTemplate.LicenseParserException;
3232
import org.spdx.licenseTemplate.LicenseTemplateRule;
3333
import org.spdx.licenseTemplate.LicenseTemplateRule.RuleType;
34+
import org.spdx.licenseTemplate.LicenseTextHelper;
35+
import org.spdx.licenseTemplate.LineColumn;
3436

3537
/**
3638
* Compares the output of a parsed license template to text. The method matches is called after
@@ -210,14 +212,14 @@ public int match(String[] matchTokens, int startToken, int endToken, String orig
210212
if (this.rule == null) {
211213
if (this.text != null) {
212214
Map<Integer, LineColumn> textLocations = new HashMap<Integer, LineColumn>();
213-
String[] textTokens = LicenseCompareHelper.tokenizeLicenseText(text, textLocations);
215+
String[] textTokens = LicenseTextHelper.tokenizeLicenseText(text, textLocations);
214216
if (this.skipFirstTextToken) {
215217
textTokens = Arrays.copyOfRange(textTokens, 1, textTokens.length);
216218
}
217219
nextToken = compareText(textTokens, matchTokens, nextToken, endToken, this);
218220
if (nextToken < 0) {
219221
int errorLocation = -nextToken;
220-
differences.addDifference(tokenToLocation.get(errorLocation), LicenseCompareHelper.getTokenAt(matchTokens, errorLocation),
222+
differences.addDifference(tokenToLocation.get(errorLocation), LicenseTextHelper.getTokenAt(matchTokens, errorLocation),
221223
"Normal text of license does not match", text, null, getLastOptionalDifference());
222224
}
223225
if (this.subInstructions.size() > 0) {
@@ -376,7 +378,7 @@ private List<Integer> findNextNonVarTextStartTokens(ParseInstruction afterChild,
376378
}
377379

378380
Map<Integer, LineColumn> normalTextLocations = new HashMap<Integer, LineColumn>();
379-
String[] textTokens = LicenseCompareHelper.tokenizeLicenseText(subInstructions.get(firstNormalTextIndex).getText(), normalTextLocations);
381+
String[] textTokens = LicenseTextHelper.tokenizeLicenseText(subInstructions.get(firstNormalTextIndex).getText(), normalTextLocations);
380382
if (textTokens.length > MAX_NEXT_NORMAL_TEXT_SEARCH_LENGTH) {
381383
textTokens = Arrays.copyOf(textTokens, MAX_NEXT_NORMAL_TEXT_SEARCH_LENGTH);
382384
}
@@ -442,7 +444,7 @@ private int numTokensMatched(String text, int end) {
442444
}
443445
Map<Integer, LineColumn> temp = new HashMap<Integer, LineColumn>();
444446
String subText = text.substring(0, end);
445-
String[] tokenizedString = LicenseCompareHelper.tokenizeLicenseText(subText, temp);
447+
String[] tokenizedString = LicenseTextHelper.tokenizeLicenseText(subText, temp);
446448
return tokenizedString.length;
447449
}
448450

@@ -475,7 +477,7 @@ private int matchVariable(List<Integer> matchingStartTokens, String[] matchToken
475477
}
476478
}
477479
// if we got here, there was no match found
478-
differences.addDifference(tokenToLocation.get(startToken), LicenseCompareHelper.getTokenAt(matchTokens, startToken), "Variable text rule "+rule.getName()+" did not match the compare text",
480+
differences.addDifference(tokenToLocation.get(startToken), LicenseTextHelper.getTokenAt(matchTokens, startToken), "Variable text rule "+rule.getName()+" did not match the compare text",
479481
null, rule, getLastOptionalDifference());
480482
return -1;
481483
}
@@ -567,7 +569,7 @@ public String[] getNextOptionalTextTokens() {
567569
}
568570
}
569571
Map<Integer, LineColumn> temp = new HashMap<>();
570-
return LicenseCompareHelper.tokenizeLicenseText(sb.toString(), temp);
572+
return LicenseTextHelper.tokenizeLicenseText(sb.toString(), temp);
571573
}
572574
}
573575

@@ -746,9 +748,9 @@ public void addDifference(LineColumn location, String token, String msg, String
746748
* @throws IOException This is not to be expected since we are using StringReaders
747749
*/
748750
public CompareTemplateOutputHandler(String compareText) throws IOException {
749-
this.compareText = LicenseCompareHelper.normalizeText(
750-
LicenseCompareHelper.replaceMultWord(LicenseCompareHelper.replaceSpaceComma(compareText)));
751-
this.compareTokens = LicenseCompareHelper.tokenizeLicenseText(this.compareText, tokenToLocation);
751+
this.compareText = LicenseTextHelper.normalizeText(
752+
LicenseTextHelper.replaceMultWord(LicenseTextHelper.replaceSpaceComma(compareText)));
753+
this.compareTokens = LicenseTextHelper.tokenizeLicenseText(this.compareText, tokenToLocation);
752754
}
753755

754756
/**
@@ -765,37 +767,37 @@ private int compareText(String[] textTokens, String[] matchTokens, int startToke
765767
return startToken;
766768
}
767769
int textTokenCounter = 0;
768-
String nextTextToken = LicenseCompareHelper.getTokenAt(textTokens, textTokenCounter++);
770+
String nextTextToken = LicenseTextHelper.getTokenAt(textTokens, textTokenCounter++);
769771
int matchTokenCounter = startToken;
770-
String nextMatchToken = LicenseCompareHelper.getTokenAt(matchTokens, matchTokenCounter++);
772+
String nextMatchToken = LicenseTextHelper.getTokenAt(matchTokens, matchTokenCounter++);
771773
while (nextTextToken != null) {
772774
if (nextMatchToken == null) {
773775
// end of compare text stream
774-
while (nextTextToken != null && LicenseCompareHelper.canSkip(nextTextToken)) {
775-
nextTextToken = LicenseCompareHelper.getTokenAt(textTokens, textTokenCounter++);
776+
while (nextTextToken != null && LicenseTextHelper.canSkip(nextTextToken)) {
777+
nextTextToken = LicenseTextHelper.getTokenAt(textTokens, textTokenCounter++);
776778
}
777779
if (nextTextToken != null) {
778780
return -matchTokenCounter; // there is more stuff in the compare license text, so not equiv.
779781
}
780-
} else if (LicenseCompareHelper.tokensEquivalent(nextTextToken, nextMatchToken)) {
782+
} else if (LicenseTextHelper.tokensEquivalent(nextTextToken, nextMatchToken)) {
781783
// just move onto the next set of tokens
782-
nextTextToken = LicenseCompareHelper.getTokenAt(textTokens, textTokenCounter++);
784+
nextTextToken = LicenseTextHelper.getTokenAt(textTokens, textTokenCounter++);
783785
if (nextTextToken != null) {
784-
nextMatchToken = LicenseCompareHelper.getTokenAt(matchTokens, matchTokenCounter++);
786+
nextMatchToken = LicenseTextHelper.getTokenAt(matchTokens, matchTokenCounter++);
785787
}
786788
} else {
787789
// see if we can skip through some compare tokens to find a match
788-
while (nextMatchToken != null && LicenseCompareHelper.canSkip(nextMatchToken)) {
789-
nextMatchToken = LicenseCompareHelper.getTokenAt(matchTokens, matchTokenCounter++);
790+
while (nextMatchToken != null && LicenseTextHelper.canSkip(nextMatchToken)) {
791+
nextMatchToken = LicenseTextHelper.getTokenAt(matchTokens, matchTokenCounter++);
790792
}
791793
// just to be sure, skip forward on the text
792-
while (nextTextToken != null && LicenseCompareHelper.canSkip(nextTextToken)) {
793-
nextTextToken = LicenseCompareHelper.getTokenAt(textTokens, textTokenCounter++);
794+
while (nextTextToken != null && LicenseTextHelper.canSkip(nextTextToken)) {
795+
nextTextToken = LicenseTextHelper.getTokenAt(textTokens, textTokenCounter++);
794796
}
795-
if (LicenseCompareHelper.tokensEquivalent(nextMatchToken, nextTextToken)) {
796-
nextTextToken = LicenseCompareHelper.getTokenAt(textTokens, textTokenCounter++);
797+
if (LicenseTextHelper.tokensEquivalent(nextMatchToken, nextTextToken)) {
798+
nextTextToken = LicenseTextHelper.getTokenAt(textTokens, textTokenCounter++);
797799
if (nextTextToken != null) {
798-
nextMatchToken = LicenseCompareHelper.getTokenAt(compareTokens, matchTokenCounter++);
800+
nextMatchToken = LicenseTextHelper.getTokenAt(compareTokens, matchTokenCounter++);
799801
}
800802
} else {
801803
if (textTokenCounter == textTokens.length &&
@@ -806,7 +808,7 @@ private int compareText(String[] textTokens, String[] matchTokens, int startToke
806808
//less than a token at the end of a compare
807809
//Yes - this is a bit of a hack
808810
String compareToken = nextTextToken + instruction.getNextOptionalTextTokens()[0];
809-
if (LicenseCompareHelper.tokensEquivalent(compareToken, nextMatchToken)) {
811+
if (LicenseTextHelper.tokensEquivalent(compareToken, nextMatchToken)) {
810812
instruction.skipNextInstruction();
811813
return matchTokenCounter;
812814
} else {
@@ -815,8 +817,8 @@ private int compareText(String[] textTokens, String[] matchTokens, int startToke
815817
if (nextNormalText != null) {
816818
compareToken = compareToken + nextNormalText;
817819
String compareWithoutOptional = nextTextToken + nextNormalText;
818-
if (LicenseCompareHelper.tokensEquivalent(compareToken, nextMatchToken) ||
819-
LicenseCompareHelper.tokensEquivalent(compareWithoutOptional, nextMatchToken)) {
820+
if (LicenseTextHelper.tokensEquivalent(compareToken, nextMatchToken) ||
821+
LicenseTextHelper.tokensEquivalent(compareWithoutOptional, nextMatchToken)) {
820822
instruction.skipNextInstruction();
821823
nextNormal.setSkipFirstToken(true);
822824
return matchTokenCounter;
@@ -910,7 +912,7 @@ public void completeParsing() throws LicenseParserException {
910912
int nextTokenIndex = this.topLevelInstruction.match(compareTokens, 0, compareTokens.length-1, compareText, differences, tokenToLocation);
911913
if (nextTokenIndex > 0 && nextTokenIndex < compareTokens.length) {
912914
this.differences.addDifference(tokenToLocation.get(nextTokenIndex),
913-
LicenseCompareHelper.getTokenAt(compareTokens, nextTokenIndex),
915+
LicenseTextHelper.getTokenAt(compareTokens, nextTokenIndex),
914916
"Additional text found after the end of the expected license text", null, null, null);
915917
}
916918
parsingComplete = true;
@@ -924,7 +926,7 @@ public void completeParsing() throws LicenseParserException {
924926
*/
925927
public int textEquivalent(String text, int startToken) {
926928
Map<Integer, LineColumn> textLocations = new HashMap<Integer, LineColumn>();
927-
String[] textTokens = LicenseCompareHelper.tokenizeLicenseText(text, textLocations);
929+
String[] textTokens = LicenseTextHelper.tokenizeLicenseText(text, textLocations);
928930
return this.compareText(textTokens, this.compareTokens, startToken, this.compareTokens.length-1, null);
929931
}
930932
}

0 commit comments

Comments
 (0)