Fix remaining checkstyle issues in utils/ subpackage
This commit is contained in:
parent
1d5f22e41f
commit
ca7c63f273
5 changed files with 79 additions and 71 deletions
|
@ -11,31 +11,36 @@ import org.schabi.newpipe.extractor.stream.StreamInfo;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ExtractorHelper {
|
public final class ExtractorHelper {
|
||||||
private ExtractorHelper() {}
|
private ExtractorHelper() {
|
||||||
|
}
|
||||||
|
|
||||||
public static <T extends InfoItem> InfoItemsPage<T> getItemsPageOrLogError(Info info, ListExtractor<T> extractor) {
|
public static <T extends InfoItem> InfoItemsPage<T> getItemsPageOrLogError(
|
||||||
|
final Info info, final ListExtractor<T> extractor) {
|
||||||
try {
|
try {
|
||||||
InfoItemsPage<T> page = extractor.getInitialPage();
|
final InfoItemsPage<T> page = extractor.getInitialPage();
|
||||||
info.addAllErrors(page.getErrors());
|
info.addAllErrors(page.getErrors());
|
||||||
|
|
||||||
return page;
|
return page;
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
info.addError(e);
|
info.addError(e);
|
||||||
return InfoItemsPage.emptyPage();
|
return InfoItemsPage.emptyPage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<InfoItem> getRelatedItemsOrLogError(StreamInfo info, StreamExtractor extractor) {
|
public static List<InfoItem> getRelatedItemsOrLogError(final StreamInfo info,
|
||||||
|
final StreamExtractor extractor) {
|
||||||
try {
|
try {
|
||||||
InfoItemsCollector<? extends InfoItem, ?> collector = extractor.getRelatedItems();
|
final InfoItemsCollector<? extends InfoItem, ?> collector = extractor.getRelatedItems();
|
||||||
if (collector == null) return Collections.emptyList();
|
if (collector == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
info.addAllErrors(collector.getErrors());
|
info.addAllErrors(collector.getErrors());
|
||||||
|
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
return (List<InfoItem>) collector.getItems();
|
return (List<InfoItem>) collector.getItems();
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
info.addError(e);
|
info.addError(e);
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
@ -45,7 +50,8 @@ public class ExtractorHelper {
|
||||||
* @deprecated Use {@link #getRelatedItemsOrLogError(StreamInfo, StreamExtractor)}
|
* @deprecated Use {@link #getRelatedItemsOrLogError(StreamInfo, StreamExtractor)}
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static List<InfoItem> getRelatedVideosOrLogError(StreamInfo info, StreamExtractor extractor) {
|
public static List<InfoItem> getRelatedVideosOrLogError(final StreamInfo info,
|
||||||
|
final StreamExtractor extractor) {
|
||||||
return getRelatedItemsOrLogError(info, extractor);
|
return getRelatedItemsOrLogError(info, extractor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import org.mozilla.javascript.Context;
|
||||||
import org.mozilla.javascript.Function;
|
import org.mozilla.javascript.Function;
|
||||||
import org.mozilla.javascript.ScriptableObject;
|
import org.mozilla.javascript.ScriptableObject;
|
||||||
|
|
||||||
public class JavaScript {
|
public final class JavaScript {
|
||||||
|
|
||||||
private JavaScript() {
|
private JavaScript() {
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,60 +39,66 @@ import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
|
||||||
/**
|
/**
|
||||||
* avoid using regex !!!
|
* avoid using regex !!!
|
||||||
*/
|
*/
|
||||||
public class Parser {
|
public final class Parser {
|
||||||
|
|
||||||
private Parser() {
|
private Parser() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class RegexException extends ParsingException {
|
public static class RegexException extends ParsingException {
|
||||||
public RegexException(String message) {
|
public RegexException(final String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String matchGroup1(String pattern, String input) throws RegexException {
|
public static String matchGroup1(final String pattern, final String input)
|
||||||
|
throws RegexException {
|
||||||
return matchGroup(pattern, input, 1);
|
return matchGroup(pattern, input, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String matchGroup1(Pattern pattern, String input) throws RegexException {
|
public static String matchGroup1(final Pattern pattern,
|
||||||
|
final String input) throws RegexException {
|
||||||
return matchGroup(pattern, input, 1);
|
return matchGroup(pattern, input, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String matchGroup(String pattern, String input, int group) throws RegexException {
|
public static String matchGroup(final String pattern,
|
||||||
Pattern pat = Pattern.compile(pattern);
|
final String input,
|
||||||
return matchGroup(pat, input, group);
|
final int group) throws RegexException {
|
||||||
|
return matchGroup(Pattern.compile(pattern), input, group);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String matchGroup(Pattern pat, String input, int group) throws RegexException {
|
public static String matchGroup(final Pattern pat, final String input, final int group)
|
||||||
Matcher mat = pat.matcher(input);
|
throws RegexException {
|
||||||
boolean foundMatch = mat.find();
|
final Matcher matcher = pat.matcher(input);
|
||||||
|
final boolean foundMatch = matcher.find();
|
||||||
if (foundMatch) {
|
if (foundMatch) {
|
||||||
return mat.group(group);
|
return matcher.group(group);
|
||||||
} else {
|
} else {
|
||||||
// only pass input to exception message when it is not too long
|
// only pass input to exception message when it is not too long
|
||||||
if (input.length() > 1024) {
|
if (input.length() > 1024) {
|
||||||
throw new RegexException("failed to find pattern \"" + pat.pattern() + "\"");
|
throw new RegexException("failed to find pattern \"" + pat.pattern() + "\"");
|
||||||
} else {
|
} else {
|
||||||
throw new RegexException("failed to find pattern \"" + pat.pattern() + "\" inside of \"" + input + "\"");
|
throw new RegexException("failed to find pattern \"" + pat.pattern()
|
||||||
|
+ "\" inside of \"" + input + "\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isMatch(String pattern, String input) {
|
public static boolean isMatch(final String pattern, final String input) {
|
||||||
final Pattern pat = Pattern.compile(pattern);
|
final Pattern pat = Pattern.compile(pattern);
|
||||||
final Matcher mat = pat.matcher(input);
|
final Matcher mat = pat.matcher(input);
|
||||||
return mat.find();
|
return mat.find();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isMatch(Pattern pattern, String input) {
|
public static boolean isMatch(final Pattern pattern, final String input) {
|
||||||
final Matcher mat = pattern.matcher(input);
|
final Matcher mat = pattern.matcher(input);
|
||||||
return mat.find();
|
return mat.find();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, String> compatParseMap(final String input) throws UnsupportedEncodingException {
|
public static Map<String, String> compatParseMap(final String input)
|
||||||
Map<String, String> map = new HashMap<>();
|
throws UnsupportedEncodingException {
|
||||||
for (String arg : input.split("&")) {
|
final Map<String, String> map = new HashMap<>();
|
||||||
String[] splitArg = arg.split("=");
|
for (final String arg : input.split("&")) {
|
||||||
|
final String[] splitArg = arg.split("=");
|
||||||
if (splitArg.length > 1) {
|
if (splitArg.length > 1) {
|
||||||
map.put(splitArg[0], URLDecoder.decode(splitArg[1], UTF_8));
|
map.put(splitArg[0], URLDecoder.decode(splitArg[1], UTF_8));
|
||||||
} else {
|
} else {
|
||||||
|
@ -104,19 +110,19 @@ public class Parser {
|
||||||
|
|
||||||
public static String[] getLinksFromString(final String txt) throws ParsingException {
|
public static String[] getLinksFromString(final String txt) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
ArrayList<String> links = new ArrayList<>();
|
final ArrayList<String> links = new ArrayList<>();
|
||||||
LinkExtractor linkExtractor = LinkExtractor.builder()
|
final LinkExtractor linkExtractor = LinkExtractor.builder()
|
||||||
.linkTypes(EnumSet.of(LinkType.URL, LinkType.WWW))
|
.linkTypes(EnumSet.of(LinkType.URL, LinkType.WWW))
|
||||||
.build();
|
.build();
|
||||||
Iterable<LinkSpan> linkss = linkExtractor.extractLinks(txt);
|
final Iterable<LinkSpan> linkSpans = linkExtractor.extractLinks(txt);
|
||||||
for (LinkSpan ls : linkss) {
|
for (final LinkSpan ls : linkSpans) {
|
||||||
links.add(txt.substring(ls.getBeginIndex(), ls.getEndIndex()));
|
links.add(txt.substring(ls.getBeginIndex(), ls.getEndIndex()));
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] linksarray = new String[links.size()];
|
String[] linksarray = new String[links.size()];
|
||||||
linksarray = links.toArray(linksarray);
|
linksarray = links.toArray(linksarray);
|
||||||
return linksarray;
|
return linksarray;
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
throw new ParsingException("Could not get links from string", e);
|
throw new ParsingException("Could not get links from string", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.schabi.newpipe.extractor.utils;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class StringUtils {
|
public final class StringUtils {
|
||||||
|
|
||||||
private StringUtils() {
|
private StringUtils() {
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,8 @@ public class StringUtils {
|
||||||
* or parenthesis could not be matched .
|
* or parenthesis could not be matched .
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static String matchToClosingParenthesis(@Nonnull final String string, @Nonnull final String start) {
|
public static String matchToClosingParenthesis(@Nonnull final String string,
|
||||||
|
@Nonnull final String start) {
|
||||||
int startIndex = string.indexOf(start);
|
int startIndex = string.indexOf(start);
|
||||||
if (startIndex < 0) {
|
if (startIndex < 0) {
|
||||||
throw new IndexOutOfBoundsException();
|
throw new IndexOutOfBoundsException();
|
||||||
|
|
|
@ -6,10 +6,15 @@ import java.io.UnsupportedEncodingException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.util.*;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class Utils {
|
public final class Utils {
|
||||||
|
|
||||||
public static final String HTTP = "http://";
|
public static final String HTTP = "http://";
|
||||||
public static final String HTTPS = "https://";
|
public static final String HTTPS = "https://";
|
||||||
|
@ -46,18 +51,16 @@ public class Utils {
|
||||||
*
|
*
|
||||||
* @param numberWord string to be converted to a long
|
* @param numberWord string to be converted to a long
|
||||||
* @return a long
|
* @return a long
|
||||||
* @throws NumberFormatException
|
|
||||||
* @throws ParsingException
|
|
||||||
*/
|
*/
|
||||||
public static long mixedNumberWordToLong(final String numberWord) throws NumberFormatException,
|
public static long mixedNumberWordToLong(final String numberWord) throws NumberFormatException,
|
||||||
ParsingException {
|
ParsingException {
|
||||||
String multiplier = "";
|
String multiplier = "";
|
||||||
try {
|
try {
|
||||||
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2);
|
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2);
|
||||||
} catch (ParsingException ignored) {
|
} catch (final ParsingException ignored) {
|
||||||
}
|
}
|
||||||
double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord)
|
final double count = Double.parseDouble(
|
||||||
.replace(",", "."));
|
Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord).replace(",", "."));
|
||||||
switch (multiplier.toUpperCase()) {
|
switch (multiplier.toUpperCase()) {
|
||||||
case "K":
|
case "K":
|
||||||
return (long) (count * 1e3);
|
return (long) (count * 1e3);
|
||||||
|
@ -86,15 +89,10 @@ public class Utils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printErrors(List<Throwable> errors) {
|
|
||||||
for (Throwable e : errors) {
|
|
||||||
e.printStackTrace();
|
|
||||||
System.err.println("----------------");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String replaceHttpWithHttps(final String url) {
|
public static String replaceHttpWithHttps(final String url) {
|
||||||
if (url == null) return null;
|
if (url == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (!url.isEmpty() && url.startsWith(HTTP)) {
|
if (!url.isEmpty() && url.startsWith(HTTP)) {
|
||||||
return HTTPS + url.substring(HTTP.length());
|
return HTTPS + url.substring(HTTP.length());
|
||||||
|
@ -111,19 +109,17 @@ public class Utils {
|
||||||
* @return a string that contains the value of the query parameter or null if nothing was found
|
* @return a string that contains the value of the query parameter or null if nothing was found
|
||||||
*/
|
*/
|
||||||
public static String getQueryValue(final URL url, final String parameterName) {
|
public static String getQueryValue(final URL url, final String parameterName) {
|
||||||
String urlQuery = url.getQuery();
|
final String urlQuery = url.getQuery();
|
||||||
|
|
||||||
if (urlQuery != null) {
|
if (urlQuery != null) {
|
||||||
for (String param : urlQuery.split("&")) {
|
for (final String param : urlQuery.split("&")) {
|
||||||
String[] params = param.split("=", 2);
|
final String[] params = param.split("=", 2);
|
||||||
|
|
||||||
String query;
|
String query;
|
||||||
try {
|
try {
|
||||||
query = URLDecoder.decode(params[0], UTF_8);
|
query = URLDecoder.decode(params[0], UTF_8);
|
||||||
} catch (final UnsupportedEncodingException e) {
|
} catch (final UnsupportedEncodingException e) {
|
||||||
System.err.println(
|
// Cannot decode string with UTF-8, using the string without decoding
|
||||||
"Cannot decode string with UTF-8. using the string without decoding");
|
|
||||||
e.printStackTrace();
|
|
||||||
query = params[0];
|
query = params[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,9 +127,7 @@ public class Utils {
|
||||||
try {
|
try {
|
||||||
return URLDecoder.decode(params[1], UTF_8);
|
return URLDecoder.decode(params[1], UTF_8);
|
||||||
} catch (final UnsupportedEncodingException e) {
|
} catch (final UnsupportedEncodingException e) {
|
||||||
System.err.println(
|
// Cannot decode string with UTF-8, using the string without decoding
|
||||||
"Cannot decode string with UTF-8. using the string without decoding");
|
|
||||||
e.printStackTrace();
|
|
||||||
return params[1];
|
return params[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,7 +147,7 @@ public class Utils {
|
||||||
public static URL stringToURL(final String url) throws MalformedURLException {
|
public static URL stringToURL(final String url) throws MalformedURLException {
|
||||||
try {
|
try {
|
||||||
return new URL(url);
|
return new URL(url);
|
||||||
} catch (MalformedURLException e) {
|
} catch (final MalformedURLException e) {
|
||||||
// if no protocol is given try prepending "https://"
|
// if no protocol is given try prepending "https://"
|
||||||
if (e.getMessage().equals("no protocol: " + url)) {
|
if (e.getMessage().equals("no protocol: " + url)) {
|
||||||
return new URL(HTTPS + url);
|
return new URL(HTTPS + url);
|
||||||
|
@ -165,13 +159,13 @@ public class Utils {
|
||||||
|
|
||||||
public static boolean isHTTP(final URL url) {
|
public static boolean isHTTP(final URL url) {
|
||||||
// make sure its http or https
|
// make sure its http or https
|
||||||
String protocol = url.getProtocol();
|
final String protocol = url.getProtocol();
|
||||||
if (!protocol.equals("http") && !protocol.equals("https")) {
|
if (!protocol.equals("http") && !protocol.equals("https")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
|
final boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
|
||||||
boolean setsNoPort = url.getPort() == -1;
|
final boolean setsNoPort = url.getPort() == -1;
|
||||||
|
|
||||||
return setsNoPort || usesDefaultPort;
|
return setsNoPort || usesDefaultPort;
|
||||||
}
|
}
|
||||||
|
@ -186,14 +180,15 @@ public class Utils {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String removeUTF8BOM(String s) {
|
public static String removeUTF8BOM(final String s) {
|
||||||
if (s.startsWith("\uFEFF")) {
|
String result = s;
|
||||||
s = s.substring(1);
|
if (result.startsWith("\uFEFF")) {
|
||||||
|
result = result.substring(1);
|
||||||
}
|
}
|
||||||
if (s.endsWith("\uFEFF")) {
|
if (result.endsWith("\uFEFF")) {
|
||||||
s = s.substring(0, s.length() - 1);
|
result = result.substring(0, result.length() - 1);
|
||||||
}
|
}
|
||||||
return s;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getBaseUrl(final String url) throws ParsingException {
|
public static String getBaseUrl(final String url) throws ParsingException {
|
||||||
|
@ -243,7 +238,7 @@ public class Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
// can be used for JsonObjects
|
// can be used for JsonObjects
|
||||||
public static boolean isNullOrEmpty(final Map map) {
|
public static boolean isNullOrEmpty(final Map<?, ?> map) {
|
||||||
return map == null || map.isEmpty();
|
return map == null || map.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue