Add a util method to clean a string

This commit is contained in:
Shinokuni 2019-10-11 14:58:06 +02:00
parent 1a38155c01
commit 7946b2a62d
3 changed files with 32 additions and 4 deletions

View File

@ -13,7 +13,6 @@ import com.readrops.readropslibrary.utils.ParseException;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.jsoup.Jsoup;
import java.util.ArrayList;
import java.util.List;
@ -72,7 +71,7 @@ public final class ItemMatcher {
newItem.setContent(item.getContent()); // Jsoup.clean(item.getContent(), Whitelist.relaxed())
newItem.setDescription(item.getDescription());
newItem.setGuid(item.getGuid());
newItem.setTitle(Jsoup.parse(item.getTitle()).text().trim());
newItem.setTitle(Utils.cleanText(item.getTitle()));
try {
newItem.setPubDate(DateUtils.stringToLocalDateTime(item.getDate()));
@ -117,13 +116,14 @@ public final class ItemMatcher {
dbItem.setContent(item.getContent()); // Jsoup.clean(item.getContent(), Whitelist.relaxed())
dbItem.setDescription(item.getSummary());
dbItem.setGuid(item.getId());
dbItem.setTitle(Jsoup.parse(item.getTitle()).text().trim());
dbItem.setTitle(Utils.cleanText(item.getTitle()));
try {
dbItem.setPubDate(DateUtils.stringToLocalDateTime(item.getUpdated()));
} catch (Exception e) {
throw new ParseException();
}
dbItem.setLink(item.getUrl());
dbItem.setFeedId(feed.getId());
@ -146,7 +146,7 @@ public final class ItemMatcher {
dbItem.setContent(item.getContent()); // Jsoup.clean(item.getContent(), Whitelist.relaxed())
dbItem.setDescription(item.getSummary());
dbItem.setGuid(item.getId());
dbItem.setTitle(Jsoup.parse(item.getTitle()).text().trim());
dbItem.setTitle(Utils.cleanText(item.getTitle()));
try {
dbItem.setPubDate(DateUtils.stringToLocalDateTime(item.getPubDate()));

View File

@ -15,6 +15,8 @@ import androidx.annotation.NonNull;
import com.google.android.material.snackbar.Snackbar;
import org.jsoup.Jsoup;
import java.io.InputStream;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
@ -95,4 +97,13 @@ public final class Utils {
Snackbar snackbar = Snackbar.make(root, message, Snackbar.LENGTH_LONG);
snackbar.show();
}
/**
* Remove html tags and trim the text
* @param text string to clean
* @return cleaned text
*/
public static String cleanText(String text) {
return Jsoup.parse(text).text().trim();
}
}

View File

@ -0,0 +1,17 @@
package com.readrops.app;
import com.readrops.app.utils.Utils;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class UtilsTest {
@Test
public void cleanTextTest() {
String text = " <p>This is a text<br/>to</p> clean ";
assertEquals("This is a text to clean", Utils.cleanText(text));
}
}