From 94dce083f1687acbf57e3e5a5a5e94c52f2ae90d Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sun, 2 Apr 2023 14:51:21 +0800 Subject: [PATCH] Convert ForumsParser to kotlin --- .../ehviewer/client/parser/ForumsParser.kt | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ForumsParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/ForumsParser.kt index b8b5d3531..1367d17c9 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ForumsParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ForumsParser.kt @@ -13,28 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.client.parser -package com.hippo.ehviewer.client.parser; +import com.hippo.ehviewer.client.EhUrl +import com.hippo.ehviewer.client.exception.EhException +import com.hippo.util.ExceptionUtils +import org.jsoup.Jsoup -import com.hippo.ehviewer.client.EhUrl; -import com.hippo.ehviewer.client.exception.EhException; -import com.hippo.util.ExceptionUtils; - -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; - -public class ForumsParser { - - public static String parse(String body) throws EhException { - try { - Document d = Jsoup.parse(body, EhUrl.URL_FORUMS); - Element userlinks = d.getElementById("userlinks"); - Element child = userlinks.child(0).child(0).child(0); - return child.attr("href"); - } catch (Throwable e) { - ExceptionUtils.throwIfFatal(e); - throw new EhException("Not logged in"); +object ForumsParser { + fun parse(body: String): String { + return runCatching { + val d = Jsoup.parse(body, EhUrl.URL_FORUMS) + val userlinks = d.getElementById("userlinks") + val child = userlinks!!.child(0).child(0).child(0) + child.attr("href") + }.getOrElse { + ExceptionUtils.throwIfFatal(it) + throw EhException("Not logged in") } } }