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" awaitilityVersion = "3.1.6"
commonsioVersion = "2.5" commonsioVersion = "2.5"
commonslangVersion = "3.6" commonslangVersion = "3.6"
commonstextVersion = "1.3"
eventbusVersion = "3.1.1" eventbusVersion = "3.1.1"
glideVersion = "4.8.0" glideVersion = "4.8.0"
glideOkhttpIntegrationVersion = "4.8.0" glideOkhttpIntegrationVersion = "4.8.0"

View File

@ -62,7 +62,6 @@ dependencies {
implementation "android.arch.work:work-runtime:$workManagerVersion" implementation "android.arch.work:work-runtime:$workManagerVersion"
implementation "androidx.media:media:1.1.0" implementation "androidx.media:media:1.1.0"
implementation "org.apache.commons:commons-lang3:$commonslangVersion" implementation "org.apache.commons:commons-lang3:$commonslangVersion"
implementation "org.apache.commons:commons-text:$commonstextVersion"
implementation "commons-io:commons-io:$commonsioVersion" implementation "commons-io:commons-io:$commonsioVersion"
implementation "org.jsoup:jsoup:$jsoupVersion" implementation "org.jsoup:jsoup:$jsoupVersion"
implementation "com.github.bumptech.glide:glide:$glideVersion" 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; 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.Namespace;
import de.danoeh.antennapod.core.syndication.namespace.SyndElement; import de.danoeh.antennapod.core.syndication.namespace.SyndElement;
/** Represents Atom Element which contains text (content, title, summary). */ /** Represents Atom Element which contains text (content, title, summary). */
public class AtomText extends SyndElement { public class AtomText extends SyndElement {
public static final String TYPE_TEXT = "text"; public static final String TYPE_TEXT = "text";
private static final String TYPE_HTML = "html"; public static final String TYPE_HTML = "html";
private static final String TYPE_XHTML = "xhtml"; private static final String TYPE_XHTML = "xhtml";
private final String type; private final String type;
private String content; private String content;
public AtomText(String name, Namespace namespace, String type) { public AtomText(String name, Namespace namespace, String type) {
super(name, namespace); super(name, namespace);
this.type = type; this.type = type;
} }
/** Processes the content according to the type and returns it. */ /** Processes the content according to the type and returns it. */
public String getProcessedContent() { public String getProcessedContent() {
if (type == null) { if (type == null) {
return content; return content;
} else if (type.equals(TYPE_HTML)) { } else if (type.equals(TYPE_HTML)) {
return StringEscapeUtils.unescapeHtml4(content); if (Build.VERSION.SDK_INT >= 24) {
} else if (type.equals(TYPE_XHTML)) { return Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString();
return content; } else {
} else { // Handle as text by default return Html.fromHtml(content).toString();
return content; }
} } else if (type.equals(TYPE_XHTML)) {
} return content;
} else { // Handle as text by default
return content;
}
}
public String getContent() { public String getContent() {
return content; return content;
} }
public void setContent(String content) { public void setContent(String content) {
this.content = content; this.content = content;
} }
public String getType() { public String getType() {
return type; return type;
} }
} }

View File

@ -3,45 +3,48 @@ package de.danoeh.antennapod.core.util;
import android.text.TextUtils; import android.text.TextUtils;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.text.RandomStringGenerator;
/** Generates valid filenames for a given string. */ /** Generates valid filenames for a given string. */
public class FileNameGenerator { public class FileNameGenerator {
private static final char[] validChars = (
"abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"0123456789" +
" _-").toCharArray();
private FileNameGenerator() {
}
/** private static final char[] validChars =
* This method will return a new string that doesn't contain any illegal ("abcdefghijklmnopqrstuvwxyz"
* characters of the given string. + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
*/ + "0123456789"
public static String generateFileName(String string) { + " _-").toCharArray();
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 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();
}
} }