Search in sources :

Example 1 with TagWriter

use of jodd.lagarto.TagWriter in project jodd by oblac.

the class StripHtmlTest method testStripHtml.

@Test
public void testStripHtml() {
    LagartoParser lagartoParser = new LagartoParser("<html>  <div   id='a'>   x \n\n\n </div>  </html>", false);
    StringBuilder out = new StringBuilder();
    StripHtmlTagAdapter stripHtmlTagAdapter = new StripHtmlTagAdapter(new TagWriter(out));
    lagartoParser.parse(stripHtmlTagAdapter);
    assertEquals("<html><div id=\"a\"> x </div></html>", out.toString());
}
Also used : TagWriter(jodd.lagarto.TagWriter) LagartoParser(jodd.lagarto.LagartoParser) Test(org.junit.Test)

Example 2 with TagWriter

use of jodd.lagarto.TagWriter in project jodd by oblac.

the class UrlRewriterTest method testUrlRewriter.

@Test
public void testUrlRewriter() {
    LagartoParser lagartoParser = new LagartoParser("<a href=\"http://jodd.org\">1</a><a href=\"page.html\">2</a>", false);
    StringBuilder out = new StringBuilder();
    UrlRewriterTagAdapter urlRewriterTagAdapter = new UrlRewriterTagAdapter(new TagWriter(out)) {

        @Override
        protected CharSequence rewriteUrl(CharSequence url) {
            String u = url.toString();
            if (u.startsWith("http")) {
                return url;
            }
            return "/ctx/" + url;
        }
    };
    lagartoParser.parse(urlRewriterTagAdapter);
    assertEquals("<a href=\"http://jodd.org\">1</a><a href=\"/ctx/page.html\">2</a>", out.toString());
}
Also used : TagWriter(jodd.lagarto.TagWriter) LagartoParser(jodd.lagarto.LagartoParser) Test(org.junit.Test)

Example 3 with TagWriter

use of jodd.lagarto.TagWriter in project jodd by oblac.

the class HtmlStaplerFilter method createParsingProcessor.

@Override
protected LagartoParsingProcessor createParsingProcessor() {
    if (!enabled) {
        return null;
    }
    return new LagartoParsingProcessor(true) {

        @Override
        protected char[] parse(TagWriter rootTagWriter, HttpServletRequest request) {
            TagVisitor visitor = rootTagWriter;
            if (stripHtml) {
                visitor = new StripHtmlTagAdapter(rootTagWriter) {

                    @Override
                    public void end() {
                        super.end();
                        if (log.isDebugEnabled()) {
                            log.debug("Stripped: " + getStrippedCharsCount() + " chars");
                        }
                    }
                };
            }
            String servletPath = DispatcherUtil.getServletPath(request);
            HtmlStaplerTagAdapter htmlStaplerTagAdapter = new HtmlStaplerTagAdapter(bundlesManager, servletPath, visitor);
            // todo add more adapters
            char[] content = invokeLagarto(htmlStaplerTagAdapter);
            return htmlStaplerTagAdapter.postProcess(content);
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TagWriter(jodd.lagarto.TagWriter) StripHtmlTagAdapter(jodd.lagarto.adapter.StripHtmlTagAdapter) TagVisitor(jodd.lagarto.TagVisitor)

Aggregations

TagWriter (jodd.lagarto.TagWriter)3 LagartoParser (jodd.lagarto.LagartoParser)2 Test (org.junit.Test)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 TagVisitor (jodd.lagarto.TagVisitor)1 StripHtmlTagAdapter (jodd.lagarto.adapter.StripHtmlTagAdapter)1