From 511c5d446ff84c40363af3ff5d9a73c4fcf2e669 Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Wed, 21 May 2025 16:31:39 +0100 Subject: [PATCH 1/3] Pre-compile Patterns fo removeCommentChars Signed-off-by: Arthit Suriyawongkul --- .../utility/compare/LicenseCompareHelper.java | 77 +++++++++++-------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java b/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java index 0915a0d6..76cb05b3 100644 --- a/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java +++ b/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java @@ -63,10 +63,18 @@ public class LicenseCompareHelper { protected static final Pattern REGEX_QUANTIFIER_PATTERN = Pattern.compile(".*\\.\\{(\\d+),(\\d+)}$"); static final String START_COMMENT_CHAR_PATTERN = "(//|/\\*|\\*|#|' |REM ||-}|\\*\\)|\\s\\*)\\s*$"); + static final Pattern START_COMMENT_PATTERN = Pattern.compile("^\\s*" + START_COMMENT_CHAR_PATTERN); + static final Pattern BEGIN_OPTIONAL_COMMENT_PATTERN = Pattern + .compile("^\\s*<>\\s*" + START_COMMENT_CHAR_PATTERN); + /** - * @param objectUri URI of the license - * @return license ID + * Convert a license object URI to its corresponding License ID + * + * @param objectUri The URI of the license. + * @return The SPDX License ID extracted from the URI, or the original + * {@code objectUri} if no known prefix is found. */ public static String licenseUriToLicenseId(String objectUri) { if (objectUri.startsWith(SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX)) { @@ -77,41 +85,46 @@ public static String licenseUriToLicenseId(String objectUri) { return objectUri; // no match - should we throw an exception? } } - + /** - * Remove common comment characters from either a template or license text strings + * Remove common comment characters from either a template or license text + * strings + * * @param s string source * @return string without comment characters */ public static String removeCommentChars(String s) { - StringBuilder sb = new StringBuilder(); - BufferedReader reader = null; - try { - reader = new BufferedReader(new StringReader(s)); - String line = reader.readLine(); - while (line != null) { - line = line.replaceAll("(\\*/|-->|-}|\\*\\)|\\s\\*)\\s*$", ""); // remove end of line comments - line = line.replaceAll("^\\s*" + START_COMMENT_CHAR_PATTERN, ""); // remove start of line comments - line = line.replaceAll("^\\s*<>\\s*" + START_COMMENT_CHAR_PATTERN, "<>"); - sb.append(line); - sb.append("\n"); - line = reader.readLine(); - } - return sb.toString(); - } catch (IOException e) { - logger.warn("IO error reading strings?!?", e); - return s; - } finally { - if (Objects.nonNull(reader)) { - try { - reader.close(); - } catch (IOException e) { - logger.warn("IO error closing a string reader?!?", e); - } - } - } + if (s == null) { + return ""; + } + StringBuilder sb = new StringBuilder(); + BufferedReader reader = null; + try { + reader = new BufferedReader(new StringReader(s)); + String line = reader.readLine(); + while (line != null) { + line = END_COMMENT_PATTERN.matcher(line).replaceAll(""); + line = START_COMMENT_PATTERN.matcher(line).replaceAll(""); + line = BEGIN_OPTIONAL_COMMENT_PATTERN.matcher(line).replaceAll("<>"); + sb.append(line); + sb.append("\n"); + line = reader.readLine(); + } + return sb.toString(); + } catch (IOException e) { + logger.warn("IO error reading strings?!?", e); + return s; + } finally { + if (Objects.nonNull(reader)) { + try { + reader.close(); + } catch (IOException e) { + logger.warn("IO error closing a string reader?!?", e); + } + } + } } - + /** * Locate the original text starting with the start token and ending with the end token * @param fullLicenseText entire license text From 5080c78adf7b640c129f3a7fb2e13acc6bb012da Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Wed, 21 May 2025 16:46:16 +0100 Subject: [PATCH 2/3] Check for empty string Signed-off-by: Arthit Suriyawongkul --- .../java/org/spdx/utility/compare/LicenseCompareHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java b/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java index 76cb05b3..8db6cee3 100644 --- a/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java +++ b/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java @@ -94,7 +94,7 @@ public static String licenseUriToLicenseId(String objectUri) { * @return string without comment characters */ public static String removeCommentChars(String s) { - if (s == null) { + if (s == null || s.isEmpty()) { return ""; } StringBuilder sb = new StringBuilder(); From ba7f83d8489e2667b8902ebeb0bcc3cc53c7c482 Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Wed, 21 May 2025 17:05:16 +0100 Subject: [PATCH 3/3] Ensure no trailing newline Also use try-resource to manage the closing of the resource instead of checking it in finally Signed-off-by: Arthit Suriyawongkul --- .../utility/compare/LicenseCompareHelper.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java b/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java index 8db6cee3..08f75f6b 100644 --- a/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java +++ b/src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java @@ -98,31 +98,27 @@ public static String removeCommentChars(String s) { return ""; } StringBuilder sb = new StringBuilder(); - BufferedReader reader = null; - try { - reader = new BufferedReader(new StringReader(s)); + try (BufferedReader reader = new BufferedReader(new StringReader(s))) { String line = reader.readLine(); + boolean firstLine = true; while (line != null) { line = END_COMMENT_PATTERN.matcher(line).replaceAll(""); line = START_COMMENT_PATTERN.matcher(line).replaceAll(""); line = BEGIN_OPTIONAL_COMMENT_PATTERN.matcher(line).replaceAll("<>"); + + if (!firstLine) { + sb.append("\n"); + } else { + firstLine = false; + } sb.append(line); - sb.append("\n"); line = reader.readLine(); } - return sb.toString(); } catch (IOException e) { logger.warn("IO error reading strings?!?", e); return s; - } finally { - if (Objects.nonNull(reader)) { - try { - reader.close(); - } catch (IOException e) { - logger.warn("IO error closing a string reader?!?", e); - } - } } + return sb.toString(); } /**