javawebparts.taglib.jstags
Class StripCharsTag

java.lang.Object
  |
  +--javax.servlet.jsp.tagext.TagSupport
        |
        +--javawebparts.taglib.jstags.StripCharsTag
All Implemented Interfaces:
javax.servlet.jsp.tagext.IterationTag, java.io.Serializable, 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.

Author:
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
EVAL_BODY_AGAIN
 
Fields inherited from interface javax.servlet.jsp.tagext.Tag
EVAL_BODY_INCLUDE, EVAL_PAGE, SKIP_BODY, SKIP_PAGE
 
Constructor Summary
StripCharsTag()
           
 
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

log

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


renderScriptTags

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

Constructor Detail

StripCharsTag

public StripCharsTag()
Method Detail

setRenderScriptTags

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

Parameters:
inRenderScriptTags - renderScriptTags.

doStartTag

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

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


Copyright © 2005 Frank W. Zammetti