Safe number string parsing
This commit is contained in:
1 parent
a0b786b2a6
commit
a33bd63e6e
7 files changed
+65
-27
No files matched your search
@@ -56,7 +56,7 @@ public class FavoritesParser {
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Element fp = fps.get(i);
|
||||
countArray[i] = ParserUtils.parseInt(fp.child(0).text());
|
||||
countArray[i] = ParserUtils.parseInt(fp.child(0).text(), 0);
|
||||
catArray[i] = ParserUtils.trim(fp.child(2).text());
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.hippo.ehviewer.client.parser;
|
||||
import com.hippo.ehviewer.client.EhUtils;
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo;
|
||||
|
||||
import com.hippo.yorozuya.NumberUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@@ -43,8 +44,8 @@ public class GalleryApiParser {
|
||||
gi.category = EhUtils.getCategory(g.getString("category"));
|
||||
gi.thumb = EhUtils.handleThumbUrlResolution(g.getString("thumb"));
|
||||
gi.uploader = g.getString("uploader");
|
||||
gi.posted = ParserUtils.formatDate(ParserUtils.parseLong(g.getString("posted")) * 1000);
|
||||
gi.rating = Float.parseFloat(g.getString("rating"));
|
||||
gi.posted = ParserUtils.formatDate(ParserUtils.parseLong(g.getString("posted"), 0) * 1000);
|
||||
gi.rating = NumberUtils.parseFloatSafely(g.getString("rating"), 0.0f);
|
||||
// tags
|
||||
JSONArray tagJa = g.getJSONArray("tags");
|
||||
int tagLength = tagJa.length();
|
||||
@@ -53,7 +54,7 @@ public class GalleryApiParser {
|
||||
tags[j] = tagJa.getString(j);
|
||||
}
|
||||
gi.simpleTags = tags;
|
||||
gi.pages = Integer.parseInt(g.getString("filecount"));
|
||||
gi.pages = NumberUtils.parseIntSafely(g.getString("filecount"), 0);
|
||||
gi.generateSLang();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,13 +109,16 @@ public class GalleryDetailParser {
|
||||
private static void parseDetail(GalleryDetail gd, Document d, String body) throws ParseException {
|
||||
Matcher matcher = PATTERN_DETAIL.matcher(body);
|
||||
if (matcher.find()) {
|
||||
gd.gid = Long.parseLong(matcher.group(1));
|
||||
gd.gid = NumberUtils.parseLongSafely(matcher.group(1), -1L);
|
||||
gd.token = matcher.group(2);
|
||||
gd.apiUid = NumberUtils.parseLongSafely(matcher.group(3), -1L);
|
||||
gd.apiKey = matcher.group(4);
|
||||
} else {
|
||||
throw new ParseException("Can't parse gallery detail", body);
|
||||
}
|
||||
if (gd.gid == -1L) {
|
||||
throw new ParseException("Can't parse gallery detail", body);
|
||||
}
|
||||
|
||||
matcher = PATTERN_TORRENT.matcher(body);
|
||||
if (matcher.find()) {
|
||||
@@ -494,7 +497,7 @@ public class GalleryDetailParser {
|
||||
Matcher m = PATTERN_PREVIEW_PAGES.matcher(body);
|
||||
int previewPages = -1;
|
||||
if (m.find()) {
|
||||
previewPages = ParserUtils.parseInt(m.group(1));
|
||||
previewPages = ParserUtils.parseInt(m.group(1), -1);
|
||||
}
|
||||
|
||||
if (previewPages <= 0) {
|
||||
@@ -508,12 +511,18 @@ public class GalleryDetailParser {
|
||||
* Parse pages with regular expressions
|
||||
*/
|
||||
public static int parsePages(String body) throws ParseException {
|
||||
int pages = -1;
|
||||
|
||||
Matcher m = PATTERN_PAGES.matcher(body);
|
||||
if (m.find()) {
|
||||
return ParserUtils.parseInt(m.group(1));
|
||||
} else {
|
||||
pages = ParserUtils.parseInt(m.group(1), -1);
|
||||
}
|
||||
|
||||
if (pages < 0) {
|
||||
throw new ParseException("Parse pages error", body);
|
||||
}
|
||||
|
||||
return pages;
|
||||
}
|
||||
|
||||
public static PreviewSet parsePreviewSet(Document d, String body) throws ParseException {
|
||||
@@ -571,7 +580,10 @@ public class GalleryDetailParser {
|
||||
LargePreviewSet largePreviewSet = new LargePreviewSet();
|
||||
|
||||
while (m.find()) {
|
||||
int index = ParserUtils.parseInt(m.group(2)) - 1;
|
||||
int index = ParserUtils.parseInt(m.group(2), 0) - 1;
|
||||
if (index < 0) {
|
||||
continue;
|
||||
}
|
||||
String imageUrl = ParserUtils.trim(m.group(3));
|
||||
String pageUrl = ParserUtils.trim(m.group(1));
|
||||
if (Settings.getFixThumbUrl()) {
|
||||
@@ -594,10 +606,23 @@ public class GalleryDetailParser {
|
||||
Matcher m = PATTERN_NORMAL_PREVIEW.matcher(body);
|
||||
NormalPreviewSet normalPreviewSet = new NormalPreviewSet();
|
||||
while (m.find()) {
|
||||
normalPreviewSet.addItem(ParserUtils.parseInt(m.group(6)) - 1,
|
||||
ParserUtils.trim(m.group(3)), ParserUtils.parseInt((m.group(4))), 0,
|
||||
ParserUtils.parseInt(m.group(1)), ParserUtils.parseInt(m.group(2)),
|
||||
ParserUtils.trim(m.group(5)));
|
||||
int position = ParserUtils.parseInt(m.group(6), 0) - 1;
|
||||
if (position < 0) {
|
||||
continue;
|
||||
}
|
||||
String imageUrl = ParserUtils.trim(m.group(3));
|
||||
int xOffset = ParserUtils.parseInt(m.group(4), 0);
|
||||
int yOffset = 0;
|
||||
int width = ParserUtils.parseInt(m.group(1), 0);
|
||||
if (width <= 0) {
|
||||
continue;
|
||||
}
|
||||
int height = ParserUtils.parseInt(m.group(2), 0);
|
||||
if (height <= 0) {
|
||||
continue;
|
||||
}
|
||||
String pageUrl = ParserUtils.trim(m.group(5));
|
||||
normalPreviewSet.addItem(position, imageUrl, xOffset, yOffset, width, height, pageUrl);
|
||||
}
|
||||
|
||||
if (normalPreviewSet.size() == 0) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import android.support.annotation.Nullable;
|
||||
|
||||
import com.hippo.ehviewer.client.EhUrl;
|
||||
|
||||
import com.hippo.yorozuya.NumberUtils;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -50,8 +51,11 @@ public final class GalleryDetailUrlParser {
|
||||
Matcher m = pattern.matcher(url);
|
||||
if (m.find()) {
|
||||
Result result = new Result();
|
||||
result.gid = Long.parseLong(m.group(1));
|
||||
result.gid = NumberUtils.parseLongSafely(m.group(1), -1L);
|
||||
result.token = m.group(2);
|
||||
if (result.gid < 0) {
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
|
||||
@@ -60,18 +60,17 @@ public class GalleryListParser {
|
||||
|
||||
private static String parseRating(String ratingStyle) {
|
||||
Matcher m = PATTERN_RATING.matcher(ratingStyle);
|
||||
int num1;
|
||||
int num2;
|
||||
int num1 = Integer.MIN_VALUE;
|
||||
int num2 = Integer.MIN_VALUE;
|
||||
int rate = 5;
|
||||
String re;
|
||||
if (m.find()) {
|
||||
num1 = ParserUtils.parseInt(m.group().replace("px", ""));
|
||||
} else {
|
||||
return null;
|
||||
num1 = ParserUtils.parseInt(m.group().replace("px", ""), Integer.MIN_VALUE);
|
||||
}
|
||||
if (m.find()) {
|
||||
num2 = ParserUtils.parseInt(m.group().replace("px", ""));
|
||||
} else {
|
||||
num2 = ParserUtils.parseInt(m.group().replace("px", ""), Integer.MIN_VALUE);
|
||||
}
|
||||
if (num1 == Integer.MIN_VALUE || num2 == Integer.MIN_VALUE) {
|
||||
return null;
|
||||
}
|
||||
rate = rate - num1 / 16;
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.hippo.ehviewer.client.parser;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
import com.hippo.ehviewer.client.EhUrl;
|
||||
import com.hippo.yorozuya.NumberUtils;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -47,9 +48,12 @@ public final class GalleryPageUrlParser {
|
||||
Matcher m = pattern.matcher(url);
|
||||
if (m.find()) {
|
||||
Result result = new Result();
|
||||
result.gid = Long.parseLong(m.group(2));
|
||||
result.gid = NumberUtils.parseLongSafely(m.group(2), -1L);
|
||||
result.pToken = m.group(1);
|
||||
result.page = Integer.parseInt(m.group(3)) - 1;
|
||||
result.page = NumberUtils.parseIntSafely(m.group(3), 0) - 1;
|
||||
if (result.gid < 0 || result.page < 0) {
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.hippo.ehviewer.client.parser;
|
||||
|
||||
import com.hippo.yorozuya.NumberUtils;
|
||||
import com.hippo.yorozuya.StringUtils;
|
||||
|
||||
import java.text.DateFormat;
|
||||
@@ -39,11 +40,15 @@ public class ParserUtils {
|
||||
return StringUtils.unescapeXml(str).trim();
|
||||
}
|
||||
|
||||
public static int parseInt(String str) {
|
||||
return Integer.parseInt(trim(str).replace(",", ""));
|
||||
public static int parseInt(String str, int defValue) {
|
||||
return NumberUtils.parseIntSafely(trim(str).replace(",", ""), defValue);
|
||||
}
|
||||
|
||||
public static long parseLong(String str) {
|
||||
return Long.parseLong(trim(str).replace(",", ""));
|
||||
public static long parseLong(String str, long defValue) {
|
||||
return NumberUtils.parseLongSafely(trim(str).replace(",", ""), defValue);
|
||||
}
|
||||
|
||||
public static float parseFloat(String str, float defValue) {
|
||||
return NumberUtils.parseFloatSafely(trim(str).replace(",", ""), defValue);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user