From fac6b7a4a063456bcd473502efb4f03268872937 Mon Sep 17 00:00:00 2001 From: Victor Alfaro Date: Tue, 18 Jun 2024 22:31:45 -0600 Subject: [PATCH] Fixing issues detected by Sonar (#28929) --- .../dotcms/ai/util/ContentToStringUtil.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/dotCMS/src/main/java/com/dotcms/ai/util/ContentToStringUtil.java b/dotCMS/src/main/java/com/dotcms/ai/util/ContentToStringUtil.java index a5daeacc687b..b0cf21376b05 100644 --- a/dotCMS/src/main/java/com/dotcms/ai/util/ContentToStringUtil.java +++ b/dotCMS/src/main/java/com/dotcms/ai/util/ContentToStringUtil.java @@ -16,7 +16,6 @@ import com.dotmarketing.util.Logger; import com.dotmarketing.util.StringUtils; import com.dotmarketing.util.UtilMethods; -import com.liferay.util.StringPool; import io.vavr.Lazy; import io.vavr.control.Try; import org.apache.felix.framework.OSGISystem; @@ -40,7 +39,10 @@ */ public class ContentToStringUtil { + public static final String FILE_ASSET_KEY = "fileAsset"; + public static final String ASSET_KEY = "asset"; public static final Lazy impl = Lazy.of(ContentToStringUtil::new); + private static final String[] MARKDOWN_STRING_PATTERNS = { "(^|[\\n])\\s*1\\.\\s.*\\s+1\\.\\s", // markdown list with 1. \n 1. "(^|[\\n])\\s*-\\s.*\\s+-\\s", // markdown unordered list - @@ -56,7 +58,7 @@ public class ContentToStringUtil { }; - private static final Lazy> MARKDOWN_PATTERNS = Lazy.of(() -> Arrays.stream(MARKDOWN_STRING_PATTERNS).map(Pattern::compile).collect(Collectors.toList())); + private static final Lazy> MARKDOWN_PATTERNS = Lazy.of(() -> Arrays.stream(MARKDOWN_STRING_PATTERNS).map(Pattern::compile).collect(Collectors.toUnmodifiableList())); private static final Pattern HTML_PATTERN = Pattern.compile(".*\\<[^>]+>.*"); @@ -148,19 +150,19 @@ public List guessWhatFieldsToIndex(@NotNull Contentlet contentlet) { final List embedMe = new ArrayList<>(); if (contentlet.isFileAsset()) { - File fileAsset = Try.of(() -> contentlet.getBinary("fileAsset")).getOrNull(); + File fileAsset = Try.of(() -> contentlet.getBinary(FILE_ASSET_KEY)).getOrNull(); if (shouldIndex(fileAsset)) { - embedMe.add(contentlet.getContentType().fieldMap().get("fileAsset")); + embedMe.add(contentlet.getContentType().fieldMap().get(FILE_ASSET_KEY)); } } if (contentlet.isDotAsset()) { - File asset = Try.of(() -> contentlet.getBinary("asset")).getOrNull(); + File asset = Try.of(() -> contentlet.getBinary(ASSET_KEY)).getOrNull(); if (shouldIndex(asset)) { - embedMe.add(contentlet.getContentType().fieldMap().get("asset")); + embedMe.add(contentlet.getContentType().fieldMap().get(ASSET_KEY)); } } // pages are not indexed based on fields, instead they will be rendered to be parsed - if (contentlet.isHTMLPage()) { + if (Boolean.TRUE.equals(contentlet.isHTMLPage())) { return List.of(); } @@ -180,7 +182,7 @@ public List guessWhatFieldsToIndex(@NotNull Contentlet contentlet) { .stream().filter(f -> !ignoreUrlMapFields.contains("{" + f.variable() + "}")) .filter(f -> f.dataType().equals(DataTypes.LONG_TEXT) - ).collect(Collectors.toList()); + ).collect(Collectors.toUnmodifiableList()); } private boolean shouldIndex(File file) { @@ -274,10 +276,10 @@ private Optional handleFileField(Contentlet contentlet, String identifie return Optional.empty(); } if(con.get().isDotAsset()){ - return parseFile(Try.of(() -> con.get().getBinary("asset")).getOrNull()); + return parseFile(Try.of(() -> con.get().getBinary(ASSET_KEY)).getOrNull()); } else if(con.get().isFileAsset()){ - return parseFile(Try.of(() -> con.get().getBinary("fileAsset")).getOrNull()); + return parseFile(Try.of(() -> con.get().getBinary(FILE_ASSET_KEY)).getOrNull()); } return Optional.empty(); }