本文整理汇总了Java中org.owasp.html.HtmlStreamRenderer类的典型用法代码示例。如果您正苦于以下问题:Java HtmlStreamRenderer类的具体用法?Java HtmlStreamRenderer怎么用?Java HtmlStreamRenderer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HtmlStreamRenderer类属于org.owasp.html包,在下文中一共展示了HtmlStreamRenderer类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.owasp.html.HtmlStreamRenderer; //导入依赖的package包/类
/**
* A test-bed that reads HTML from stdin and writes sanitized content to
* stdout.
*/
public static void main(String[] args) throws IOException {
if (args.length != 0) {
System.err.println("Reads from STDIN and writes to STDOUT");
System.exit(-1);
}
System.err.println("[Reading from STDIN]");
// Fetch the HTML to sanitize.
String html = CharStreams.toString(
new InputStreamReader(System.in, Charsets.UTF_8));
// Set up an output channel to receive the sanitized HTML.
HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
System.out,
// Receives notifications on a failure to write to the output.
new Handler<IOException>() {
public void handle(IOException ex) {
Throwables.propagate(ex); // System.out suppresses IOExceptions
}
},
// Our HTML parser is very lenient, but this receives notifications on
// truly bizarre inputs.
new Handler<String>() {
public void handle(String x) {
throw new AssertionError(x);
}
});
// Use the policy defined above to sanitize the HTML.
HtmlSanitizer.sanitize(html, POLICY_DEFINITION.apply(renderer));
}
示例2: makePolicy
import org.owasp.html.HtmlStreamRenderer; //导入依赖的package包/类
private static Policy makePolicy(Appendable buffer) {
final HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
buffer,
new Handler<IOException>() {
public void handle(IOException ex) {
throw new RuntimeException(ex);
}
},
new Handler<String>() {
public void handle(String errorMessage) {
throw new RuntimeException(errorMessage);
}
});
return new HtmlPolicyBuilder()
.allowElements( "h1", "h2", "h3", "h4", "p",
"ol", "li", "ul",
"i", "u", "b",
"blockquote",
"a", "br", "div", "img", "span")
.allowAttributes("href").onElements("a")
.allowAttributes("src").onElements("img")
.allowAttributes("width").onElements("img")
.allowAttributes("height").onElements("img")
.allowAttributes("alt").onElements("img")
.allowAttributes("class", "id", "title").globally()
.allowStandardUrlProtocols()
// .requireRelNofollowOnLinks()
// .disallowElements("script")
.build(renderer);
}
示例3: ImageAwareHtmlRenderer
import org.owasp.html.HtmlStreamRenderer; //导入依赖的package包/类
public ImageAwareHtmlRenderer(Appendable htmlOutput, List<StreamItemMedia> media) {
this.media = media;
this.htmlStreamRenderer = HtmlStreamRenderer.create(htmlOutput, Handler.DO_NOTHING, Handler.DO_NOTHING);
}