GalleryPageUrlParser supports non-strict url

This commit is contained in:
seven332 committed 2018-12-04 21:27:37 +08:00
1 parent 54f1c4806f
commit 7104fddded
2 files changed
+85 -4

No files matched your search

@@ -16,8 +16,8 @@
package com.hippo.ehviewer.client.parser;
import android.support.annotation.Nullable;
import com.hippo.ehviewer.client.EhUrl;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -26,15 +26,25 @@ import java.util.regex.Pattern;
*/
public final class GalleryPageUrlParser {
public static final Pattern URL_PATTERN = Pattern.compile("https?://(?:" +
EhUrl.DOMAIN_EX + "|" + EhUrl.DOMAIN_E + "|" + EhUrl.DOMAIN_LOFI + ")/s/(\\w+)/(\\d+)-(\\d+)");
private static final Pattern URL_STRICT_PATTERN = Pattern.compile(
"https?://(?:" + EhUrl.DOMAIN_EX + "|" + EhUrl.DOMAIN_E + "|" + EhUrl.DOMAIN_LOFI + ")/s/([0-9a-f]{10})/(\\d+)-(\\d+)");
private static final Pattern URL_PATTERN = Pattern.compile(
"([0-9a-f]{10})/(\\d+)-(\\d+)");
@Nullable
public static Result parse(String url) {
return parse(url, true);
}
@Nullable
public static Result parse(String url, boolean strict) {
if (url == null) {
return null;
}
Matcher m = URL_PATTERN.matcher(url);
Pattern pattern = strict ? URL_STRICT_PATTERN : URL_PATTERN;
Matcher m = pattern.matcher(url);
if (m.find()) {
Result result = new Result();
result.gid = Long.parseLong(m.group(2));
@@ -0,0 +1,71 @@
/*
* Copyright 2018 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.client.parser;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class GalleryPageUrlParserTest {
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "https://e-hentai.org/s/7b87643838/530350-1", true, false, 530350, "7b87643838", 0},
{ "https://exhentai.org/s/7b87643838/530350-1", true, false, 530350, "7b87643838", 0},
{ "7b87643838/530350-1", true, true, 0, null, 0},
{ "7b87643838/530350-1", false, false, 530350, "7b87643838", 0},
{ "s/7b87643838/530350-1", false, false, 530350, "7b87643838", 0},
{ "7b87643838/530350a-1", false, true, 0, null, 0},
});
}
private String url;
private boolean strict;
private boolean isNull;
private long gid;
private String pToken;
private int page;
public GalleryPageUrlParserTest(String url, boolean strict, boolean isNull, long gid, String pToken, int page) {
this.url = url;
this.strict = strict;
this.isNull = isNull;
this.gid = gid;
this.pToken = pToken;
this.page = page;
}
@Test
public void testParse() {
GalleryPageUrlParser.Result result = GalleryPageUrlParser.parse(url, strict);
if (isNull) {
assertNull(result);
} else {
assertEquals(gid, result.gid);
assertEquals(pToken, result.pToken);
assertEquals(page, result.page);
}
}
}