替换String中的字符,但避免使用允许的HTML标记

时间:2014-11-12 16:31:26

标签: java

我有一个可以包含HTML Code的字符串,如下所示:

String description = "<strong> HI </strong> all, <br /> here some other special chars <  >  \" and Html tag which is not allowed <b> </b>;

我有一个允许的HTML值数组,需要保存在这个字符串中:

String[] allowedValues = {"<br />", "<strong>", "</strong>", "<u>", "</u>",
                                "<i>", "</i>", "<li>", "</li>", "<ul>", "</ul>", 
                                "&lt;", "&gt;", "&amp;", "&qout;" ,"&apos;"};

必须以这种方式替换所有其他HTML标记或特殊字符:

StringUtils.replaceEach(str, new String[]{"&", "\"", "<", ">"}, new String[]{"&amp;", "&quot;", "&lt;", "&gt;"})

但不得更改allowedValues

如何避免更改allowedValues

2 个答案:

答案 0 :(得分:0)

感谢@SMT,JSoup库是我需要的。我的解决方案看起来像这样

我为不同的用例创建了一个枚举,因为一些HTMl字符串有其他允许的标记。

import org.jsoup.safety.Whitelist;

public enum StringModifier {

    DETAIL {
        @Override
        public Whitelist whitelist() {
                return new Whitelist()
                    .addTags("strong", "i", "li", "ul", "br", "u", "b", "a")
                    .addAttributes("a", "href");
        }
    },

    TEASER {
        @Override
        public Whitelist whitelist() {
                return new Whitelist()
                    .addTags();
        }
    },

    INCLUSIVE {
        @Override
        public Whitelist whitelist() {
                return new Whitelist()
                    .addTags("strong", "i", "li", "ul", "br", "u", "b");
        }
    },

    DOCINFO {
        @Override
        public Whitelist whitelist() {
                return new Whitelist()
                    .addTags("br", "li", "ul");
        }
    },

    INTERNAL {
        @Override
        public Whitelist whitelist() {
                return new Whitelist()
                .addTags(
                        "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")

                .addAttributes("a", "href", "title")
                .addAttributes("blockquote", "cite")
                .addAttributes("col", "span", "width")
                .addAttributes("colgroup", "span", "width")
                .addAttributes("img", "align", "alt", "height", "src", "title", "width")
                .addAttributes("ol", "start", "type")
                .addAttributes("q", "cite")
                .addAttributes("table", "summary", "width")
                .addAttributes("td", "abbr", "axis", "colspan", "rowspan", "width")
                .addAttributes(
                        "th", "abbr", "axis", "colspan", "rowspan", "scope",
                        "width")
                .addAttributes("ul", "type")

                .addProtocols("a", "href", "ftp", "http", "https", "mailto")
                .addProtocols("blockquote", "cite", "http", "https")
                .addProtocols("cite", "cite", "http", "https")
                .addProtocols("img", "src", "http", "https")
                .addProtocols("q", "cite", "http", "https")
                ;
        }
    },

    OTHER {
        @Override
        public Whitelist whitelist() {
                return new Whitelist()
                    .addTags();
        }
    };

    public abstract Whitelist whitelist();

}

在对字符串进行消毒后,我会根据需要更换一些标签。

public class ModifyString {

    public static void main(String[] args) {
        modifyStringValue("", "DETAIL", "<b>Day 1 Supertöllertag </b> <a href=iwmefwfe.de>  <table> < > ; ");
    }

    public static String modifyStringValue (String scope, String objectType, String description){
        description = Jsoup.clean(description,StringModifier.valueOf(objectType).whitelist());
        description = StringUtils.replaceEach(description.toString(), 
                new String[]{"<br>", "<p>", "<b>", "</b>", "&nbsp;"},
                new String[]{"<br />", "<br /><br />", "<strong>", "</strong>", " "});

        System.out.println(description);

        return null;
    }
}

结果:

<strong>Day 1 Supertöllertag </strong> <a href="iwmefwfe.de">  &lt; &gt; ; </a>

答案 1 :(得分:-1)

我建议你改用正则表达式。

这里有一个示例Regular expression to remove HTML tags from a string