Created
November 5, 2014 17:56
-
-
Save yupadhyay/0e54657d7993c7448572 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test.wemblog | |
import javax.servlet.jsp.tagext.BodyContent; | |
import javax.servlet.jsp.tagext.BodyTagSupport; | |
import java.io.IOException; | |
import javax.servlet.jsp.JspWriter; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* wemblog:removeWhitespace tag removes whitespace between HTML elements | |
*/ | |
public class RemoveWhitespace extends BodyTagSupport | |
{ | |
private static final Logger LOGGER = LoggerFactory.getLogger(RemoveWhitespace.class); | |
private static final String REGEX_WHITESPACE_BETWEEN_HTML = "[>]{1}\\s+[<]{1}"; | |
private static final String REGEX_REPLACE_BETWEEN_HTML = "><"; | |
public void setBodyContent(BodyContent bc) | |
{ | |
super.setBodyContent(bc); | |
} | |
@Override | |
public int doAfterBody() | |
{ | |
try | |
{ | |
BodyContent bodyContent = super.getBodyContent(); | |
JspWriter out = bodyContent.getEnclosingWriter(); | |
String html = bodyContent.getString(); | |
out.print(html.replaceAll(REGEX_WHITESPACE_BETWEEN_HTML, REGEX_REPLACE_BETWEEN_HTML)); | |
} | |
catch (IOException e) | |
{ | |
LOGGER.debug("wemblog:removeWhitespace error: " + e.getMessage()); | |
} | |
return SKIP_BODY; | |
} | |
} | |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1"> | |
<tlib-version>5.4.0-SNAPSHOT</tlib-version> | |
<short-name>wemblog</short-name> | |
<uri>http://www.wemblog.com</uri> | |
<tag> | |
<name>removeWhitespace</name> | |
<tagclass>com.test.wemblog.RemoveWhitespace</tagclass> | |
<body-content>scriptless</body-content> | |
<info>Remove Whitespace</info> | |
</tag> | |
</taglib> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment