Custom Tags 4 – Body manipulation of Classic Tags


This post explains how to manipulate the body of a classic tag.

Step 1. Write a BodyTagSupport and provide its doAfterBody() method.

package my;
public class Body extends BodyTagSupport {
  public int doAfterBody() {
    try {
      final String body = bodyContent.getString();
      final StringBuilder result = new StringBuilder();
      for (int index = 0; index < body.length(); index = index + 2) {
        result.append(body.charAt(index));
      }
      bodyContent.getEnclosingWriter().print(result.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return SKIP_BODY;
  }
}

Step 2. Declare it in a TLD.

<taglib ...>
  <uri>nikojava</uri>
  <tag>
    <name>body</name>
    <tag-class>my.Body</tag-class>
    <body-content>scriptless</body-content>
  </tag>
</taglib>

Step 3. Call it!

<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>ABABABABABABAB</mari:body>
    (After)
  </body>
</html>
Filtering the even characters of a classic tag's body

Filtering the even characters of a classic tag's body

As a result, only the characters at an even position (0, 2, 4, etc.) are printed!

Explanation

BodyTagSupport IS-A TagSupport. However it offers an extra weapon: EVAL_BODY_BUFFERED. If doStartTag() returns this value, then a series of methods is invoked: setBodyContent(BodyContent), then doInitBody(), then the body is evaluated and doAfterBody() is about to execute. At this point we do have access to the actual body content.

In the above example doStartTag() is not overriden, so by default EVAL_BODY_BUFFERED is returned. The actual content is taken as a string,

  final String body = bodyContent.getString();

and its even characters are selected:

  for (int index = 0; index < body.length(); index = index + 2) {
    result.append(body.charAt(index));
  }

More examples

Color alternate letters

public int doAfterBody() {
  try {
    final String body = bodyContent.getString();
    final StringBuilder result = new StringBuilder();
    for (int index = 0; index < body.length(); index++) {
      if (isOdd(index)) {
        result.append("<span style='color:red'>");
        result.append(body.charAt(index));
        result.append("</span>");
      } else {
        result.append(body.charAt(index));
      }
    }
    bodyContent.getEnclosingWriter().print(result.toString());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
private boolean isOdd(int number) {
  return number % 2 == 1;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>ABABABABABABAB</mari:body>
    (After)
  </body>
</html>
Coloring the even characters of a classic tag's body

Coloring the even characters of a classic tag's body

Remove capital letters

public int doAfterBody() {
  try {
    final String body = bodyContent.getString();
    final StringBuilder result = new StringBuilder();
    for (int index = 0; index < body.length(); index++) {
      final char current = body.charAt(index);
      if (!Character.isUpperCase(current)) {
        result.append(current);
      }
    }
    bodyContent.getEnclosingWriter().print(result.toString());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>Google Sun JBoss</mari:body>
    (After)
  </body>
</html>
Disposing the capital letters of a custom tag's body

Disposing the capital letters of a custom tag's body

Have fun

public int doAfterBody() {
  try {
    final StringBuilder body = new StringBuilder(bodyContent.getString());
    final int initialSize = body.length();
    for (int index = 0; index < initialSize; index++) {
      bodyContent.getEnclosingWriter().append(body);
      bodyContent.getEnclosingWriter().append("<br />");
      body.deleteCharAt(body.length() - 1);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    <mari:body>Microsystems</mari:body>
  </body>
</html>
Having fun with the body of a classic tag

Having fun with the body of a classic tag

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: