generictag.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,024 行 · 第 1/2 页
JAVA
1,024 行
boolean isFragment = false; if (attribute != null) { isFragment = (attribute.isFragment() || attribute.getTypeName().equals(JspFragment.class.getName())); } if (value instanceof JspAttribute && ((JspAttribute) value).isJspFragment()) isFragment = true; generateSetAttribute(out, name, attrName, value, attribute == null || attribute.canBeRequestTime(), isFragment, attribute); } } private TagAttributeInfo getAttributeInfo(QName attrName) { TagAttributeInfo attrs[] = _tagInfo.getAttributes(); int j = 0; for (j = 0; attrs != null && j < attrs.length; j++) { if (isNameMatch(attrs[j].getName(), attrName)) return attrs[j]; } return null; } private int getAttributeIndex(String name) { for (int i = 0; i < _attributeNames.size(); i++) { QName attrName = _attributeNames.get(i); if (isNameMatch(name, attrName)) return i; } return -1; } private boolean isNameMatch(String defName, QName attrName) { if (defName.equals(attrName.getName())) { return true; } else if (defName.equals(attrName.getLocalName()) && attrName.getPrefix().equals(getQName().getPrefix())) { return true; } else return false; } /** * Sets an attribute for a tag * * @param info the tag's introspected information * @param name the tag's Java variable name * @param attrName the attribute name to set * @param value the new value of the tag. */ void generateSetAttribute(JspJavaWriter out, String name, QName attrName, Object value, boolean allowRtexpr, boolean isFragment, TagAttributeInfo attrInfo) throws Exception { Method method = getAttributeMethod(attrName); boolean isDynamic = DynamicAttributes.class.isAssignableFrom(_tagClass); if (method != null) { // jsp/18cq if (Modifier.isStatic(method.getModifiers())) throw error(L.l("attribute '{0}' may not be a static method.", method.getName())); generateSetParameter(out, name, value, method, allowRtexpr, "pageContext", isFragment, attrInfo); } else if (! isDynamic) { throw error(L.l("attribute '{0}' in tag '{1}' has no corresponding set method in tag class '{2}'", attrName.getName(), getTagName(), _tagClass.getName())); } else if (isFragment) { String uri = attrName.getNamespaceURI(); String local = attrName.getLocalName(); out.print(name + ".setDynamicAttribute("); if (uri == null) out.print("null, "); else out.print("\"" + escapeJavaString(uri) + "\", "); JspFragmentNode frag = (JspFragmentNode) value; out.print("\"" + escapeJavaString(local) + "\", "); out.print(frag.generateValue()); out.println(");"); } else { String uri = attrName.getNamespaceURI(); String local = attrName.getLocalName(); out.print(name + ".setDynamicAttribute("); if (uri == null) out.print("null, "); else out.print("\"" + escapeJavaString(uri) + "\", "); out.print("\"" + escapeJavaString(local) + "\", "); out.print(generateRTValue(Object.class, value)); out.println(");"); } } private Method getAttributeMethod(QName attrName) throws Exception { Method method = null; try { BeanInfo info = Introspector.getBeanInfo(_tagClass); if (info != null) method = BeanUtil.getSetMethod(info, attrName.getLocalName()); if (method != null) return method; } catch (Exception e) { log.log(Level.FINER, e.toString(), e); } /* try { method = BeanUtil.getSetMethod(_tagClass, attrName.getLocalName()); if (method != null) return method; } catch (Throwable e) { log.log(Level.FINER, e.toString(), e); } */ return method; } /** * Returns true if there is a tag variable declaration matching the scope. */ protected boolean hasVarDeclaration(int scope) throws Exception { for (int i = 0; _varInfo != null && i < _varInfo.length; i++) { VariableInfo var = _varInfo[i]; if (var != null && var.getScope() == scope) return true; } return false; } /** * Prints a tag variable declaration. Only the variables matching the * scope will be printed. * * @param out the stream to the java code. * @param scope the variable scope to print */ protected void printVarDeclaration(JspJavaWriter out, int scope) throws Exception { for (int i = 0; _varInfo != null && i < _varInfo.length; i++) { VariableInfo var = _varInfo[i]; if (var != null) { printVarDeclare(out, scope, var); printVarAssign(out, scope, var); } } } /** * Prints a tag variable declaration. Only the variables matching the * scope will be printed. * * @param out the stream to the java code. * @param scope the variable scope to print */ protected void printVarDeclare(JspJavaWriter out, int scope) throws Exception { for (int i = 0; _varInfo != null && i < _varInfo.length; i++) { VariableInfo var = _varInfo[i]; if (var != null) printVarDeclare(out, scope, var); } } /** * Prints a tag variable declaration. Only the variables matching the * scope will be printed. * * @param out the stream to the java code. * @param scope the variable scope to print */ protected void printVarAssign(JspJavaWriter out, int scope) throws Exception { for (int i = 0; _varInfo != null && i < _varInfo.length; i++) { VariableInfo var = _varInfo[i]; if (var != null) printVarAssign(out, scope, var); } } /** * Returns the VariableInfo corresponding the to tag vars and the tag * data. Mainly, this means looking up the variable names from the * attributes for the name-from-attribute. * * @param tagVars the implicit tag variables for the tag * @param tagData the parsed tag attributes * * @return an array of filled VariableInfo */ protected VariableInfo []fillVariableInfo(TagVariableInfo []tagVars, TagData tagData) throws JspParseException { if (tagVars == null) return null; VariableInfo []vars = new VariableInfo[tagVars.length]; for (int i = 0; i < tagVars.length; i++) { TagVariableInfo tagVar = tagVars[i]; String name = null; if (tagVar.getNameGiven() != null) name = tagVar.getNameGiven(); else { String attributeName = tagVar.getNameFromAttribute(); name = tagData.getAttributeString(attributeName); if (name == null || "".equals(name) || "null".equals(name)) continue; } vars[i] = new VariableInfo(name, tagVar.getClassName(), tagVar.getDeclare(), tagVar.getScope()); } return vars; } /** * Prints a tag variable declaration. Only the variables matching the * scope will be printed. * * @param out the stream to the java code. * @param scope the variable scope to print */ protected void printVarDeclare(JspJavaWriter out, int scope, VariableInfo var) throws Exception { if (! _gen.hasScripting() || var == null || var.getVarName() == null || "".equals(var.getVarName()) || "null".equals(var.getVarName())) return; if (var.getScope() == scope || var.getScope() == VariableInfo.AT_BEGIN) { if (var.getVarName() == null) throw error(L.l("tag variable expects a name")); String className = var.getClassName(); if (className == null || "".equals(className) || "null".equals(className)) className = DEFAULT_VAR_TYPE; /* if (var.getClassName() == null) throw error(L.l("tag variable '{0}' expects a classname", var.getVarName())); */ validateVarName(var.getVarName()); // jsp/107r if (var.getDeclare() && var.getScope() == scope && (var.getScope() == VariableInfo.NESTED && hasScripting() || var.getScope() == VariableInfo.AT_BEGIN) && ! varAlreadyDeclared(var.getVarName())) { validateClass(className, var.getVarName()); out.println(className + " " + var.getVarName() + ";"); } } } /** * Prints a tag variable declaration. Only the variables matching the * scope will be printed. * * @param out the stream to the java code. * @param scope the variable scope to print */ protected void printVarAssign(JspJavaWriter out, int scope, VariableInfo var) throws Exception { if ("".equals(var.getVarName()) || "null".equals(var.getVarName())) return; if (var.getScope() == scope || var.getScope() == VariableInfo.AT_BEGIN) { if (var.getVarName() == null) throw error(L.l("tag variable expects a name")); String className = var.getClassName(); if (className == null || className.equals("null")) className = DEFAULT_VAR_TYPE; /* if (var.getClassName() == null) throw error(L.l("tag variable '{0}' expects a classname", var.getVarName())); */ validateVarName(var.getVarName()); if (! _gen.hasScripting()) { } else if (var.getScope() != VariableInfo.NESTED || hasScripting()) { out.setLocation(_filename, _startLine); out.print(var.getVarName() + " = "); String v = "pageContext.findAttribute(\"" + var.getVarName() + "\")"; convertParameterValue(out, className, v); out.println(";"); } try { _gen.addBeanClass(var.getVarName(), className); } catch (Exception e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw error(L.l("'{0}' is an unknown class for tag variable '{1}' in <{2}>", className, var.getVarName(), getTagInfo().getTagName()), cause); } } } private void validateVarName(String name) throws JspParseException { if (! Character.isJavaIdentifierStart(name.charAt(0))) throw error(L.l("tag variable '{0}' is an illegal Java identifier.", name)); for (int i = 0; i < name.length(); i++) { if (! Character.isJavaIdentifierPart(name.charAt(i))) throw error(L.l("tag variable '{0}' is an illegal Java identifier.", name)); } } /** * Returns true if the variable has been declared. */ private boolean varAlreadyDeclared(String varName) { if (_gen.isDeclared(varName)) return true; for (JspNode node = getParent(); node != null; node = node.getParent()) { if (! (node instanceof GenericTag)) continue; if (node instanceof JspFragmentNode) break; GenericTag tag = (GenericTag) node; VariableInfo []varInfo = tag.getVarInfo(); for (int i = 0; varInfo != null && i < varInfo.length; i++) { if (varInfo[i] == null) continue; else if (varInfo[i].getVarName().equals(varName)) return true; } } return false; } /** * Returns true if the tag instance has been declared */ protected boolean isDeclared() { if (! _gen.getRecycleTags()) return false; JspNode parent = getParent(); if (! (parent instanceof JspRoot) && ! (parent instanceof JspTop) && ! (parent instanceof GenericTag) && ! (parent instanceof JspAttribute)) return false; boolean isDeclared = false; ArrayList<JspNode> siblings = getParent().getChildren(); for (int i = 0; i < siblings.size(); i++) { JspNode node = siblings.get(i); if (node == this) { return isDeclared; } if (hasScriptlet(node)) { return false; } if (node instanceof GenericTag) { GenericTag customTag = (GenericTag) node; if (customTag.getTag() == getTag()) isDeclared = true; } } return isDeclared; } /** * Returns true if the node or one of its children is a scriptlet */ protected boolean hasScriptlet(JspNode node) { if (node instanceof JspScriptlet || node instanceof JspExpression) return true; ArrayList<JspNode> children = node.getChildren(); if (children == null) return false; for (int i = 0; i < children.size(); i++) { JspNode child = children.get(i); if (hasScriptlet(child)) return true; } return false; } /** * Checks that the given class is a valid variable class. */ protected void validateClass(String className, String varName) throws JspParseException { try { if (_primTypes.contains(className)) return; else if (className.endsWith("[]")) { validateClass(className.substring(0, className.length() - 2), varName); return; } Class cl = _gen.getBeanClass(className); } catch (ClassNotFoundException e) { throw error(L.l("'{0}' is an unknown class for tag variable '{1}'.", className, varName)); } } static { _primTypes.add("boolean"); _primTypes.add("byte"); _primTypes.add("short"); _primTypes.add("int"); _primTypes.add("long"); _primTypes.add("float"); _primTypes.add("double"); _primTypes.add("char"); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?