Esempio n. 1
0
  /**
   * Utility method for building an anchor tag that links to an image page and includes the HTML
   * image tag to display the image.
   *
   * @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 topicName The name of the image for which a link is being created.
   * @param frame Set to <code>true</code> if the image should display with a frame border.
   * @param thumb Set to <code>true</code> if the image should display as a thumbnail.
   * @param align Indicates how the image should horizontally align on the page. Valid values are
   *     "left", "right" and "center".
   * @param caption An optional text caption to display for the image. If no caption is used then
   *     this value should be either empty or <code>null</code>.
   * @param maxDimension A value in pixels indicating the maximum width or height value allowed for
   *     the image. Images will be resized so that neither the width or height exceeds this value.
   * @param suppressLink If this value is <code>true</code> then the generated HTML will include the
   *     image tag without a link to the image topic page.
   * @param style The CSS class to use with the img HTML tag. This value can be <code>null</code> or
   *     empty if no custom style is used.
   * @param escapeHtml Set to <code>true</code> if the 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 The full HTML required to display an image enclosed within an HTML anchor tag that
   *     links to the image topic page.
   * @throws DataAccessException Thrown if any error occurs while retrieving image information.
   * @throws IOException Thrown if any error occurs while reading image information.
   */
  public static String buildImageLinkHtml(
      String context,
      String virtualWiki,
      String topicName,
      boolean frame,
      boolean thumb,
      String align,
      String caption,
      int maxDimension,
      boolean suppressLink,
      String style,
      boolean escapeHtml)
      throws DataAccessException, IOException {
    // TODO check this for GAE/J
    String url = LinkUtil.buildImageFileUrl(context, virtualWiki, topicName);
    if (url == null) {
      WikiLink uploadLink = LinkUtil.parseWikiLink("Special:Upload");
      return LinkUtil.buildInternalLinkHtml(
          context, virtualWiki, uploadLink, topicName, "edit", null, true);
    }
    // WikiFile wikiFile = WikiBase.getDataHandler().lookupWikiFile(virtualWiki,
    // topicName);
    Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null);
    StringBuffer html = new StringBuffer();
    // if (topic.getTopicType() == Topic.TYPE_FILE) {
    // // file, not an image
    // if (StringUtils.isBlank(caption)) {
    // caption = topicName.substring(NamespaceHandler.NAMESPACE_IMAGE.length() +
    // 1);
    // }
    // html.append("<a href=\"").append(url).append("\">");
    // if (escapeHtml) {
    // html.append(StringEscapeUtils.escapeHtml(caption));
    // } else {
    // html.append(caption);
    // }
    // html.append("</a>");
    // return html.toString();
    // }

    return "<span class=\"error\">LinkUtil.buildImageLinkHtml(>) not supported</span>";
  }
Esempio n. 2
0
 /** Parse a Mediawiki link of the form "[[topic|text]]" and return the resulting HTML output. */
 public String parse(JFlexLexer lexer, String raw, Object... args) throws ParserException {
   boolean containsNestedLinks =
       (args.length > 0 && StringUtils.equals(args[0].toString(), "nested"));
   WikiLink wikiLink = JFlexParserUtil.parseWikiLink(lexer.getParserInput(), raw);
   if (StringUtils.isBlank(wikiLink.getDestination())
       && StringUtils.isBlank(wikiLink.getSection())) {
     // no destination or section
     return raw;
   }
   if (containsNestedLinks) {
     // if there is a nested link it must be an image, otherwise the syntax is invalid.
     if (wikiLink.getColon() || !wikiLink.getNamespace().getId().equals(Namespace.FILE_ID)) {
       int start = raw.indexOf("[[");
       int end = raw.lastIndexOf("]]");
       String content = raw.substring(start + "[[".length(), end);
       return "[["
           + JFlexParserUtil.parseFragment(lexer.getParserInput(), content, lexer.getMode())
           + "]]";
     }
   }
   raw =
       this.processLinkMetadata(
           lexer.getParserInput(), lexer.getParserOutput(), lexer.getMode(), raw, wikiLink);
   if (lexer.getMode() <= JFlexParser.MODE_PREPROCESS) {
     // do not parse to HTML when in preprocess mode
     return raw;
   }
   if (!wikiLink.getColon() && wikiLink.getNamespace().getId().equals(Namespace.FILE_ID)) {
     // parse as an image
     return lexer.parse(JFlexLexer.TAG_TYPE_IMAGE_LINK, raw);
   }
   try {
     if (!StringUtils.isBlank(wikiLink.getInterWiki())) {
       // inter-wiki link
       return LinkUtil.interWiki(wikiLink);
     }
     String virtualWiki = lexer.getParserInput().getVirtualWiki();
     if (wikiLink.getVirtualWiki() != null) {
       // link to another virtual wiki
       virtualWiki = wikiLink.getVirtualWiki().getName();
     }
     if (StringUtils.isBlank(wikiLink.getText())
         && !StringUtils.isBlank(wikiLink.getDestination())) {
       wikiLink.setText(wikiLink.getDestination());
       if (!StringUtils.isBlank(wikiLink.getSection())) {
         wikiLink.setText(
             wikiLink.getText()
                 + "#"
                 + Utilities.decodeAndEscapeTopicName(wikiLink.getSection(), true));
       }
     } else if (StringUtils.isBlank(wikiLink.getText())
         && !StringUtils.isBlank(wikiLink.getSection())) {
       wikiLink.setText(Utilities.decodeAndEscapeTopicName("#" + wikiLink.getSection(), true));
     } else {
       // pass a parameter via the parserInput to prevent nested links from being generated
       lexer.getParserInput().getTempParams().put(LINK_CAPTION, true);
       wikiLink.setText(
           JFlexParserUtil.parseFragment(
               lexer.getParserInput(), wikiLink.getText(), lexer.getMode()));
       lexer.getParserInput().getTempParams().remove(LINK_CAPTION);
     }
     if (StringUtils.equals(wikiLink.getDestination(), lexer.getParserInput().getTopicName())
         && StringUtils.equals(virtualWiki, lexer.getParserInput().getVirtualWiki())
         && StringUtils.isBlank(wikiLink.getSection())) {
       // same page, bold the text and return
       return "<b>"
           + (StringUtils.isBlank(wikiLink.getText())
               ? wikiLink.getDestination()
               : wikiLink.getText())
           + "</b>";
     }
     // do not escape text html - already done by parser
     return LinkUtil.buildInternalLinkHtml(
         lexer.getParserInput().getContext(),
         virtualWiki,
         wikiLink,
         wikiLink.getText(),
         null,
         null,
         false);
   } catch (DataAccessException e) {
     logger.severe("Failure while parsing link " + raw, e);
     return "";
   } catch (ParserException e) {
     logger.severe("Failure while parsing link " + raw, e);
     return "";
   }
 }