diff --git a/src/main/java/org/olat/core/util/StringHelper.java b/src/main/java/org/olat/core/util/StringHelper.java index aa718043ee2d855f5b612184b9fe9d25b66d0d79..741cb9dabd5f0fef7d3e08c2b8ed6919bd3d11c4 100644 --- a/src/main/java/org/olat/core/util/StringHelper.java +++ b/src/main/java/org/olat/core/util/StringHelper.java @@ -51,6 +51,7 @@ import org.olat.core.logging.AssertException; import org.apache.logging.log4j.Logger; import org.olat.core.logging.Tracing; import org.olat.core.util.filter.impl.HtmlScanner; +import org.olat.core.util.filter.FilterFactory; import org.olat.core.util.filter.impl.OWASPAntiSamyXSSFilter; import org.olat.user.UserManager; @@ -625,4 +626,19 @@ public class StringHelper { return formatAsCSVString(rv); } -} \ No newline at end of file + + public static String truncateText(String text) { + String shortenedText = ""; + if (text != null) { + String shortText = FilterFactory.getHtmlTagsFilter().filter(text); + if(shortText.length() > 255) { + shortenedText = shortText.substring(0, 255); + } else { + shortenedText = shortText; + } + } else { + shortenedText = ""; + } + return shortenedText; + } +} diff --git a/src/main/java/org/olat/repository/ui/author/AuthoringEntryRow.java b/src/main/java/org/olat/repository/ui/author/AuthoringEntryRow.java index b950968ac0163651a36d80fd0c24e8fc07ee1c59..a4e902be52123b083440fdb6f89f1cbe133799e8 100644 --- a/src/main/java/org/olat/repository/ui/author/AuthoringEntryRow.java +++ b/src/main/java/org/olat/repository/ui/author/AuthoringEntryRow.java @@ -25,7 +25,7 @@ import java.util.List; import org.olat.core.commons.services.license.License; import org.olat.core.gui.components.form.flexible.elements.FormLink; import org.olat.core.id.OLATResourceable; -import org.olat.core.util.filter.FilterFactory; +import org.olat.core.util.StringHelper; import org.olat.core.util.resource.OresHelper; import org.olat.repository.RepositoryEntryAuthorView; import org.olat.repository.RepositoryEntryLight; @@ -94,16 +94,7 @@ public class AuthoringEntryRow implements RepositoryEntryRef, RepositoryEntryLig author = fullnameAuthor; authors = view.getAuthors(); location = view.getLocation(); - if(view.getDescription() != null) { - String shortDesc = FilterFactory.getHtmlTagsFilter().filter(view.getDescription()); - if(shortDesc.length() > 255) { - shortenedDescription = shortDesc.substring(0, 255); - } else { - shortenedDescription = shortDesc; - } - } else { - shortenedDescription = ""; - } + shortenedDescription = StringHelper.truncateText(view.getDescription()); lastUsage = view.getLastUsage(); creationDate = view.getCreationDate(); diff --git a/src/main/java/org/olat/repository/ui/catalog/CatalogEntryRow.java b/src/main/java/org/olat/repository/ui/catalog/CatalogEntryRow.java index 9adcebc4db2e6ec59bd4afdd4a458b00bfc9a04e..6561b24910ecd9ef4db27fe5fa00dda596e859de 100644 --- a/src/main/java/org/olat/repository/ui/catalog/CatalogEntryRow.java +++ b/src/main/java/org/olat/repository/ui/catalog/CatalogEntryRow.java @@ -23,7 +23,7 @@ import java.util.Date; import java.util.List; import org.olat.core.id.OLATResourceable; -import org.olat.core.util.filter.FilterFactory; +import org.olat.core.util.StringHelper; import org.olat.core.util.resource.OresHelper; import org.olat.repository.RepositoryEntry; import org.olat.repository.RepositoryEntryLight; @@ -72,17 +72,7 @@ public class CatalogEntryRow implements RepositoryEntryRef, RepositoryEntryLight key = view.getKey(); name = view.getDisplayname(); authors = view.getAuthors(); - if(view.getDescription() != null) { - String shortDesc = FilterFactory.getHtmlTagsFilter().filter(view.getDescription()); - if(shortDesc.length() > 255) { - shortenedDescription = shortDesc.substring(0, 255); - } else { - shortenedDescription = shortDesc; - } - } else { - shortenedDescription = ""; - } - + shortenedDescription = StringHelper.truncateText(view.getDescription()); creationDate = view.getCreationDate(); externalId = view.getExternalId(); diff --git a/src/main/java/org/olat/repository/ui/list/RepositoryEntryRow.java b/src/main/java/org/olat/repository/ui/list/RepositoryEntryRow.java index 503405a8ae2fa2497ae1b5898268840258cf104e..4ff27173e75151ecd563215a6b477180fc56961e 100644 --- a/src/main/java/org/olat/repository/ui/list/RepositoryEntryRow.java +++ b/src/main/java/org/olat/repository/ui/list/RepositoryEntryRow.java @@ -26,7 +26,6 @@ import org.olat.core.gui.components.form.flexible.FormItem; import org.olat.core.gui.components.form.flexible.elements.FormLink; import org.olat.core.id.OLATResourceable; import org.olat.core.util.StringHelper; -import org.olat.core.util.filter.FilterFactory; import org.olat.core.util.resource.OresHelper; import org.olat.course.assessment.AssessmentHelper; import org.olat.repository.RepositoryEntryMyView; @@ -93,16 +92,7 @@ public class RepositoryEntryRow implements RepositoryEntryRef { externalId = entry.getExternalId(); externalRef = entry.getExternalRef(); name = entry.getDisplayname(); - if(entry.getDescription() != null) { - String shortDesc = FilterFactory.getHtmlTagsFilter().filter(entry.getDescription()); - if(shortDesc.length() > 255) { - shortenedDescription = shortDesc.substring(0, 255); - } else { - shortenedDescription = shortDesc; - } - } else { - shortenedDescription = ""; - } + shortenedDescription = StringHelper.truncateText(entry.getDescription()); setOLATResourceable(OresHelper.clone(entry.getOlatResource())); authors = entry.getAuthors(); location = entry.getLocation(); diff --git a/src/test/java/org/olat/core/util/StringHelperTest.java b/src/test/java/org/olat/core/util/StringHelperTest.java index 4322a8ea12ef0b27a929113333e1b1135a70a24a..618d6ec1e21de9ce86e291184389ee6463b19be1 100644 --- a/src/test/java/org/olat/core/util/StringHelperTest.java +++ b/src/test/java/org/olat/core/util/StringHelperTest.java @@ -204,4 +204,23 @@ public class StringHelperTest { String csv = StringHelper.formatAsEscapedCSVString(entries); Assert.assertEquals("Hell\"\"o\"\",\"Test,dru,\",Final", csv); } + + @Test + public void truncateText() { + Assert.assertEquals(StringHelper.truncateText("foobar"), "foobar"); + } + + @Test + public void truncateText_null() { + Assert.assertEquals(StringHelper.truncateText(null), ""); + } + + @Test + public void truncateText_long() { + Assert.assertEquals( + StringHelper.truncateText( + "Destination event Execute Pending What I Ideate Conceptualize discovered was Brand Goals the Fyre manner Understand Festival. A a unique music, art revenue in and food capture brand festival taking allows to place on 360 methodology the private Festival. Our island of for Fyre Fyre Cay."), + "Destination event Execute Pending What I Ideate Conceptualize discovered was Brand Goals the Fyre manner Understand Festival. A a unique music, art revenue in and food capture brand festival taking allows to place on 360 methodology the private Festival. "); + } + }