Skip to content

Commit

Permalink
Fixing issues detected by Sonar (#28929)
Browse files Browse the repository at this point in the history
  • Loading branch information
victoralfaro-dotcms authored Jun 19, 2024
1 parent f319f84 commit fac6b7a
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions dotCMS/src/main/java/com/dotcms/ai/util/ContentToStringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ContentToStringUtil> 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 -
Expand All @@ -56,7 +58,7 @@ public class ContentToStringUtil {

};

private static final Lazy<List<Pattern>> MARKDOWN_PATTERNS = Lazy.of(() -> Arrays.stream(MARKDOWN_STRING_PATTERNS).map(Pattern::compile).collect(Collectors.toList()));
private static final Lazy<List<Pattern>> MARKDOWN_PATTERNS = Lazy.of(() -> Arrays.stream(MARKDOWN_STRING_PATTERNS).map(Pattern::compile).collect(Collectors.toUnmodifiableList()));

private static final Pattern HTML_PATTERN = Pattern.compile(".*\\<[^>]+>.*");

Expand Down Expand Up @@ -148,19 +150,19 @@ public List<Field> guessWhatFieldsToIndex(@NotNull Contentlet contentlet) {

final List<Field> 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();
}

Expand All @@ -180,7 +182,7 @@ public List<Field> 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) {
Expand Down Expand Up @@ -274,10 +276,10 @@ private Optional<String> 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();
}
Expand Down

0 comments on commit fac6b7a

Please sign in to comment.