📄 tagutil.java
字号:
package jsp.tags.dapact.util;
import java.util.*;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 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@
*/
/**
* Utility class for the tag library - contains various utility functions.
*/
public class TagUtil
{
/**
* This is the character that should appear at the beginning of the attribute name
* to hide the attribute from the output.
*/
public static final char HIDE_ATTR_CHAR = '-';
/**
* Concatenates all attributes into attribute value pairs (separated by spaces)
* and returns it as a string. It takes care of converting any double quotes into
* " and it will not add any attributes that start with a minus sign (so that
* custom attributes do not show up as attributes of the controls in HTML).
*
* @return a string of all attribute and value pairs in the following format:
* attr1="value1" attr2="value2"
*/
protected static final String SPACE = " ";
protected static final String EQUALS_QUOTE = "=\"";
protected static final String QUOTE = "\"";
public static final String getAllAttrAsStr(Enumeration attrNames, TagSupport tag)
{
StringBuffer result = new StringBuffer();
while (attrNames.hasMoreElements())
{
// Build the name value pair and write it to the output stream.
String key = (String)attrNames.nextElement();
if ((key != null) && (key.length() > 0) && (key.charAt(0) != HIDE_ATTR_CHAR))
{
String value = null;
try
{
value = (String)tag.getValue(key);
}
catch (ClassCastException e)
{
Object objVal = tag.getValue(key);
value = objVal.toString();
}
if (value != null)
{
result.append(SPACE);
result.append(key);
result.append(EQUALS_QUOTE);
result.append(makeStringAttrSafe(value));
result.append(QUOTE);
}
}
}
return result.toString();
}
/**
* Static function that takes a string and turns all double quotes in it ampersand quot;
*
* @param value the string to be converted.
*
* @return the string with all double quotes turned into amp quot;
*/
public static final String makeStringAttrSafe(String value)
{
return replace(value, "\"", """);
}
/**
* Static function that takes a string and turns all double quotes in it ampersand quot;
*
* @param value the string to be converted.
*
* @return the string with all double quotes turned into amp quot;
*/
public static final String replace(String value, final String stringToMatch, final String replacementString)
{
if (value != null)
{
StringBuffer buff = null;
int pos = 0;
int lastpos = 0;
while ((pos = value.indexOf(stringToMatch, lastpos)) > -1)
{
// Only create the buffer if we get this far...
if (buff == null)
{
buff = new StringBuffer(value.length() + replacementString.length());
}
buff.append(value.substring(lastpos, pos));
buff.append(replacementString);
lastpos = pos + 1;
}
// Append the last part of the string to the return buffer.
if (buff != null)
{
buff.append(value.substring(lastpos));
value = buff.toString();
}
}
return value;
}
/**
* Examine an object and get one string value out of it.
*
* @param obj the object to examine.
*
* @return a string representing the object or null if that is what was passed in.
*/
public static final String getStringFromObj(Object obj)
{
String result = null;
if (obj != null)
{
if (obj instanceof String)
{
result = (String)obj;
}
else if (obj instanceof String[])
{
result = ((String[])obj)[0];
}
else
{
result = obj.toString();
}
}
return result;
}
/**
* For testing purposes....
*/
public static void main(String[] args)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -