📄 basetagsupport.java
字号:
package jsp.tags.dapact;
import java.util.Enumeration;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import jsp.tags.dapact.conf.UserClassFactory;
import jsp.tags.dapact.lookup.TagDataParamNames;
import jsp.tags.dapact.util.TagUtil;
/**
* Title: Data Aware Processing And Control Tags
* Description: Tag library for the processing and controlling the input and output of data.
* Copyright: LGPL (http://www.gnu.org/copyleft/lesser.html)
* Compile Date: @compile_date@
* @author Allen M Servedio
* @amp_sign@version @VERSION@
*/
/**
* Base class for all tags that need to implement the Tag interface by providing
* an implementation of the interface that works with the tag library to add further
* functionality.
*/
public class BaseTagSupport extends TagSupport
{
/**
* Default constructor.
*/
public BaseTagSupport()
{
}
/**
* Called when the starting tag is found.
*
* @return EVAL_BODY_INCLUDE so that it processes what ever is in the body.
*/
public int doStartTag() throws JspException
{
super.doStartTag();
return EVAL_BODY_INCLUDE;
}
/**
* Called at the end of the tag.
*
* @exception JspException general JSP exception.
*
* @return EVAL_PAGE so that it continues to process the rest of the page.
*/
public int doEndTag() throws JspException
{
super.doEndTag();
return EVAL_PAGE;
}
/**
* Call this to set an object value for a specific key. This is useful for storing
* non-Sting objects in a tag for later retrieval. The value passed in will be processed
* by a call to TagUtil.filterValue(String, Object, TagSupport, PageContext). If
* that filter returns a null, it will remove the key-value from storage.
*
* @param key the key to reference the object with - remember, these keys will be mixed
* with the ones from the setValue(String, String) function call. If this is null, the
* filter will not be called and nothing will happen.
* @param value the object to store for the key.
*/
public void setValue(String key, Object value)
{
if (key != null)
{
Object newValue = UserClassFactory.getFilterValueInstance(getFilterValueName()).lookupValue(key, value, this, pageContext);
if (newValue != null)
{
super.setValue(key, newValue);
String name = isPublishableAttr(key, newValue);
if (name != null)
{
pageContext.setAttribute(name, newValue);
}
}
else
{
removeValue(key);
String name = isReleasableAttr(key);
if (name != null)
{
pageContext.removeAttribute(name);
}
}
}
}
/**
* Call this to set a string value for a specific key. This is useful for storing
* non-Object objects in a tag for later retrieval - like the value of tag attributes.
* The value passed in will be processed by a call to TagUtil.filterValue(String, Object, TagSupport, PageContext).
* If that filter returns a null, it will remove the key-value from storage.
*
* @param key the key to reference the object with - remember, these keys will be mixed
* with the ones from the setValue(String, Object) function call. If this is null, the
* filter will not be called and nothing will happen.
* @param value the string to store for the key.
*/
public void setValue(String key, String value)
{
if (key != null)
{
Object newValue = UserClassFactory.getFilterValueInstance(getFilterValueName()).lookupValue(key, value, this, pageContext);
if (newValue != null)
{
super.setValue(key, newValue);
String name = isPublishableAttr(key, newValue);
if (name != null)
{
pageContext.setAttribute(name, newValue);
}
}
else
{
removeValue(key);
String name = isReleasableAttr(key);
if (name != null)
{
pageContext.removeAttribute(name);
}
}
}
}
/**
* Retrieves a value as a String.
*
* @param key the key to be used to look up the value.
*
* @return the value as a string or null if it was not found.
*/
public String getValueAsString(String key)
{
String result = null;
if (key != null)
{
Object value = getValue(key);
if (value != null)
{
result = value.toString();
}
}
return result;
}
/**
* Called by the container to free the tag's resources - this also releases any published
* attributes. This is called after the end tag.
*/
public void release()
{
// Releases the tag
Enumeration attrs = getValues();
if (attrs != null)
{
while (attrs.hasMoreElements())
{
String key = (String)attrs.nextElement();
String name = isReleasableAttr(key);
if (name != null)
{
pageContext.removeAttribute(name);
}
}
}
super.release();
}
/**
* Checks to see if the attribute should be published (and, if so, with what name).
* This base implementation always returns null - the subclass should make the decision.
* This implementation will therefore only mandate that this function be overridden
* when the subclass wants to publish some attributes.
*
* @param attr the attribute under consideration.
* @param value the attribute's current value.
*
* @return <code>null</code> if it should not be published, otherwise the name that will be
* used to publish it (set it as an attribute of the page context).
*/
public String isPublishableAttr(String attr, Object value)
{
return null;
}
/**
* Checks to see if the attribute should be released (and, if so, with what name).
* This base implementation always returns null - the subclass should make the decision.
* This implementation will therefore only mandate that this function be overridden
* when the subclass wants to release some attributes.
*
* @param attr the attribute under consideration.
* @param value the attribute's current value.
*
* @return <code>null</code> if it should not be released, otherwise the name that will be
* used to release it (from the page context).
*/
public String isReleasableAttr(String attr)
{
return null;
}
/**
* Retrieve the name of the filter value object to create (will be used to look up
* the filter value object class name).
*/
protected String getFilterValueName()
{
String name = null;
try
{
name = (String)pageContext.findAttribute(TagDataParamNames.FILTER_VALUE_NAME_DATA_NAME);
}
catch (ClassCastException e)
{
// Just log exception.
UserClassFactory.getLogger().log("Filter value name found but is not a string.", e);
}
return name;
}
/**
* @return Retrieve the name of the lookup value object to create (will be used to look up
* the lookup value object class name).
*/
protected String getLookupValueName()
{
String name = null;
try
{
name = (String)pageContext.findAttribute(TagDataParamNames.LOOKUP_VALUE_NAME_DATA_NAME);
}
catch (ClassCastException e)
{
// Just log exception.
UserClassFactory.getLogger().log("Lookup value name found but is not a string.", e);
}
return name;
}
/*=============================================================================*/
/* PROPERTIES/ATTRIBUTES */
/*=============================================================================*/
/**
* A string constant that is used to retrieve and set the value of the lookup name property.
*/
public static final String LOOKUP_NAME_CONST = TagUtil.HIDE_ATTR_CHAR + "lookupName";
/**
* Get the lookup name of the object.
*/
public String getLookupName()
{
return (String)getValue(LOOKUP_NAME_CONST);
}
/**
* Set the lookup name of the object.
*/
public void setLookupName(String lookupName)
{
this.setValue(LOOKUP_NAME_CONST, lookupName);
}
/**
* A string constant that is used to retrieve and set the value of the name property.
*/
public static final String NAME_CONST = "name";
/**
* Get the name of the object.
*/
public String getName()
{
return (String)getValue(NAME_CONST);
}
/**
* Set the name of the object.
*/
public void setName(String name)
{
this.setValue(NAME_CONST, name);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -