Example usage for org.jsoup.safety Whitelist relaxed

List of usage examples for org.jsoup.safety Whitelist relaxed

Introduction

In this page you can find the example usage for org.jsoup.safety Whitelist relaxed.

Prototype

public static Whitelist relaxed() 

Source Link

Document

This whitelist allows a full range of text and structural body HTML: a, b, blockquote, br, caption, cite, code, col, colgroup, dd, div, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, span, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, u, ul

Links do not have an enforced rel=nofollow attribute, but you can add that if desired.

Usage

From source file:com.lloydtorres.stately.helpers.SparkleHelper.java

/**
 * Transform NationStates' BBCode-formatted content into HTML
 * @param c App context//from w  ww.  j  av a2s  .  c  om
 * @param t TextView
 * @param content Target content
 * @param fm FragmentManager to show spoiler dialogs in
 */
public static void setBbCodeFormatting(Context c, TextView t, String content, FragmentManager fm) {
    if (content == null || content.length() < 0) {
        return;
    }

    String holder = content.trim();
    holder = holder.replace("\n", "<br>");
    holder = holder.replace("&amp;#39;", "'");
    holder = holder.replace("&amp;", "&");
    holder = Jsoup.clean(holder, Whitelist.simpleText().addTags("br"));

    // Replace raw NS nation and region links with Stately versions
    holder = linkifyHelper(c, t, holder, NS_RAW_NATION_LINK, ExploreActivity.EXPLORE_NATION);
    holder = linkifyHelper(c, t, holder, NS_RAW_REGION_LINK, ExploreActivity.EXPLORE_REGION);
    holder = linkifyHelper(c, t, holder, NS_RAW_REGION_LINK_TG, ExploreActivity.EXPLORE_REGION);
    holder = regexReplace(holder, NS_BBCODE_URL_NATION,
            "[url=" + ExploreActivity.EXPLORE_TARGET + "%s/" + ExploreActivity.EXPLORE_NATION + "]");
    holder = regexReplace(holder, NS_BBCODE_URL_REGION,
            "[url=" + ExploreActivity.EXPLORE_TARGET + "%s/" + ExploreActivity.EXPLORE_REGION + "]");

    // Basic BBcode processing
    holder = holder.replaceAll("(?i)\\[hr\\]", "<br>");

    // Process lists first (they're problematic!)
    TextProcessor processor = BBProcessorFactory.getInstance()
            .create(c.getResources().openRawResource(R.raw.bbcode));
    holder = processor.process(holder);
    holder = holder.replace("&lt;", "<");
    holder = holder.replace("&gt;", ">");
    holder = holder.replace("[*]", "<li>");
    holder = Jsoup.clean(holder, Whitelist.relaxed());

    // Q: Why don't you use the BBCode parser instead of doing this manually? :(
    // A: Because it misses some tags for some reason, so it's limited to lists for now.
    holder = regexReplace(holder, BBCODE_B, "<b>%s</b>");
    holder = regexReplace(holder, BBCODE_I, "<i>%s</i>");
    holder = regexReplace(holder, BBCODE_U, "<u>%s</u>");
    holder = regexReplace(holder, BBCODE_PRE, "<code>%s</code>");
    holder = regexDoubleReplace(holder, BBCODE_PROPOSAL, "<a href=\"" + Resolution.PATH_PROPOSAL + "\">%s</a>");
    holder = regexResolutionFormat(holder);
    holder = regexExtract(holder, BBCODE_RESOLUTION_GENERIC);
    holder = regexDoubleReplace(holder, BBCODE_COLOR, "<font color=\"%s\">%s</font>");
    holder = regexDoubleReplace(holder, BBCODE_INTERNAL_URL, "<a href=\"" + BASE_URI_NOSLASH + "/%s\">%s</a>");
    holder = regexGenericUrlFormat(c, holder);
    holder = regexQuoteFormat(c, t, holder);

    // Extract and replace spoilers
    List<Spoiler> spoilers = getSpoilerReplacePairs(c, holder);
    for (int i = 0; i < spoilers.size(); i++) {
        Spoiler s = spoilers.get(i);
        holder = holder.replace(s.raw, s.replacer);
    }

    // Linkify nations and regions
    holder = linkifyHelper(c, t, holder, NS_BBCODE_NATION, ExploreActivity.EXPLORE_NATION);
    holder = linkifyHelper(c, t, holder, NS_BBCODE_NATION_2, ExploreActivity.EXPLORE_NATION);
    holder = linkifyHelper(c, t, holder, NS_BBCODE_NATION_3, ExploreActivity.EXPLORE_NATION);
    holder = linkifyHelper(c, t, holder, NS_BBCODE_REGION, ExploreActivity.EXPLORE_REGION);
    holder = linkifyHelper(c, t, holder, NS_BBCODE_REGION_2, ExploreActivity.EXPLORE_REGION);

    // In case there are no nations or regions to linkify, set and style TextView here too
    setStyledTextView(c, t, holder, spoilers, fm);
}

