javataggenerator.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 929 行 · 第 1/2 页

JAVA
929
字号
/* * 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 SoftwareFoundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.jsp.java;import com.caucho.config.types.Signature;import com.caucho.jsp.JspParseException;import com.caucho.jsp.ParseTagManager;import com.caucho.jsp.cfg.TldAttribute;import com.caucho.jsp.cfg.TldTag;import com.caucho.jsp.cfg.TldVariable;import com.caucho.log.Log;import com.caucho.util.L10N;import javax.servlet.jsp.tagext.TagInfo;import javax.servlet.jsp.tagext.TagLibraryInfo;import java.io.IOException;import java.util.ArrayList;import java.util.HashSet;import java.util.logging.Level;import java.util.logging.Logger;/** * Generates JSP code.  JavaGenerator, JavaScriptGenerator, and * StaticGenerator specialize the JspGenerator for language-specific * requirements. * * <p>JspParser parses the JSP file into an XML-DOM tree.  JspGenerator * generates code from that tree. */public class JavaTagGenerator extends JavaJspGenerator {  static final L10N L = new L10N(JavaTagGenerator.class);  static final Logger log = Log.open(JavaTagGenerator.class);  private static HashSet<String> _reserved    = new HashSet<String>();  private String _description = null;  private String _displayName = null;  private String _smallIcon = null;  private String _largeIcon = null;  private String _example = null;  private String _bodyContent = null;  private String _dynamicAttributes = null;    private ArrayList<TldAttribute> _attributes = new ArrayList<TldAttribute>();  private ArrayList<TldVariable> _variables = new ArrayList<TldVariable>();  public JavaTagGenerator(ParseTagManager tagManager)  {    super(tagManager);    setOmitXmlDeclaration(true);  }  public void init()  {    super.init();        setOmitXmlDeclaration(true);  }  /**   * Returns true if the XML declaration should be ignored.   */  /*  boolean isOmitXmlDeclaration()  {    // tags always omit the declaration    return true;  }  */  public void setDescription(String description)  {    _description = description;  }  public String getDescription()  {    return _description;  }  public void setDisplayName(String displayName)  {    _displayName = displayName;  }  public String getDisplayName()  {    return _displayName;  }  public void setSmallIcon(String smallIcon)  {    _smallIcon = smallIcon;  }  public String getSmallIcon()  {    return _smallIcon;  }  public void setLargeIcon(String largeIcon)  {    _largeIcon = largeIcon;  }  public String getLargeIcon()  {    return _largeIcon;  }  public void setExample(String example)  {    _example = example;  }  public String getExample()  {    return _example;  }  /**   * Sets the body content.   */  public void setBodyContent(String bodyContent)  {    _bodyContent = bodyContent;  }  /**   * Gets the body content.   */  public String getBodyContent()  {    return _bodyContent;  }  /**   * Sets the name of the dynamic attributes map   */  public void setDynamicAttributes(String dynamicAttributes)  {    _dynamicAttributes = dynamicAttributes;  }  /**   * Gets the body content.   */  public String getDynamicAttributes()  {    return _dynamicAttributes;  }  /**   * Adds an attribute.   */  public void addAttribute(TldAttribute attribute)  {    _attributes.add(attribute);  }  /**   * Returns the attributes.   */  public ArrayList<TldAttribute> getAttributes()  {    return _attributes;  }  /**   * Finds an attribute.   */  public TldAttribute findAttribute(String name)  {    for (int i = 0; i < _attributes.size(); i++) {      TldAttribute attr = _attributes.get(i);      if (name.equals(attr.getName()))	return attr;    }    return null;  }  /**   * Adds a variable.   */  public void addVariable(TldVariable var)  {    _variables.add(var);  }  /**   * Finds a variable.   */  public TldVariable findVariable(String name)  {    for (int i = 0; i < _variables.size(); i++) {      TldVariable var = _variables.get(i);      // jsp/1071      if (name.equals(var.getNameGiven())	  || name.equals(var.getAlias()))	return var;    }    return null;  }  /**   * Returns the variables.   */  public ArrayList<TldVariable> getVariables()  {    return _variables;  }  public boolean isTag()  {    return true;  }  /**   * Returns true for XML.   */  boolean isXml()  {    return _parseState.isXml();  }  /**   * Generates the Java code.   */  protected void generate(JspJavaWriter out)    throws Exception  {    out.setLineMap(_lineMap);        generateClassHeader(out);    generateAttributes(out);    if (_dynamicAttributes != null)      generateDynamicAttributes(out);    generateDoTag(out, _rootNode);    if (isStaticDoTag())      generateStaticDoTag(out, _rootNode);    generateTagInfo(out);        generateClassFooter(out);  }  protected boolean isStaticDoTag()  {    return ! hasScripting();  }  /**   * Generates the class header.   *   * @param doc the XML document representing the JSP page.   */  protected void generateClassHeader(JspJavaWriter out)    throws IOException, JspParseException  {    out.println("/*");    out.println(" * JSP-Tag generated by " + com.caucho.Version.FULL_VERSION);    out.println(" */" );    out.println();    if (_pkg != null && ! _pkg.equals(""))      out.println("package " + _pkg + ";");    out.println("import javax.servlet.*;");    out.println("import javax.servlet.jsp.*;");    out.println("import javax.servlet.http.*;");    fillSingleTaglibImports();    ArrayList<String> imports = _parseState.getImportList();    for (int i = 0; i < imports.size(); i++) {      String name = imports.get(i);      out.print("import ");      out.print(name);      out.println(";");    }    _parseState.addImport("javax.servlet.*");    _parseState.addImport("javax.servlet.jsp.*");    _parseState.addImport("javax.servlet.http.*");    _parseState.addImport("java.lang.*");    out.println();    out.print("public class ");    out.print(_className);    if (hasScripting())      out.print(" extends com.caucho.jsp.java.JspTagSupport");    else      out.print(" extends com.caucho.jsp.java.JspTagFileSupport");    if (_dynamicAttributes != null)      out.print(" implements javax.servlet.jsp.tagext.DynamicAttributes");    out.println(" {");    out.pushDepth();    out.println("private final java.util.HashMap<String,java.lang.reflect.Method> _jsp_functionMap = new java.util.HashMap<String,java.lang.reflect.Method>();");    out.println("private boolean _caucho_isDead;");    for (int i = 0; i < _declarations.size(); i++) {      JspDeclaration decl = _declarations.get(i);      out.println();      decl.generateDeclaration(out);    }  }  /**   * Generates the attribute definitions.   *   * @param out the writer to the .java source   */  protected void generateAttributes(JspJavaWriter out)    throws IOException, JspParseException  {    for (int i = 0; i < _attributes.size(); i++) {      TldAttribute attr = _attributes.get(i);      String name = attr.getName();      String upperName;      char ch = name.charAt(0);      upperName = Character.toUpperCase(ch) + name.substring(1);      Class cl = attr.getType();      if (cl == null)        cl = String.class;      String type = cl.getName();      String isSetName = "_jsp_" + name + "_isSet";      String fieldName = toFieldName(name);      out.println();      out.print("private ");      out.printClass(cl);      out.println(" " + fieldName + ";");      out.println("private boolean " + isSetName + ";");            out.println();      out.print("public void set" + upperName + "(");      out.printClass(cl);      out.println(" value)");      out.println("{");      out.pushDepth();      out.println("this." + isSetName + " = true;");      out.println("this." + fieldName + " = value;");      out.popDepth();      out.println("}");      /*      // jsp/101f      out.println();      out.println("public " + type + " get" + upperName + "()");      out.println("{");      out.pushDepth();      out.println("return " + fieldName + ";");      out.popDepth();      out.println("}");      */    }  }  /**   * Generates the attribute definitions.   *   * @param out the writer to the .java source   */  protected void generateDynamicAttributes(JspJavaWriter out)    throws IOException, JspParseException  {    String dyn = toFieldName(_dynamicAttributes);    out.println();    out.println("java.util.HashMap " + dyn + " = new java.util.HashMap();");    out.println();    out.println("public void setDynamicAttribute(String uri, String localName, Object value)");    out.println("  throws javax.servlet.jsp.JspException");    out.println("{");    out.println("  if (uri == null || \"\".equals(uri))");    out.println("    " + dyn + ".put(localName, value);");    out.println("}");  }  /**   * Prints the _jspService header   */  protected void generateDoTag(JspJavaWriter out, JspNode node)    throws Exception  {    out.println();    out.println("public void doTag()");    out.println("  throws javax.servlet.jsp.JspException, java.io.IOException");    out.println("{");    out.pushDepth();    out.println("javax.servlet.jsp.JspContext _jsp_parentContext = getJspContext();");    out.println("com.caucho.jsp.PageContextWrapper pageContext = com.caucho.jsp.PageContextWrapper.create(_jsp_parentContext);");    // jsp/1056    out.println("setJspContext(pageContext);");    if (hasScripting()) {      out.println("javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest) pageContext.getRequest();");      out.println("javax.servlet.http.HttpServletResponse response = (javax.servlet.http.HttpServletResponse) pageContext.getResponse();");      out.println("javax.servlet.http.HttpSession session = pageContext.getSession();");      out.println("javax.servlet.ServletContext application = pageContext.getServletContext();");      out.println("javax.servlet.ServletConfig config = pageContext.getServletConfig();");    }    out.println("com.caucho.jsp.PageContextWrapper jspContext = pageContext;");    out.println("javax.el.ELContext _jsp_env = pageContext.getELContext();");    out.println("javax.servlet.jsp.JspWriter out = pageContext.getOut();");    generateTagAttributes(out);    if (hasScripting())      generatePrologue(out);    out.println("try {");    out.pushDepth();         if (hasScripting()) {      out.println("TagState _jsp_state = new TagState();");      out.println("javax.servlet.jsp.tagext.JspTag _jsp_parent_tag = null;");      node.generate(out);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?