Skip to content

Commit 2797fe1

Browse files
bactgoneall
authored andcommitted
Count non-whitespace token
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
1 parent 3801f04 commit 2797fe1

1 file changed

Lines changed: 24 additions & 21 deletions

File tree

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ public static String removeCommentChars(String s) {
123123
}
124124

125125
/**
126-
* Locate the original text starting with the start token and ending with the end token
126+
* Locate the original text starting with the start token and ending with the
127+
* end token
128+
*
127129
* @param fullLicenseText entire license text
128-
* @param startToken starting token
129-
* @param endToken ending token
130+
* @param startToken starting token
131+
* @param endToken ending token
130132
* @param tokenToLocation token location
131-
* @return original text starting with the start token and ending with the end token
133+
* @return original text starting with the start token and ending with the end
134+
* token
132135
*/
133136
public static String locateOriginalText(String fullLicenseText, int startToken, int endToken,
134137
Map<Integer, LineColumn> tokenToLocation, String[] tokens) {
@@ -224,39 +227,35 @@ public static String locateOriginalText(String fullLicenseText, int startToken,
224227
/**
225228
* Check whether the given text contains only a single token
226229
* <p>
227-
* A single token string is defined as a non-null string that does not
228-
* contain any newline characters and contains exactly one non-empty token
229-
* as identified by the {@link LicenseTextHelper#TOKEN_SPLIT_PATTERN}.
230+
* A single token string is a string that contains zero or one token as
231+
* identified by the {@link LicenseTextHelper#TOKEN_SPLIT_PATTERN}.
230232
* </p>
231233
*
232234
* @param text The text to test.
233-
* @return {@code true} if the text contains a single token,
235+
* @return {@code true} if the text contains zero one token,
234236
* {@code false} otherwise.
235237
*/
236238
public static boolean isSingleTokenString(@Nullable String text) {
237239
if (text == null || text.isEmpty()) {
238-
return false;
239-
}
240-
if (text.contains("\n")) {
241-
return false;
240+
return true; // zero tokens is considered a single token string
242241
}
243242
Matcher m = LicenseTextHelper.TOKEN_SPLIT_PATTERN.matcher(text);
244-
boolean found = false;
243+
int nonWhitespaceTokenCount = 0;
245244
while (m.find()) {
246245
if (!m.group(1).trim().isEmpty()) {
247-
if (found) {
248-
return false;
249-
} else {
250-
found = true;
246+
nonWhitespaceTokenCount++;
247+
if (nonWhitespaceTokenCount > 1) {
248+
return false; // more than one token
251249
}
252250
}
253251
}
254-
return found;
252+
return true; // either no tokens or one token
255253
}
256254

257255
/**
258256
* Compares two licenses from potentially two different documents which may have
259257
* different license ID's for the same license
258+
*
260259
* @param license1 first license to compare
261260
* @param license2 second license to compare
262261
* @param xlationMap Mapping the license ID's from license 1 to license 2
@@ -367,11 +366,15 @@ public static List<String> getNonOptionalLicenseText(String licenseTemplate,
367366
}
368367

369368
/**
370-
* @param template Template in the standard template format used for comparison
369+
* Compare the provided text against a license template using SPDX matching
370+
* guidelines
371+
*
372+
* @param template Template in the standard template format used for
373+
* comparison
371374
* @param compareText Text to compare using the template
372-
* @return any differences found
375+
* @return Any differences found
373376
* @throws SpdxCompareException on comparison errors
374-
*/
377+
*/
375378
public static DifferenceDescription isTextMatchingTemplate(String template, String compareText) throws SpdxCompareException {
376379
CompareTemplateOutputHandler compareTemplateOutputHandler;
377380
try {

0 commit comments

Comments
 (0)