generictag.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,024 行 · 第 1/2 页
JAVA
1,024 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.jsp.java;import com.caucho.jsp.JspParseException;import com.caucho.jsp.TagInstance;import com.caucho.util.BeanUtil;import com.caucho.vfs.WriteStream;import com.caucho.xml.QName;import javax.servlet.jsp.tagext.*;import java.beans.BeanInfo;import java.beans.Introspector;import java.io.IOException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.ArrayList;import java.util.HashSet;import java.util.Hashtable;import java.util.logging.Level;/** * Represents a custom tag. */abstract public class GenericTag extends JspContainerNode{ private static final String DEFAULT_VAR_TYPE = "java.lang.String"; private static final HashSet<String> _primTypes = new HashSet<String>(); protected TagInstance _tag; protected TagInfo _tagInfo; protected Class _tagClass; protected VariableInfo []_varInfo; private boolean _isDeclaringInstance; public GenericTag() { } public void setTagInfo(TagInfo tagInfo) { _tagInfo = tagInfo; } public TagInfo getTagInfo() { return _tagInfo; } @Override public boolean isPre21Taglib() { if (_tagInfo == null) return false; TagLibraryInfo library = _tagInfo.getTagLibrary(); if (library == null || library.getRequiredVersion() == null) return false; return "2.1".compareTo(library.getRequiredVersion()) > 0; } public TagInstance getTag() { return _tag; } /** * Returns the tag name for the current tag. */ public String getCustomTagName() { return _tag.getId(); } /** * Returns true if the tag is a simple tag. */ public boolean isSimple() { return _tag.isSimpleTag(); } /** * True if this is a jstl node. */ public boolean isJstl() { String uri = _tag.getTagInfo().getTagLibrary().getURI(); return (JavaJspBuilder.JSTL_CORE_URI.equals(uri) || JavaJspBuilder.JSTL_EL_CORE_URI.equals(uri) || JavaJspBuilder.JSTL_FMT_URI.equals(uri) || JavaJspBuilder.JSTL_XML_URI.equals(uri) || JavaJspBuilder.JSTL_SQL_URI.equals(uri)); } public void setTagClass(Class cl) { _tagClass = cl; } public VariableInfo []getVarInfo() { return _varInfo; } /** * Returns the body content. */ public String getBodyContent() { return _tagInfo.getBodyContent(); } /** * Adds a child node. */ public void addChild(JspNode node) throws JspParseException { if (! "empty".equals(getBodyContent())) super.addChild(node); else if (node instanceof JspAttribute) { super.addChild(node); } else if (node instanceof StaticText && ((StaticText) node).isWhitespace()) { } else { throw error(L.l("<{0}> must be empty. Since <{0}> has a body-content of 'empty', it must not have any content.", getTagName())); } } /** * Completes the element */ public void endElement() throws Exception { if (_tagClass != null) _gen.addDepend(_tagClass); Hashtable<String,Object> tags = new Hashtable<String,Object>(); for (int i = 0; i < _attributeNames.size(); i++) { QName qName = _attributeNames.get(i); Object value = _attributeValues.get(i); String name = qName.getName(); if (value instanceof JspAttribute) { JspAttribute attr = (JspAttribute) value; if (attr.isStatic()) tags.put(name, attr.getStaticText()); else tags.put(name, TagData.REQUEST_TIME_VALUE); } else if (value instanceof String && hasRuntimeAttribute((String) value)) tags.put(name, TagData.REQUEST_TIME_VALUE); else tags.put(name, value); TagAttributeInfo attrInfo = getAttributeInfo(qName); String typeName = null; boolean isFragment = false; Method method = getAttributeMethod(qName); Class type = null; if (method != null) type = method.getParameterTypes()[0]; if (attrInfo != null) { typeName = attrInfo.getTypeName(); isFragment = attrInfo.isFragment(); if (isFragment && type != null && type.isAssignableFrom(JspFragment.class)) typeName = JspFragment.class.getName(); } else if (method != null) typeName = type.getName(); if (! isFragment && ! JspFragment.class.getName().equals(typeName)) { } else if (value instanceof JspAttribute) { JspAttribute jspAttr = (JspAttribute) value; jspAttr.setJspFragment(true); } } TagData tagData = new TagData(tags); _varInfo = _tagInfo.getVariableInfo(tagData); if (_varInfo == null) _varInfo = fillVariableInfo(_tagInfo.getTagVariableInfos(), tagData); TagExtraInfo tei = _tagInfo.getTagExtraInfo(); ValidationMessage []messages; if (tei != null) { messages = tei.validate(tagData); _gen.addDepend(tei.getClass()); if (messages != null && messages.length != 0) { throw error(messages[0].getMessage()); } } } /** * True if the node has scripting */ public boolean hasScripting() { if (super.hasScripting()) return true; // Any conflicting values must be set each time. for (int i = 0; i < _attributeValues.size(); i++) { QName name = _attributeNames.get(i); Object value = _attributeValues.get(i); try { if (value instanceof String && hasRuntimeAttribute((String) value)) return true; } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); return true; } } return false; } /** * True if the jsf-parent setting is required. */ @Override public boolean isJsfParentRequired() { return true; } /** * Returns true if this instance declares the tag. */ public boolean isDeclaringInstance() { return _isDeclaringInstance; } /** * Returns the variable containing the jsf component */ public String getJsfVar() { return null; } /** * Returns the variable containing the jsf body */ public String getJsfBodyVar() { return null; } /** * Generates code before the actual JSP. */ public void generatePrologue(JspJavaWriter out) throws Exception { for (int i = 0; i < _attributeNames.size(); i++) { QName name = _attributeNames.get(i); Object value = _attributeValues.get(i); if (! (value instanceof JspFragmentNode)) continue; JspFragmentNode frag = (JspFragmentNode) value; TagAttributeInfo attribute = getAttributeInfo(name); String typeName = null; boolean isFragment = false; if (attribute != null && attribute.isFragment()) isFragment = true; String fragmentClass = JspFragment.class.getName(); if (attribute != null && fragmentClass.equals(attribute.getTypeName())) isFragment = true; Method method = getAttributeMethod(name); if (method != null) { typeName = method.getParameterTypes()[0].getName(); if (fragmentClass.equals(typeName)) isFragment = true; } if (isFragment) frag.generateFragmentPrologue(out); } TagInstance parent = getParent().getTag(); boolean isBodyTag = BodyTag.class.isAssignableFrom(_tagClass); boolean isEmpty = isEmpty(); boolean hasBodyContent = isBodyTag && ! isEmpty; _tag = parent.findTag(getQName(), _attributeNames, hasBodyContent); if (_tag == null || ! _parseState.isRecycleTags()) { _tag = parent.addTag(_gen, getQName(), _tagInfo, _tagClass, _attributeNames, _attributeValues, hasBodyContent); if (! JspTagFileSupport.class.isAssignableFrom(_tagClass)) { out.printClass(_tagClass); out.println(" " + _tag.getId() + " = null;"); _isDeclaringInstance = true; } /* if (SimpleTag.class.isAssignableFrom(_tagClass) && hasCustomTag()) out.println("javax.servlet.jsp.tagext.Tag " + _tag.getId() + "_adapter = null;"); */ } else { // Any conflicting values must be set each time. for (int i = 0; i < _attributeNames.size(); i++) { QName name = _attributeNames.get(i); Object value = _attributeValues.get(i); _tag.addAttribute(name, value); } } if (_tag == null) throw new NullPointerException(); /* already taken care of if (! isEmpty()) _tag.setBodyContent(true); */ generatePrologueDeclare(out); generatePrologueChildren(out); } public void generatePrologueDeclare(JspJavaWriter out) throws Exception { // Any AT_END variables for (int i = 0; _varInfo != null && i < _varInfo.length; i++) { VariableInfo var = _varInfo[i]; if (var == null) { } else if (! _gen.hasScripting()) { } else if ((var.getScope() == VariableInfo.AT_END || var.getScope() == VariableInfo.AT_BEGIN) && var.getDeclare() && ! _gen.isDeclared(var.getVarName())) { String className = var.getClassName(); if (className == null || "".equals(className) || "null".equals(className)) className = DEFAULT_VAR_TYPE; validateClass(className, var.getVarName()); out.print(className + " " + var.getVarName() + " = "); _gen.addDeclared(var.getVarName()); if ("byte".equals(var.getClassName()) || "short".equals(var.getClassName()) || "char".equals(var.getClassName()) || "int".equals(var.getClassName()) || "long".equals(var.getClassName()) || "float".equals(var.getClassName()) || "double".equals(var.getClassName())) out.println("0;"); else if ("boolean".equals(var.getClassName())) out.println("false;"); else out.println("null;"); } } } /** * Generates the XML text representation for the tag validation. * * @param os write stream to the generated XML. */ public void printXml(WriteStream os) throws IOException { TagInfo tag = getTagInfo(); String name = tag.getTagLibrary().getPrefixString() + ':' + tag.getTagName(); os.print("<" + name); printJspId(os); for (int i = 0; i < _attributeNames.size(); i++) { QName attrName = _attributeNames.get(i); Object value = _attributeValues.get(i); if (value instanceof String) { String string = (String) value; printXmlAttribute(os, attrName.getName(), string); } } os.print(">"); printXmlChildren(os); os.print("</" + name + ">"); } /** * Generates the code for a custom tag. * * @param out the output writer for the generated java. */ abstract public void generate(JspJavaWriter out) throws Exception; protected void fillAttributes(JspJavaWriter out, String name) throws Exception { TagAttributeInfo attrs[] = _tagInfo.getAttributes(); // clear any attributes mentioned in the taglib that aren't set for (int i = 0; attrs != null && i < attrs.length; i++) { int p = getAttributeIndex(attrs[i].getName()); if (p < 0 && attrs[i].isRequired()) { throw error(L.l("required attribute '{0}' missing from <{1}>", attrs[i].getName(), getTagName())); } } boolean isDynamic = DynamicAttributes.class.isAssignableFrom(_tagClass); // fill all mentioned attributes for (int i = 0; i < _attributeNames.size(); i++) { QName attrName = _attributeNames.get(i); Object value = _attributeValues.get(i); TagAttributeInfo attribute = getAttributeInfo(attrName); if (attrs != null && attribute == null && ! isDynamic) throw error(L.l("unexpected attribute '{0}' in <{1}>", attrName.getName(), getTagName())); if (_tag.getAttribute(attrName) != null) continue;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?