/** * Build the HTML anchor link to a topic page for a given WikLink object. * * @param context The servlet context for the link that is being created. * @param virtualWiki The virtual wiki for the link that is being created. * @param wikiLink The WikiLink object containing all relevant information about the link being * generated. * @param text The text to display as the link content. * @param style The CSS class to use with the anchor HTML tag. This value can be <code>null</code> * or empty if no custom style is used. * @param target The anchor link target, or <code>null</code> or empty if no target is needed. * @param escapeHtml Set to <code>true</code> if the link caption should be HTML escaped. This * value should be <code>true</code> in any case where the caption is not guaranteed to be * free from potentially malicious HTML code. * @return An HTML anchor link that matches the given input parameters. * @throws DataAccessException Thrown if any error occurs while retrieving topic information. */ public static String buildInternalLinkHtml( String context, String virtualWiki, WikiLink wikiLink, String text, String style, String target, boolean escapeHtml) throws DataAccessException { String url = LinkUtil.buildTopicUrl(context, virtualWiki, wikiLink); String topic = wikiLink.getDestination(); if (StringUtils.isBlank(text)) { text = topic; } if (!StringUtils.isBlank(topic) && StringUtils.isBlank(style)) { if (!StringUtils.isEmpty(virtualWiki) && InterWikiHandler.isInterWiki(virtualWiki)) { style = "interwiki"; } else if (!LinkUtil.isExistingArticle(virtualWiki, topic)) { style = "edit"; } } if (!StringUtils.isBlank(style)) { style = " class=\"" + style + "\""; } else { style = ""; } if (!StringUtils.isBlank(target)) { target = " target=\"" + target + "\""; } else { target = ""; } if (StringUtils.isBlank(topic) && !StringUtils.isBlank(wikiLink.getSection())) { topic = wikiLink.getSection(); } StringBuffer html = new StringBuffer(); html.append("<a href=\"").append(url).append('\"').append(style); html.append(" title=\"") .append(StringEscapeUtils.escapeHtml(topic)) .append('\"') .append(target) .append('>'); if (escapeHtml) { html.append(StringEscapeUtils.escapeHtml(text)); } else { html.append(text); } html.append("</a>"); return html.toString(); }
@Test public void testConstructor() throws Throwable { WikiLink wikiLink = new WikiLink("/wiki", "en", null); assertNull("wikiLink.getQuery()", wikiLink.getQuery()); assertNull("wikiLink.getSection()", wikiLink.getSection()); assertNull("wikiLink.getText()", wikiLink.getText()); assertNull("wikiLink.getArticle()", wikiLink.getArticle()); assertEquals( "wikiLink.getNamespace()", Namespace.namespace(Namespace.MAIN_ID), wikiLink.getNamespace()); assertNull("wikiLink.getDestination()", wikiLink.getDestination()); assertFalse("wikiLink.getColon()", wikiLink.getColon()); }
/** * Build a URL to the topic page for a given topic. * * @param context The servlet context path. If this value is <code>null</code> then the resulting * URL will NOT include context path, which breaks HTML links but is useful for servlet * redirection URLs. * @param virtualWiki The virtual wiki for the link that is being created. * @param wikiLink The WikiLink object containing all relevant information about the link being * generated. * @throws DataAccessException Thrown if any error occurs while retrieving topic information. */ public static String buildTopicUrl(String context, String virtualWiki, WikiLink wikiLink) throws DataAccessException { String topic = wikiLink.getDestination(); String section = wikiLink.getSection(); String query = wikiLink.getQuery(); String url = LinkUtil.buildTopicUrlNoEdit(context, virtualWiki, topic, section, query); if (StringUtils.isBlank(topic) && !StringUtils.isBlank(section)) { // do not check existence for section links return url; } if (!LinkUtil.isExistingArticle(virtualWiki, topic)) { url = LinkUtil.buildEditLinkUrl(context, virtualWiki, topic, query, -1); } return url; }
/** * Build a URL to the topic page for a given topic. * * @param context The servlet context path. If this value is <code>null</code> then the resulting * URL will NOT include context path, which breaks HTML links but is useful for servlet * redirection URLs. * @param virtualWiki The virtual wiki for the link that is being created. * @param topic The topic name for the URL that is being generated. * @param validateTopic Set to <code>true</code> if the topic must exist and must not be a * "Special:" page. If the topic does not exist then a link to an edit page will be returned. * @throws DataAccessException Thrown if any error occurs while retrieving topic information. */ public static String buildTopicUrl( String context, String virtualWiki, String topic, boolean validateTopic) throws DataAccessException { if (StringUtils.isBlank(topic)) { return null; } WikiLink wikiLink = LinkUtil.parseWikiLink(topic); if (validateTopic) { return LinkUtil.buildTopicUrl(context, virtualWiki, wikiLink); } else { return LinkUtil.buildTopicUrlNoEdit( context, virtualWiki, wikiLink.getDestination(), wikiLink.getSection(), wikiLink.getQuery()); } }
@Test public void testSetSection() throws Throwable { WikiLink wikiLink = new WikiLink("/wiki", "en", null); wikiLink.setSection("testWikiLinkSection"); assertEquals("wikiLink.getSection()", "testWikiLinkSection", wikiLink.getSection()); }