Merge pull request #3587 from ByteHamster/remove-commons-text

Removed commons text library that was just used 2 times
This commit is contained in:
H. Lehmann 2019-11-06 19:38:01 +01:00 committed by GitHub
commit 3c0489890c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 123 additions and 69 deletions

View File

@ -49,7 +49,6 @@ project.ext {
awaitilityVersion = "3.1.6"
commonsioVersion = "2.5"
commonslangVersion = "3.6"
commonstextVersion = "1.3"
eventbusVersion = "3.1.1"
glideVersion = "4.8.0"
glideOkhttpIntegrationVersion = "4.8.0"

View File

@ -62,7 +62,6 @@ dependencies {
implementation "android.arch.work:work-runtime:$workManagerVersion"
implementation "androidx.media:media:1.1.0"
implementation "org.apache.commons:commons-lang3:$commonslangVersion"
implementation "org.apache.commons:commons-text:$commonstextVersion"
implementation "commons-io:commons-io:$commonsioVersion"
implementation "org.jsoup:jsoup:$jsoupVersion"
implementation "com.github.bumptech.glide:glide:$glideVersion"

View File

@ -0,0 +1,49 @@
package de.danoeh.antennapod.core.syndication.namespace.atom;
import androidx.test.filters.SmallTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.runners.Parameterized.Parameter;
import static org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertEquals;
/**
* Unit test for {@link AtomText}.
*/
@SmallTest
@RunWith(Parameterized.class)
public class AtomTextTest {
@Parameter(value = 0)
public String input;
@Parameter(value = 1)
public String expectedOutput;
@Parameters
public static Collection<Object[]> initParameters() {
return Arrays.asList(new Object[][] {
{"&gt;", ">"},
{">", ">"},
{"&lt;Fran&ccedil;ais&gt;", "<Français>"},
{"ßÄÖÜ", "ßÄÖÜ"},
{"&quot;", "\""},
{"&szlig;", "ß"},
{"&#8217;", ""},
{"&#x2030;", ""},
{"&euro;", ""},
});
}
@Test
public void testProcessingHtml() {
final AtomText atomText = new AtomText("", new NSAtom(), AtomText.TYPE_HTML);
atomText.setContent(input);
assertEquals(expectedOutput, atomText.getProcessedContent());
}
}

View File

@ -1,47 +1,51 @@
package de.danoeh.antennapod.core.syndication.namespace.atom;
import org.apache.commons.text.StringEscapeUtils;
import android.os.Build;
import android.text.Html;
import de.danoeh.antennapod.core.syndication.namespace.Namespace;
import de.danoeh.antennapod.core.syndication.namespace.SyndElement;
/** Represents Atom Element which contains text (content, title, summary). */
public class AtomText extends SyndElement {
public static final String TYPE_TEXT = "text";
private static final String TYPE_HTML = "html";
private static final String TYPE_XHTML = "xhtml";
public static final String TYPE_TEXT = "text";
public static final String TYPE_HTML = "html";
private static final String TYPE_XHTML = "xhtml";
private final String type;
private String content;
private final String type;
private String content;
public AtomText(String name, Namespace namespace, String type) {
super(name, namespace);
this.type = type;
}
public AtomText(String name, Namespace namespace, String type) {
super(name, namespace);
this.type = type;
}
/** Processes the content according to the type and returns it. */
public String getProcessedContent() {
if (type == null) {
return content;
} else if (type.equals(TYPE_HTML)) {
return StringEscapeUtils.unescapeHtml4(content);
} else if (type.equals(TYPE_XHTML)) {
return content;
} else { // Handle as text by default
return content;
}
}
/** Processes the content according to the type and returns it. */
public String getProcessedContent() {
if (type == null) {
return content;
} else if (type.equals(TYPE_HTML)) {
if (Build.VERSION.SDK_INT >= 24) {
return Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString();
} else {
return Html.fromHtml(content).toString();
}
} else if (type.equals(TYPE_XHTML)) {
return content;
} else { // Handle as text by default
return content;
}
}
public String getContent() {
return content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public void setContent(String content) {
this.content = content;
}
public String getType() {
return type;
}
public String getType() {
return type;
}
}

View File

@ -3,45 +3,48 @@ package de.danoeh.antennapod.core.util;
import android.text.TextUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.text.RandomStringGenerator;
/** Generates valid filenames for a given string. */
public class FileNameGenerator {
private static final char[] validChars = (
"abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"0123456789" +
" _-").toCharArray();
private FileNameGenerator() {
}
/**
* This method will return a new string that doesn't contain any illegal
* characters of the given string.
*/
public static String generateFileName(String string) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if(Character.isSpaceChar(c) && (buf.length() == 0 || Character.isSpaceChar(buf.charAt(buf.length()-1)))) {
continue;
}
if (ArrayUtils.contains(validChars, c)) {
buf.append(c);
}
}
String filename = buf.toString().trim();
if(TextUtils.isEmpty(filename)) {
return new RandomStringGenerator.Builder()
.withinRange('0', 'z')
.filteredBy(Character::isLetterOrDigit)
.build()
.generate(8);
}
return filename;
}
private static final char[] validChars =
("abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "0123456789"
+ " _-").toCharArray();
private FileNameGenerator() {
}
/**
* This method will return a new string that doesn't contain any illegal
* characters of the given string.
*/
public static String generateFileName(String string) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if (Character.isSpaceChar(c)
&& (buf.length() == 0 || Character.isSpaceChar(buf.charAt(buf.length() - 1)))) {
continue;
}
if (ArrayUtils.contains(validChars, c)) {
buf.append(c);
}
}
String filename = buf.toString().trim();
if (TextUtils.isEmpty(filename)) {
return randomString(8);
}
return filename;
}
private static String randomString(int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(validChars[(int) (Math.random() * validChars.length)]);
}
return sb.toString();
}
}