javajspgenerator.java

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

JAVA
2,451
字号
    out.println("            TagState _jsp_state)");    out.println("  throws Throwable");    out.println("{");    out.pushDepth();    out.println("javax.servlet.jsp.JspWriter out = pageContext.getOut();");    out.println("final javax.el.ELContext _jsp_env = pageContext.getELContext();");    out.println("javax.servlet.ServletConfig config = getServletConfig();");    out.println("javax.servlet.Servlet page = this;");    if (_parseState.isErrorPage()) {      out.println("java.lang.Throwable exception = ((com.caucho.jsp.PageContextImpl) pageContext).getThrowable();");    }    out.println("javax.servlet.jsp.tagext.JspTag _jsp_parent_tag = null;");    generateContentType(out);        _rootNode.generatePrologue(out);    out.println();  }  /**   * Generates the content type of the page.   */  private void generateContentType(JspJavaWriter out)    throws IOException  {    String encoding = Encoding.getMimeName(_parseState.getCharEncoding());    if (encoding == null)      encoding = Encoding.getMimeName(_parseState.getPageEncoding());    if (encoding == null)      encoding = Encoding.getMimeName(CharacterEncoding.getLocalEncoding());    /*    if ("ISO-8859-1".equals(encoding))      encoding = null;    */    String contentType = _parseState.getContentType();    if (contentType != null && contentType.equals("text/html"))      contentType = null;    out.print("response.setContentType(\"");    if (contentType == null)      out.print("text/html");    else {      out.printJavaString(contentType);    }    out.println("\");");    if (encoding == null && isXml())      encoding = "UTF-8";    if (encoding == null) {      // server/1204    }    else if (contentType == null || contentType.indexOf("charset") < 0) {      out.print("response.setCharacterEncoding(\"");      if (encoding != null)	out.printJavaString(encoding);      else	out.printJavaString("iso-8859-1");      out.println("\");");    }        if (encoding != null)      out.println("request.setCharacterEncoding(\"" + encoding + "\");");  }  private void printTry(JspJavaWriter out) throws IOException  {    out.println("try {");    out.pushDepth();    //    out.println("_caucho_init_tags(pageContext, _jsp_tags);");  }  public int addString(String string)  {    int index = _strings.get(string);    if (index < 0) {      index = _strings.size();      _strings.put(string, index);    }    return index;  }  /**   * Saves a bean's class for later introspection.   *   * @param id the bean id   * @param typeName the bean's type   */  public void addBeanClass(String id, String typeName)    throws Exception  {    if (_classes == null)      _classes = new HashMap<String,Class>();    try {      if (_primitives.get(typeName) != null)        return;      Class cl = getBeanClass(typeName);      if (cl == null)        throw error(L.l("Can't find class '{0}'",                        typeName));      _classes.put(id, cl);    } catch (CompileClassNotFound e) {      log.log(Level.WARNING, e.toString(), e);      throw error(L.l("Can't find class '{0}'\n{1}",		      typeName, e.getMessage()));    } catch (ClassNotFoundException e) {      log.log(Level.FINE, e.toString(), e);      throw error(L.l("Can't find class '{0}'", typeName));    }  }  /**   * Loads a bean based on the class name.   */  public Class getBeanClass(String typeName)    throws ClassNotFoundException  {    // Generics parameters should be removed and only the base class loaded    int p = typeName.indexOf('<');    if (p > 0)      return getBeanClass(typeName.substring(0, p));        // Arrays need to use Array.newInstance(cl, new int[]);    p = typeName.indexOf('[');    if (p > 0) {      Class cl = getBeanClass(typeName.substring(0, p));      int count = 0;      for (int i = 0; i < typeName.length(); i++)        if (typeName.charAt(i) == '[')          count++;      int []dims = new int[count];      for (int i = 0; i < count; i++)        dims[i] = 1;      Object obj = Array.newInstance(cl, dims);            return obj.getClass();    }    Class cl = loadBeanClass(typeName);    if (cl != null)      return cl;    // Inner classes need rewriting Foo.Bar -> Foo$Bar    int i = typeName.lastIndexOf('.');    for (; i >= 0; i = typeName.lastIndexOf('.', i - 1)) {      String mainClassName = typeName.substring(0, i);      Class mainClass = loadBeanClass(mainClassName);            typeName = mainClassName + '$' + typeName.substring(i + 1);            if (mainClass != null)        return getBeanClass(typeName);    }    return null;  }    Class loadBeanClass(String typeName)  {    Class cl = _primitiveClasses.get(typeName);    if (cl != null)      return cl;    try {      return CauchoSystem.loadClass(typeName);    } catch (CompileClassNotFound e) {      log.log(Level.FINE, e.toString(), e);    } catch (ClassNotFoundException e) {    }    // qualified names don't use the imports    if (typeName.indexOf('.') >= 0)      return null;    ArrayList<String> imports = _parseState.getImportList();    for (int i = 0; i < imports.size(); i++) {      String pkg = imports.get(i);      String fullName = null;      if (pkg.endsWith("." + typeName))        fullName = pkg;      else if (pkg.endsWith(".*"))        fullName = pkg.substring(0, pkg.length() - 1) + typeName;      else        continue;          try {        return CauchoSystem.loadClass(fullName);      } catch (CompileClassNotFound e) {        log.log(Level.WARNING, e.toString(), e);      } catch (ClassNotFoundException e) {      }    }    return null;  }  public Class getClass(String id)  {    if (_classes == null)      return null;    return _classes.get(id);  }  protected void generatePageFooter(JspJavaWriter out) throws IOException  {    out.popDepth();    out.println("}");  }  /**   * Completes the generated class: closing the main method, generating   * the dependencies and generating the constant strings.   *   * @param doc the XML document representing the JSP page.   */  protected void generateClassFooter(JspJavaWriter out) throws Exception  {    // fragments must be first because they may create others.    generateFragments(out);        generateDepends(out);        generateTags(out);    //generateTags may still add to _value(Expr|Method)List    generateInit(out);    generateExprs(out);    generateXPath(out);    generateConstantStrings(out);    out.popDepth();    out.println("}");  }  public int addFragment(JspFragmentNode node)  {    int index = _fragmentList.indexOf(node);    if (index >= 0)      return index;        _fragmentList.add(node);        return _fragmentList.size() - 1;  }  public JspFragmentNode getFragment(int index)  {    return _fragmentList.get(index);  }    public com.caucho.el.Expr genExpr(String value)    throws JspParseException, ELException  {    JspELParser parser = new JspELParser(_elContext, value);    return parser.parse();  }  /**   * Adds an expression to the expression list.   */  public int addExpr(String expr)    throws JspParseException, ELException  {    genExpr(expr);        int index = _exprList.indexOf(expr);    if (index >= 0)      return index;    index = _exprList.size();    _exprList.add(expr);    return index;  }    /**   * Adds an expression to the expression list.   */  public int addValueExpr(String value, String type)    throws JspParseException, ELException  {    JspELParser parser = new JspELParser(_elContext, value);    com.caucho.el.Expr expr = parser.parse();    int index = _valueExprList.indexOf(expr);    if (index >= 0)      return index;    index = _valueExprList.size();    try {      if (type == null || type.equals(""))	_valueExprList.add(new ValueExpr(value, expr, Object.class));      else {	Class cl = getBeanClass(type);	if (cl == null)	  throw new NullPointerException(type);		_valueExprList.add(new ValueExpr(value, expr, cl));      }    } catch (ClassNotFoundException e) {      throw new ELException(e);    }    return index;  }    /**   * Adds an expression to the expression list.   */  public int addMethodExpr(String value, String sigString)    throws JspParseException, ELException  {    JspELParser parser = new JspELParser(_elContext, value);    com.caucho.el.Expr expr = parser.parse();    Class retType = void.class;    Class []args = new Class[0];    try {      if (sigString != null && ! sigString.equals("")) {	Signature sig = new Signature(sigString);	String []types = sig.getParameterTypes();	args = new Class[types.length];	for (int i = 0; i < types.length; i++) {	  args[i] = getBeanClass(types[i]);	}        if (sig.getReturnType() == null)          throw error(L.l("deferredMethod signature '{0}' needs a return type.",                          sigString));	retType = getBeanClass(sig.getReturnType());      }    } catch (ClassNotFoundException e) {      throw new ELException(e);    }    MethodExpr methodExpr = new MethodExpr(value, expr, args, retType);    int index = _methodExprList.indexOf(methodExpr);    if (index >= 0)      return index;    index = _methodExprList.size();    _methodExprList.add(methodExpr);    return index;  }  /**   * out.Prints the expressions   */  private void generateExprs(JspJavaWriter out) throws IOException  {    for (int i = 0; i < _exprList.size(); i++) {      String expr = _exprList.get(i);            out.println("private static com.caucho.el.Expr _caucho_expr_" + i + ";");      /*      out.print("  ");      expr.printCreate(out.getWriteStream());      out.println(";");      */    }        for (int i = 0; i < _valueExprList.size(); i++) {      ValueExpr expr = _valueExprList.get(i);      String exprType = "ObjectValueExpression";      Class retType = expr.getReturnType();      if (String.class.equals(retType))	exprType = "StringValueExpression";      else if (Byte.class.equals(retType)	       || byte.class.equals(retType))	exprType = "ByteValueExpression";      else if (Short.class.equals(retType)	       || short.class.equals(retType))	exprType = "ShortValueExpression";      else if (Integer.class.equals(retType)	       || int.class.equals(retType))	exprType = "IntegerValueExpression";      else if (Long.class.equals(retType)	       || long.class.equals(retType))	exprType = "LongValueExpression";      else if (Float.class.equals(retType)	       || long.class.equals(retType))	exprType = "FloatValueExpression";      else if (Double.class.equals(retType)	       || double.class.equals(retType))	exprType = "DoubleValueExpression";      else if (Boolean.class.equals(retType)	       || boolean.class.equals(retType))	exprType = "BooleanValueExpression";      else if (Character.class.equals(retType)	       || char.class.equals(retType))	exprType = "CharacterValueExpression";      else if (BigInteger.class.equals(retType))	exprType = "BigIntegerValueExpression";      else if (BigDecimal.class.equals(retType))	exprType = "BigDecimalValueExpression";            out.println("private static javax.el.ValueExpression _caucho_value_expr_" + i + ";");    }        for (int i = 0; i < _methodExprList.size(); i++) {      MethodExpr expr = _methodExprList.get(i);            out.println("private static javax.el.MethodExpression _caucho_method_expr_" + i + ";");    }  }  /**   * Adds an expression to the expression list.   */  public String addXPathExpr(String value, NamespaceContext ns)    throws JspParseException, XPathParseException  {    return addXPathExpr(XPath.parseExpr(value, ns));  }  /**   * Adds an expression to the expression list.   */  public String addXPathExpr(com.caucho.xpath.Expr expr)    throws JspParseException  {    int index = _xpathExprList.indexOf(expr);    if (index >= 0)      return "_caucho_xpath_" + index;    index = _xpathExprList.size();    _xpathExprList.add(expr);    return "_caucho_xpath_" + index;  }  /**   * out.Prints the expressions   */  private void generateXPath(JspJavaWriter out) throws IOException  {    if (_xpathExprList.size() == 0)      return;        for (int i = 0; i < _xpathExprList.size(); i++) {      com.caucho.xpath.Expr expr = _xpathExprList.get(i);            out.println("private static com.caucho.xpath.Expr _caucho_xpath_" + i + ";");    }    out.println("static {");    out.pushDepth();    out.println("try {");    out.pushDepth();        for (int i = 0; i < _xpathExprList.size(); i++) {      com.caucho.xpath.Expr expr =  _xpathExprList.get(i);            out.print("_caucho_xpath_" + i + " =");      out.println(" com.caucho.xpath.XPath.parseExpr(\"" + expr + "\");");    }

⌨️ 快捷键说明

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