示例#1
0
 /**
  * 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();
 }
示例#2
0
 /**
  * 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;
 }
示例#3
0
  @Override
  public void appendInternalLink(
      String topic,
      String hashSection,
      String topicDescription,
      String cssClass,
      boolean parseRecursive) {
    try {
      String virtualWiki = fParserInput.getVirtualWiki();
      WikiLink wikiLink;
      if (hashSection != null) {
        wikiLink = LinkUtil.parseWikiLink(virtualWiki, topic + "#" + hashSection);
      } else {
        wikiLink = LinkUtil.parseWikiLink(virtualWiki, topic);
      }
      String destination = wikiLink.getDestination();
      String section = wikiLink.getSection();
      String query = wikiLink.getQuery();
      String href = buildTopicUrlNoEdit(fContextPath, virtualWiki, destination, section, query);
      String style = "";
      if (StringUtils.isBlank(topic) && !StringUtils.isBlank(section)) {
        // do not check existence for section links
      } else {
        String articleName = topic.replace('_', ' ');
        if (LinkUtil.isExistingArticle(virtualWiki, articleName) == null) {
          style = "edit";
          href = LinkUtil.buildEditLinkUrl(fContextPath, virtualWiki, topic, query, -1);
        }
      }
      WPATag aTagNode = new WPATag();
      aTagNode.addAttribute("href", href, true);
      aTagNode.addAttribute("class", style, true);
      aTagNode.addObjectAttribute("wikilink", topic);

      pushNode(aTagNode);
      if (parseRecursive) {
        WikipediaParser.parseRecursive(topicDescription.trim(), this, false, true);
      } else {
        aTagNode.addChild(new ContentToken(topicDescription));
      }
      popNode();
    } catch (DataAccessException e1) {
      e1.printStackTrace();
      append(new ContentToken(topicDescription));
    }
  }