下面列出了怎么用javax.servlet.jsp.tagext.TagData的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Getter for property variableInfo.
*
* @return Value of property variableInfo.
*/
@Override
public VariableInfo[] getVariableInfo(TagData data) {
String prefix=(String)data.getAttribute("prefix");
if(prefix==null) {
prefix = "";
}
return new VariableInfo[]
{
new VariableInfo(prefix+"_navigation_switch", "String", true, VariableInfo.NESTED),
new VariableInfo(prefix+"_navigation_isHighlightKey", "Boolean", true, VariableInfo.NESTED),
new VariableInfo(prefix+"_navigation_token", "String", true, VariableInfo.NESTED),
new VariableInfo(prefix+"_navigation_href", "String", true, VariableInfo.NESTED),
new VariableInfo(prefix+"_navigation_navMsg", "String", true, VariableInfo.NESTED),
new VariableInfo(prefix+"_navigation_index", "Integer", true, VariableInfo.NESTED),
new VariableInfo(prefix+"_navigation_conditionSatisfied", "Boolean", true, VariableInfo.NESTED)
};
}
public VariableInfo[] getVariableInfo(TagData tagData) {
VariableInfo[] info = new VariableInfo[]{
new VariableInfo("renderRequest",
"javax.portlet.RenderRequest",
true,
VariableInfo.AT_BEGIN),
new VariableInfo("renderResponse",
"javax.portlet.RenderResponse",
true,
VariableInfo.AT_BEGIN),
new VariableInfo("portletConfig",
"javax.portlet.PortletConfig",
true,
VariableInfo.AT_BEGIN)
};
return info;
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[]
{
new VariableInfo("member",
"String",
true,
VariableInfo.NESTED)
};
}
public void setTagData(TagData tagData) {
this.tagData = tagData;
this.varInfos = tagInfo.getVariableInfo(tagData);
if (this.varInfos == null) {
this.varInfos = ZERO_VARIABLE_INFO;
}
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[]
{
new VariableInfo("member",
"String",
true,
VariableInfo.NESTED)
};
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("now", Bean.class.getCanonicalName(),
true, VariableInfo.AT_END)
};
}
/**
* Return information about the scripting variables to be created.
*/
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("Test", "java.lang.String", true,
VariableInfo.AT_END)
};
}
public void setTagData(TagData tagData) {
this.tagData = tagData;
this.varInfos = tagInfo.getVariableInfo(tagData);
if (this.varInfos == null) {
this.varInfos = ZERO_VARIABLE_INFO;
}
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("now", Bean.class.getCanonicalName(),
true, VariableInfo.AT_END)
};
}
/**
* Return information about the scripting variables to be created.
*/
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("Test", "java.lang.String", true,
VariableInfo.AT_END)
};
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[]
{
new VariableInfo("member",
"String",
true,
VariableInfo.NESTED)
};
}
public void setTagData(TagData tagData) {
this.tagData = tagData;
this.varInfos = tagInfo.getVariableInfo(tagData);
if (this.varInfos == null) {
this.varInfos = ZERO_VARIABLE_INFO;
}
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[]
{
new VariableInfo("member",
"String",
true,
VariableInfo.NESTED)
};
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("now", Bean.class.getCanonicalName(),
true, VariableInfo.AT_END)
};
}
/**
* Return information about the scripting variables to be created.
*/
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("Test", "java.lang.String", true,
VariableInfo.AT_END)
};
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[]
{
new VariableInfo("member",
"String",
true,
VariableInfo.NESTED)
};
}
@Override
public VariableInfo[] getVariableInfo(TagData tagData){
//如果不设置 使用 ${feilongPagerHtml1 } 是正常的
//但是如果使用 <%=feilongPagerHtml1%> 会提示 feilongPagerHtml1 cannot be resolved to a variable
String pagerHtmlAttributeName = defaultIfNullOrEmpty(
tagData.getAttributeString("pagerHtmlAttributeName"),
DEFAULT_PAGE_ATTRIBUTE_PAGER_HTML_NAME);
VariableInfo variableInfo = new VariableInfo(pagerHtmlAttributeName, String.class.getName(), true, VariableInfo.AT_END);
return ConvertUtil.toArray(variableInfo);
}
@Override
// JSP 2.0 and higher containers call validate() instead of isValid().
// The default implementation of this method is to call isValid().
// If isValid() returns false, a generic ValidationMessage[] is returned indicating isValid() returned false.
public ValidationMessage[] validate(TagData tagData){
if (LOGGER.isDebugEnabled()){
Map<String, Object> map = getTagDataAttributeMap(tagData);
LOGGER.debug(JsonUtil.format(map));
}
return super.validate(tagData);
}
/**
* 显示 {@link javax.servlet.jsp.tagext.TagData}里面的信息,一般用于 debug.
*
* @param tagData
* the tag data
* @return the map< string, object>
* @see javax.servlet.jsp.tagext.TagData#getAttributes()
* @since 1.4.0
*/
protected static Map<String, Object> getTagDataAttributeMap(TagData tagData){
Map<String, Object> map = new TreeMap<>();
Enumeration<String> attributes = tagData.getAttributes();
while (attributes.hasMoreElements()){
String key = attributes.nextElement();
map.put(key, tagData.getAttribute(key));
}
return map;
}
public void setTagData(TagData tagData) {
this.tagData = tagData;
this.varInfos = tagInfo.getVariableInfo(tagData);
if (this.varInfos == null) {
this.varInfos = ZERO_VARIABLE_INFO;
}
}
public VariableInfo [] getVariableInfo(TagData data) {
String name = data.getAttributeString("name");
String id = data.getAttributeString("id");
try {
Class.forName(Localization.ROOT + name);
return new VariableInfo[] {
new VariableInfo(id == null ? BundleTag.DEFAULT_ID : id, Localization.ROOT + name, true, VariableInfo.NESTED)
};
} catch (ClassNotFoundException e) {
return new VariableInfo[] {
new VariableInfo(id == null ? BundleTag.DEFAULT_ID : id, name, true, VariableInfo.NESTED)
};
}
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo(NavigatorApplicationIF.NAV_APP_KEY,
"net.ontopia.topicmaps.nav2.core.NavigatorApplicationIF",
true, VariableInfo.NESTED),
new VariableInfo(NavigatorApplicationIF.CONTEXT_KEY,
"net.ontopia.topicmaps.nav2.taglibs.logic.ContextTag",
true, VariableInfo.NESTED)
};
}
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo(NavigatorApplicationIF.USER_KEY,
"net.ontopia.topicmaps.nav2.core.UserIF",
true, VariableInfo.AT_END)
};
}
public VariableInfo[] getVariableInfo(TagData tagData) {
VariableInfo vi[] = null;
String var = tagData.getAttributeString("var");
if (var != null) {
vi = new VariableInfo[1];
vi[0] =
new VariableInfo(var, "java.lang.String", true,
VariableInfo.AT_BEGIN);
}
return vi;
}
@SuppressWarnings("null") // tagInfo can't be null after initial test
@Override
public void visit(Node.CustomTag n) throws JasperException {
TagInfo tagInfo = n.getTagInfo();
if (tagInfo == null) {
err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
}
/*
* The bodycontent of a SimpleTag cannot be JSP.
*/
if (n.implementsSimpleTag()
&& tagInfo.getBodyContent().equalsIgnoreCase(
TagInfo.BODY_CONTENT_JSP)) {
err.jspError(n, "jsp.error.simpletag.badbodycontent", tagInfo
.getTagClassName());
}
/*
* If the tag handler declares in the TLD that it supports dynamic
* attributes, it also must implement the DynamicAttributes
* interface.
*/
if (tagInfo.hasDynamicAttributes()
&& !n.implementsDynamicAttributes()) {
err.jspError(n, "jsp.error.dynamic.attributes.not.implemented",
n.getQName());
}
/*
* Make sure all required attributes are present, either as
* attributes or named attributes (<jsp:attribute>). Also make sure
* that the same attribute is not specified in both attributes or
* named attributes.
*/
TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
String customActionUri = n.getURI();
Attributes attrs = n.getAttributes();
int attrsSize = (attrs == null) ? 0 : attrs.getLength();
for (int i = 0; i < tldAttrs.length; i++) {
String attr = null;
if (attrs != null) {
attr = attrs.getValue(tldAttrs[i].getName());
if (attr == null) {
attr = attrs.getValue(customActionUri, tldAttrs[i]
.getName());
}
}
Node.NamedAttribute na = n.getNamedAttributeNode(tldAttrs[i]
.getName());
if (tldAttrs[i].isRequired() && attr == null && na == null) {
err.jspError(n, "jsp.error.missing_attribute", tldAttrs[i]
.getName(), n.getLocalName());
}
if (attr != null && na != null) {
err.jspError(n, "jsp.error.duplicate.name.jspattribute",
tldAttrs[i].getName());
}
}
Node.Nodes naNodes = n.getNamedAttributeNodes();
int jspAttrsSize = naNodes.size() + attrsSize;
Node.JspAttribute[] jspAttrs = null;
if (jspAttrsSize > 0) {
jspAttrs = new Node.JspAttribute[jspAttrsSize];
}
Hashtable<String, Object> tagDataAttrs = new Hashtable<>(attrsSize);
checkXmlAttributes(n, jspAttrs, tagDataAttrs);
checkNamedAttributes(n, jspAttrs, attrsSize, tagDataAttrs);
TagData tagData = new TagData(tagDataAttrs);
// JSP.C1: It is a (translation time) error for an action that
// has one or more variable subelements to have a TagExtraInfo
// class that returns a non-null object.
TagExtraInfo tei = tagInfo.getTagExtraInfo();
if (tei != null && tei.getVariableInfo(tagData) != null
&& tei.getVariableInfo(tagData).length > 0
&& tagInfo.getTagVariableInfos().length > 0) {
err.jspError("jsp.error.non_null_tei_and_var_subelems", n
.getQName());
}
n.setTagData(tagData);
n.setJspAttributes(jspAttrs);
visitBody(n);
}
private void checkNamedAttributes(Node.CustomTag n,
Node.JspAttribute[] jspAttrs, int start,
Hashtable<String, Object> tagDataAttrs)
throws JasperException {
TagInfo tagInfo = n.getTagInfo();
TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
Node.Nodes naNodes = n.getNamedAttributeNodes();
for (int i = 0; i < naNodes.size(); i++) {
Node.NamedAttribute na = (Node.NamedAttribute) naNodes
.getNode(i);
boolean found = false;
for (int j = 0; j < tldAttrs.length; j++) {
/*
* See above comment about namespace matches. For named
* attributes, we use the prefix instead of URI as the match
* criterion, because in the case of a JSP document, we'd
* have to keep track of which namespaces are in scope when
* parsing a named attribute, in order to determine the URI
* that the prefix of the named attribute's name matches to.
*/
String attrPrefix = na.getPrefix();
if (na.getLocalName().equals(tldAttrs[j].getName())
&& (attrPrefix == null || attrPrefix.length() == 0 || attrPrefix
.equals(n.getPrefix()))) {
jspAttrs[start + i] = new Node.JspAttribute(na,
tldAttrs[j], false);
NamedAttributeVisitor nav = null;
if (na.getBody() != null) {
nav = new NamedAttributeVisitor();
na.getBody().visit(nav);
}
if (nav != null && nav.hasDynamicContent()) {
tagDataAttrs.put(na.getName(),
TagData.REQUEST_TIME_VALUE);
} else {
tagDataAttrs.put(na.getName(), na.getText());
}
found = true;
break;
}
}
if (!found) {
if (tagInfo.hasDynamicAttributes()) {
jspAttrs[start + i] = new Node.JspAttribute(na, null,
true);
} else {
err.jspError(n, "jsp.error.bad_attribute",
na.getName(), n.getLocalName());
}
}
}
}
public TagData getTagData() {
return tagData;
}
protected String createBeanVarDeclarations(List<String> localBeans) {
//TODO: the parser data contains no information about offsets and
//therefore it is not possible to create proper java embeddings
//inside bean declarations. We need a similar solution to what was
//done for imports, see issue #161246
StringBuilder beanDeclarationsBuff = new StringBuilder();
PageInfo pageInfo = getPageInfo();
if (pageInfo != null) {
PageInfo.BeanData[] beanData = getBeanData();
if (beanData != null) {
for (PageInfo.BeanData bean : beanData) {
if (!localBeans.contains(bean.getId())) {
beanDeclarationsBuff.append(bean.getClassName() + " " + bean.getId() + ";\n"); //NOI18N
}
}
}
if (pageInfo.isTagFile()) {
for (TagAttributeInfo info : pageInfo.getTagInfo().getAttributes()) {
if (info.getTypeName() != null) { // will be null e.g. for fragment attrs
if (!localBeans.contains(info.getName())) {
beanDeclarationsBuff.append(info.getTypeName() + " " + info.getName() + ";\n"); //NOI18N
}
}
}
}
}
JspSyntaxSupport syntaxSupport = JspSyntaxSupport.get(doc);
JspColoringData coloringData = getParserData().getColoringData();
if (coloringData != null && coloringData.getPrefixMapper() != null) {
Collection<String> prefixes = coloringData.getPrefixMapper().keySet();
TagData fooArg = new TagData((Object[][]) null);
for (String prefix : prefixes) {
List<TagInfo> tags = syntaxSupport.getAllTags(prefix, false); //do not require fresh data - #146762
for (TagInfo tag : tags) {
// #146754 - prevent NPE:
if (tag == null) {
continue;
}
VariableInfo vars[] = tag.getVariableInfo(fooArg);
if (vars != null) {
for (VariableInfo var : vars) {
// Create Variable Definitions
if (var != null && var.getVarName() != null && var.getClassName() != null && var.getDeclare()) {
String varDeclaration = var.getClassName() + " " + var.getVarName() + ";\n";
beanDeclarationsBuff.append(varDeclaration);
}
}
}
}
}
}
return beanDeclarationsBuff.toString();
}
@Override
public void visit(Node.CustomTag n) throws JasperException {
TagInfo tagInfo = n.getTagInfo();
if (tagInfo == null) {
err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
}
/*
* The bodycontent of a SimpleTag cannot be JSP.
*/
if (n.implementsSimpleTag()
&& tagInfo.getBodyContent().equalsIgnoreCase(
TagInfo.BODY_CONTENT_JSP)) {
err.jspError(n, "jsp.error.simpletag.badbodycontent", tagInfo
.getTagClassName());
}
/*
* If the tag handler declares in the TLD that it supports dynamic
* attributes, it also must implement the DynamicAttributes
* interface.
*/
if (tagInfo.hasDynamicAttributes()
&& !n.implementsDynamicAttributes()) {
err.jspError(n, "jsp.error.dynamic.attributes.not.implemented",
n.getQName());
}
/*
* Make sure all required attributes are present, either as
* attributes or named attributes (<jsp:attribute>). Also make sure
* that the same attribute is not specified in both attributes or
* named attributes.
*/
TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
String customActionUri = n.getURI();
Attributes attrs = n.getAttributes();
int attrsSize = (attrs == null) ? 0 : attrs.getLength();
for (int i = 0; i < tldAttrs.length; i++) {
String attr = null;
if (attrs != null) {
attr = attrs.getValue(tldAttrs[i].getName());
if (attr == null) {
attr = attrs.getValue(customActionUri, tldAttrs[i]
.getName());
}
}
Node.NamedAttribute na = n.getNamedAttributeNode(tldAttrs[i]
.getName());
if (tldAttrs[i].isRequired() && attr == null && na == null) {
err.jspError(n, "jsp.error.missing_attribute", tldAttrs[i]
.getName(), n.getLocalName());
}
if (attr != null && na != null) {
err.jspError(n, "jsp.error.duplicate.name.jspattribute",
tldAttrs[i].getName());
}
}
Node.Nodes naNodes = n.getNamedAttributeNodes();
int jspAttrsSize = naNodes.size() + attrsSize;
Node.JspAttribute[] jspAttrs = null;
if (jspAttrsSize > 0) {
jspAttrs = new Node.JspAttribute[jspAttrsSize];
}
Hashtable<String, Object> tagDataAttrs = new Hashtable<String, Object>(attrsSize);
checkXmlAttributes(n, jspAttrs, tagDataAttrs);
checkNamedAttributes(n, jspAttrs, attrsSize, tagDataAttrs);
TagData tagData = new TagData(tagDataAttrs);
// JSP.C1: It is a (translation time) error for an action that
// has one or more variable subelements to have a TagExtraInfo
// class that returns a non-null object.
TagExtraInfo tei = tagInfo.getTagExtraInfo();
if (tei != null && tei.getVariableInfo(tagData) != null
&& tei.getVariableInfo(tagData).length > 0
&& tagInfo.getTagVariableInfos().length > 0) {
err.jspError("jsp.error.non_null_tei_and_var_subelems", n
.getQName());
}
n.setTagData(tagData);
n.setJspAttributes(jspAttrs);
visitBody(n);
}
private void checkNamedAttributes(Node.CustomTag n,
Node.JspAttribute[] jspAttrs, int start,
Hashtable<String, Object> tagDataAttrs)
throws JasperException {
TagInfo tagInfo = n.getTagInfo();
if (tagInfo == null) {
err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
}
TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
Node.Nodes naNodes = n.getNamedAttributeNodes();
for (int i = 0; i < naNodes.size(); i++) {
Node.NamedAttribute na = (Node.NamedAttribute) naNodes
.getNode(i);
boolean found = false;
for (int j = 0; j < tldAttrs.length; j++) {
/*
* See above comment about namespace matches. For named
* attributes, we use the prefix instead of URI as the match
* criterion, because in the case of a JSP document, we'd
* have to keep track of which namespaces are in scope when
* parsing a named attribute, in order to determine the URI
* that the prefix of the named attribute's name matches to.
*/
String attrPrefix = na.getPrefix();
if (na.getLocalName().equals(tldAttrs[j].getName())
&& (attrPrefix == null || attrPrefix.length() == 0 || attrPrefix
.equals(n.getPrefix()))) {
jspAttrs[start + i] = new Node.JspAttribute(na,
tldAttrs[j], false);
NamedAttributeVisitor nav = null;
if (na.getBody() != null) {
nav = new NamedAttributeVisitor();
na.getBody().visit(nav);
}
if (nav != null && nav.hasDynamicContent()) {
tagDataAttrs.put(na.getName(),
TagData.REQUEST_TIME_VALUE);
} else {
tagDataAttrs.put(na.getName(), na.getText());
}
found = true;
break;
}
}
if (!found) {
if (tagInfo.hasDynamicAttributes()) {
jspAttrs[start + i] = new Node.JspAttribute(na, null,
true);
} else {
err.jspError(n, "jsp.error.bad_attribute",
na.getName(), n.getLocalName());
}
}
}
}