Class StripCharsTag

All Implemented Interfaces:
javax.servlet.jsp.tagext.IterationTag,, javax.servlet.jsp.tagext.Tag

public class StripCharsTag
extends javax.servlet.jsp.tagext.TagSupport

This class is a custom tag that renders the JWPStripChars() Javascript function which takes in three strings. The first is a string to manipulate. The second is either "strip" or "allow". When "strip", then the third string denotes a list of characters that will be stripped from the first, all other are left. It "allow", then any character NOT in the third string will be removed from the first.

This tag uses the following attributes:

renderScriptTags - true/false - When set to true, the Javascript will be rendered inside a <script> </script> tag pair. When set to false, this will not be done and it is expected that the <jstags:fullTrim/> tag appears inside a <script> </script> tag pair. If this attribute is not present, the script tags WILL be rendered.

It renders the following Javascript:

function JWPStripChars(jwpsc_inStr, jwpsc_stripOrAllow, jwpsc_charList) {
if (jwpsc_inStr == null || jwpsc_inStr == "" ||
jwpsc_charList == null || jwpsc_charList == "" ||
jwpsc_stripOrAllow == null || jwpsc_stripOrAllow == "") {
return "";
jwpsc_stripOrAllow = jwpsc_stripOrAllow.toLowerCase();
outStr = "";
for (i = 0; i < jwpsc_inStr.length; i++) {
nextChar = jwpsc_inStr.substr(i, 1);
keepChar = false;
for (j = 0; j < jwpsc_charList.length; j++) {
checkChar = jwpsc_charList.substr(j, 1);
if (jwpsc_stripOrAllow == "allow" && nextChar == checkChar) {
keepChar = true;
if (jwpsc_stripOrAllow == "strip" && nextChar != checkChar) {
keepChar = true;
if (keepChar == true) {
outStr = outStr + nextChar;
return outStr;

Usage example:

myString = JWPStripChars("This is a test", "strip", " ");
This will set myString equal to "Thisisatest", having strippg out all spaces from the input string.

myString = JWPStripChars("This is a test", "allow", "Tiat");
This will set myString equal to "Tiiatt" because every character except T, i, a, t (case-sensitive!) will be stripped out.

Frank W. Zammetti.
See Also:
Serialized Form

Field Summary
private static org.apache.commons.logging.Log log
          Log instance.
private  java.lang.String renderScriptTags
          Whether to render the opening and closing script tags around the emitted Javascript.
Fields inherited from class javax.servlet.jsp.tagext.TagSupport
id, pageContext
Fields inherited from interface javax.servlet.jsp.tagext.IterationTag
Fields inherited from interface javax.servlet.jsp.tagext.Tag
Constructor Summary
Method Summary
 int doStartTag()
          Render the results of the tag.
 void setRenderScriptTags(java.lang.String inRenderScriptTags)
          renderScriptTags mutator.
Methods inherited from class javax.servlet.jsp.tagext.TagSupport
doAfterBody, doEndTag, findAncestorWithClass, getId, getParent, getValue, getValues, release, removeValue, setId, setPageContext, setParent, setValue
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Detail


private static org.apache.commons.logging.Log log
Log instance.


private java.lang.String renderScriptTags
Whether to render the opening and closing script tags around the emitted Javascript.

Constructor Detail


public StripCharsTag()
Method Detail


public void setRenderScriptTags(java.lang.String inRenderScriptTags)
renderScriptTags mutator.

inRenderScriptTags - renderScriptTags.


public int doStartTag()
               throws javax.servlet.jsp.JspException
Render the results of the tag.

Specified by:
doStartTag in interface javax.servlet.jsp.tagext.Tag
doStartTag in class javax.servlet.jsp.tagext.TagSupport
Return code.
javax.servlet.jsp.JspException - If anything goes wrong

Copyright 2005 Frank W. Zammetti