public MDLink parseRewrittenUrl(String url) { Timber.d("Parsing rewritten topic URL : %s", url); // Split url and anchor String[] urlParts = url.split("#"); url = urlParts[0]; String anchor = urlParts.length >= 2 ? urlParts[1] : null; Matcher rewrittenTopicMatcher = rewrittenTopicPattern.matcher(url); if (rewrittenTopicMatcher.matches()) { boolean hasSubCat = rewrittenTopicMatcher.groupCount() == 4; int subcatOffset = hasSubCat ? 1 : 0; String categorySlug = rewrittenTopicMatcher.group(1); String subcategorySlug = hasSubCat ? rewrittenTopicMatcher.group(2) : null; int topicId = Integer.parseInt(rewrittenTopicMatcher.group(subcatOffset + 2)); int pageNumber = Integer.parseInt(rewrittenTopicMatcher.group(subcatOffset + 3)); Timber.d( "Rewritten topic URL : %s, category : %s, subcategory : %s, topicId : %d, pageNumber : %d", url, categorySlug, subcategorySlug, topicId, pageNumber); Category topicCategory = categoriesStore.getCategoryBySlug(categorySlug); if (topicCategory == null) { Timber.e("Category '%s' is unknown", categorySlug); return MDLink.invalid(); } else { Timber.d("Link is for category '%s'", topicCategory.name()); return MDLink.forTopic(topicCategory, topicId) .atPage(pageNumber) .atPost(parseAnchor(anchor)) .build(); } } else { return MDLink.invalid(); } }
public MDLink parseStandardUrl(String url) { Timber.d("Parsing standard topic URL : %s", url); Uri parsedUri = Uri.parse(url); String categoryId = parsedUri.getQueryParameter("cat"); String pageNumber = parsedUri.getQueryParameter("page"); String topicId = parsedUri.getQueryParameter("post"); String anchor = parsedUri.getFragment(); // Set default page number as 1 (first page) if (pageNumber == null) { pageNumber = "1"; } if (categoryId == null || topicId == null) { Timber.e("URL '%s' is invalid, category, or topic id not found", url); return MDLink.invalid(); } try { Category topicCategory = categoriesStore.getCategoryById(Integer.parseInt(categoryId)); if (topicCategory == null) { Timber.e("Category with id '%s' is unknown", categoryId); return MDLink.invalid(); } else { Timber.d("Link is for category '%s'", topicCategory.name()); return MDLink.forTopic(topicCategory, Integer.parseInt(topicId)) .atPage(Integer.parseInt(pageNumber)) .atPost(parseAnchor(anchor)) .build(); } } catch (NumberFormatException e) { Timber.e(e, "Error while parsing standard URL"); return MDLink.invalid(); } }