From source file:net.triptech.buildulator.DataParser.java

/**
 * Clean the inpput HTML.//  w  w w.ja v  a 2 s .  c om
 *
 * @param htmlString the html string
 * @return the string
 */
public static String cleanHtml(final String htmlString) {
    String htmlOutput = "";

    if (StringUtils.isNotBlank(htmlString)) {
        try {
            htmlOutput = Jsoup.clean(htmlString, Whitelist.relaxed().addAttributes("table", "style", "class")
                    .addAttributes("th", "style", "class").addAttributes("td", "style", "class"));
        } catch (Exception e) {
            htmlOutput = "";
        }
    }
    return htmlOutput;
}

From source file:org.b3log.solo.service.DataModelService.java

/**
 * Fills post comments recently.//from  www.  java2  s  . c o m
 *
 * @param dataModel  data model
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillRecentComments(final Map<String, Object> dataModel, final JSONObject preference)
        throws ServiceException {
    Stopwatchs.start("Fill Recent Comments");
    try {
        LOGGER.debug("Filling recent comments....");
        final int recentCommentDisplayCnt = preference.getInt(Option.ID_C_RECENT_COMMENT_DISPLAY_CNT);
        final List<JSONObject> recentComments = commentRepository.getRecentComments(recentCommentDisplayCnt);
        for (final JSONObject comment : recentComments) {
            String commentContent = comment.optString(Comment.COMMENT_CONTENT);
            commentContent = Emotions.convert(commentContent);
            commentContent = Markdowns.toHTML(commentContent);
            commentContent = Jsoup.clean(commentContent, Whitelist.relaxed());
            comment.put(Comment.COMMENT_CONTENT, commentContent);
            comment.put(Comment.COMMENT_NAME, comment.getString(Comment.COMMENT_NAME));
            comment.put(Comment.COMMENT_URL, comment.getString(Comment.COMMENT_URL));
            comment.put(Common.IS_REPLY, false);
            comment.remove(Comment.COMMENT_EMAIL); // Erases email for security reason
            comment.put(Comment.COMMENT_T_DATE, new Date(comment.optLong(Comment.COMMENT_CREATED)));
            comment.put("commentDate2", new Date(comment.optLong(Comment.COMMENT_CREATED)));

            final String email = comment.optString(Comment.COMMENT_EMAIL);
            final String thumbnailURL = comment.optString(Comment.COMMENT_THUMBNAIL_URL);
            if (StringUtils.isBlank(thumbnailURL)) {
                comment.put(Comment.COMMENT_THUMBNAIL_URL, Solos.getGravatarURL(email, "128"));
            }
        }

        dataModel.put(Common.RECENT_COMMENTS, recentComments);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Fills recent comments failed", e);

        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.symphony.service.ArticleQueryService.java

/**
 * Markdowns the specified article content.
 *
 * <ul>/*from   w w w .  j a  va2  s  .c  o m*/
 * <li>Markdowns article content/reward content</li>
 * <li>Generates secured article content/reward content</li>
 * </ul>
 *
 * @param article the specified article content
 */
public void markdown(final JSONObject article) {
    String content = article.optString(Article.ARTICLE_CONTENT);

    final int articleType = article.optInt(Article.ARTICLE_TYPE);
    if (Article.ARTICLE_TYPE_C_THOUGHT != articleType) {
        content = Markdowns.toHTML(content);
        content = Markdowns.clean(content,
                Latkes.getServePath() + article.optString(Article.ARTICLE_PERMALINK));
    } else {
        final Document.OutputSettings outputSettings = new Document.OutputSettings();
        outputSettings.prettyPrint(false);

        content = Jsoup.clean(content, Latkes.getServePath() + article.optString(Article.ARTICLE_PERMALINK),
                Whitelist.relaxed().addAttributes(":all", "id", "target", "class").addTags("span", "hr")
                        .addAttributes("iframe", "src", "width", "height")
                        .addAttributes("audio", "controls", "src"),
                outputSettings);

        content = content.replace("\n", "\\n").replace("'", "\\'").replace("\"", "\\\"");
    }

    article.put(Article.ARTICLE_CONTENT, content);

    if (article.optInt(Article.ARTICLE_REWARD_POINT) > 0) {
        String rewardContent = article.optString(Article.ARTICLE_REWARD_CONTENT);
        rewardContent = Markdowns.toHTML(rewardContent);
        rewardContent = Markdowns.clean(rewardContent,
                Latkes.getServePath() + article.optString(Article.ARTICLE_PERMALINK));
        article.put(Article.ARTICLE_REWARD_CONTENT, rewardContent);
    }
}

