/** * Parse a wiki topic link and return a <code>WikiLink</code> object representing the link. Wiki * topic links are of the form "Topic?Query#Section". * * @param raw The raw topic link text. * @return A WikiLink object that represents the link. */ public static WikiLink parseWikiLink(String raw) { // note that this functionality was previously handled with a regular // expression, but the expression caused CPU usage to spike to 100% // with topics such as "Urnordisch oder Nordwestgermanisch?" String processed = raw.trim(); WikiLink wikiLink = new WikiLink(); if (StringUtils.isBlank(processed)) { return new WikiLink(); } // first look for a section param - "#..." int sectionPos = processed.indexOf('#'); if (sectionPos != -1 && sectionPos < processed.length()) { String sectionString = processed.substring(sectionPos + 1); wikiLink.setSection(sectionString); if (sectionPos == 0) { // link is of the form #section, no more to process return wikiLink; } processed = processed.substring(0, sectionPos); } // now see if the link ends with a query param - "?..." int queryPos = processed.indexOf('?', 1); if (queryPos != -1 && queryPos < processed.length()) { String queryString = processed.substring(queryPos + 1); wikiLink.setQuery(queryString); processed = processed.substring(0, queryPos); } // since we're having so much fun, let's find a namespace (default empty). String namespaceString = ""; int namespacePos = processed.indexOf(':', 1); if (namespacePos != -1 && namespacePos < processed.length()) { namespaceString = processed.substring(0, namespacePos); } wikiLink.setNamespace(namespaceString); String topic = processed; if (namespacePos > 0 && (namespacePos + 1) < processed.length()) { // get namespace, unless topic ends with a colon topic = processed.substring(namespacePos + 1); } wikiLink.setArticle(Utilities.decodeTopicName(topic, true)); // destination is namespace + topic wikiLink.setDestination(Utilities.decodeTopicName(processed, true)); return wikiLink; }
@Test public void testSetSection() throws Throwable { WikiLink wikiLink = new WikiLink("/wiki", "en", null); wikiLink.setSection("testWikiLinkSection"); assertEquals("wikiLink.getSection()", "testWikiLinkSection", wikiLink.getSection()); }