From source file:org.b3log.symphony.util.Markdowns.java

/**
 * Gets the safe HTML content of the specified content.
 *
 * @param content the specified content/* w  w w .j  a va 2 s  . co m*/
 * @param baseURI the specified base URI, the relative path value of href will starts with this URL
 * @return safe HTML content
 */
public static String clean(final String content, final String baseURI) {
    final Document.OutputSettings outputSettings = new Document.OutputSettings();
    outputSettings.prettyPrint(false);

    final String tmp = Jsoup.clean(content, baseURI,
            Whitelist.relaxed().addAttributes(":all", "id", "target", "class")
                    .addTags("span", "hr", "kbd", "samp", "tt", "del", "s", "strike", "u")
                    .addAttributes("iframe", "src", "width", "height", "border", "marginwidth", "marginheight")
                    .addAttributes("audio", "controls", "src")
                    .addAttributes("video", "controls", "src", "width", "height")
                    .addAttributes("source", "src", "media", "type")
                    .addAttributes("object", "width", "height", "data", "type")
                    .addAttributes("param", "name", "value")
                    .addAttributes("input", "type", "disabled", "checked").addAttributes("embed", "src", "type",
                            "width", "height", "wmode", "allowNetworking"),
            outputSettings);
    final Document doc = Jsoup.parse(tmp, baseURI, Parser.htmlParser());

    final Elements ps = doc.getElementsByTag("p");
    for (final Element p : ps) {
        p.removeAttr("style");
    }

    final Elements iframes = doc.getElementsByTag("iframe");
    for (final Element iframe : iframes) {
        final String src = StringUtils.deleteWhitespace(iframe.attr("src"));
        if (StringUtils.startsWithIgnoreCase(src, "javascript")
                || StringUtils.startsWithIgnoreCase(src, "data:")) {
            iframe.remove();
        }
    }

    final Elements objs = doc.getElementsByTag("object");
    for (final Element obj : objs) {
        final String data = StringUtils.deleteWhitespace(obj.attr("data"));
        if (StringUtils.startsWithIgnoreCase(data, "data:")
                || StringUtils.startsWithIgnoreCase(data, "javascript")) {
            obj.remove();

            continue;
        }

        final String type = StringUtils.deleteWhitespace(obj.attr("type"));
        if (StringUtils.containsIgnoreCase(type, "script")) {
            obj.remove();
        }
    }

    final Elements embeds = doc.getElementsByTag("embed");
    for (final Element embed : embeds) {
        final String data = StringUtils.deleteWhitespace(embed.attr("src"));
        if (StringUtils.startsWithIgnoreCase(data, "data:")
                || StringUtils.startsWithIgnoreCase(data, "javascript")) {
            embed.remove();

            continue;
        }
    }

    final Elements as = doc.getElementsByTag("a");
    for (final Element a : as) {
        a.attr("rel", "nofollow");

        final String href = a.attr("href");
        if (href.startsWith(Latkes.getServePath())) {
            continue;
        }

        a.attr("target", "_blank");
    }

    final Elements audios = doc.getElementsByTag("audio");
    for (final Element audio : audios) {
        audio.attr("preload", "none");
    }

    final Elements videos = doc.getElementsByTag("video");
    for (final Element video : videos) {
        video.attr("preload", "none");
    }

    String ret = doc.body().html();
    ret = ret.replaceAll("(</?br\\s*/?>\\s*)+", "<br>"); // patch for Jsoup issue

    return ret;
}

From source file:org.vaadin.maddon.label.RichText.java

public Whitelist getWhitelist() {
    if (whitelist == null) {
        return Whitelist.relaxed();
    }
    return whitelist;
}