javajspgenerator.java

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

JAVA
2,451
字号
		if (depend.getPath().isDirectory())	  continue;	        out.print("depend = new com.caucho.vfs.Depend(");        printPathDir(out, depend, depend.getPath().getFullPath(),		     appDir, classPath);        out.println(", " + depend.getDigest() + "L, " +                    _requireSource + ");");        out.println("com.caucho.jsp.JavaPage.addDepend(_caucho_depends, depend);");      }      else {        out.print("com.caucho.jsp.JavaPage.addDepend(_caucho_depends, ");        out.print(dependency.getJavaCreateString());        out.println(");");      }    }    if (_isCacheable && ! _isUncacheable) {      for (int i = 0; i < _cacheDepends.size(); i++) {        Depend depend = _cacheDepends.get(i);	if (depend.getPath().isDirectory())	  continue;        out.print("depend = new com.caucho.vfs.Depend(");        printPathDir(out, depend, depend.getPath().getFullPath(),		     appDir, classPath);        out.println(", \"" + depend.getDigest() + "\", " +                    _requireSource + ");");        out.println("_caucho_cacheDepends.add((Object) depend);");      }    }    out.popDepth();    out.println("}");  }  /**   * Prints an expression to lookup the path directory   */  private void printPathDir(JspJavaWriter out, Depend depend,			    String path, Path appDir, MergePath classPath)    throws IOException  {    String resinHome = CauchoSystem.getResinHome().getFullPath();    String prefix = getAppDir().getFullPath();        if (prefix.length() > 1 && ! prefix.endsWith("/"))      prefix = prefix + "/";    if (path.startsWith(prefix)) {      String subPath = path.substring(prefix.length());      Path appPathTest = appDir.lookup(subPath);      if (appPathTest.getCrc64() == depend.getPath().getCrc64()) {	out.print("appDir.lookup(\"");	out.printJavaString(subPath);	out.print("\")");	return;      }    }    ArrayList<Path> classPathList = classPath.getMergePaths();        for (int i = 0; i < classPathList.size(); i++) {      Path dir = classPathList.get(i);      prefix = dir.getFullPath();      if (! prefix.endsWith("/"))	prefix = prefix + "/";      if (prefix.equals("/"))	continue;      else if (path.startsWith(prefix)) {        String tail = path.substring(prefix.length());	if (tail.startsWith("/"))	  tail = tail.substring(1);	Path cpPath = appDir.lookup("classpath:" + tail);	if (depend.getPath().getCrc64() == cpPath.getCrc64()) {	  out.print("appDir.lookup(\"classpath:");	  out.printJavaString(tail);	  out.print("\")");	  return;	}	else {	  out.print("appDir.lookup(\"");	  out.printJavaString(depend.getPath().getURL());	  out.print("\")");	  return;	}      }    }          if (path.startsWith(resinHome + "/")) {      path = path.substring(resinHome.length() + 1);      out.print("mergePath.lookup(\"");      out.printJavaString(path);      out.print("\")");    }    else {      out.print("mergePath.lookup(\"");      out.printJavaString(depend.getPath().getURL());      out.print("\")");    }  }  /**   * Prints the tag injection.   */  private void generateInject(JspJavaWriter out) throws IOException  {    if (_topTag == null || ! _topTag.hasChildren())      return;    Iterator<TagInstance> iter = _topTag.iterator();    while (iter.hasNext()) {      TagInstance tag = iter.next();      if (tag != null)      generateTagInjectDecl(out, tag);    }        out.println();    out.println("static {");    out.pushDepth();    out.println("try {");    out.pushDepth();    iter = _topTag.iterator();    while (iter.hasNext()) {      TagInstance tag = iter.next();      if (tag != null)	generateTagInject(out, tag);    }        out.popDepth();    out.println("} catch (Exception e) {");    out.println("  e.printStackTrace();");    out.println("  throw new RuntimeException(e);");    out.println("}");    out.popDepth();    out.println("}");  }  /**   * Prints the tag injection.   */  private void generateTagInjectDecl(JspJavaWriter out, TagInstance tag)    throws IOException  {    if (tag.getAnalyzedTag() != null	&& tag.getAnalyzedTag().getHasInjection()) {      out.println("private static com.caucho.config.j2ee.InjectProgram _jsp_inject_" + tag.getId() + ";");    }    Iterator<TagInstance> iter = tag.iterator();    while (iter.hasNext()) {      TagInstance child = iter.next();      generateTagInjectDecl(out, child);    }  }  /**   * Prints the tag injection.   */  private void generateTagInject(JspJavaWriter out, TagInstance tag)    throws IOException  {    if (tag.getAnalyzedTag() != null	&& tag.getAnalyzedTag().getHasInjection()) {      out.print("_jsp_inject_" + tag.getId() + " = ");      out.println("com.caucho.config.j2ee.InjectIntrospector.introspectProgram("		  + tag.getTagClass().getName() + ".class);");    }    Iterator<TagInstance> iter = tag.iterator();    while (iter.hasNext()) {      TagInstance child = iter.next();      generateTagInject(out, child);    }  }  private void generateConstantStrings(JspJavaWriter out)    throws IOException  {    if (_strings.size() == 0)      return;    out.println();    Iterator iter = _strings.iterator();    while (iter.hasNext()) {      Object key = iter.next();      int j = _strings.get(key);      if (_ideHack)        out.println("private final char []_jsp_string" + j + ";");      else        out.println("private final static char []_jsp_string" + j + ";");    }    if (_ideHack) {      out.println("private void _jsp_init_strings() {");      out.pushDepth();    }    else {      out.println("static {");      out.pushDepth();    }        String enc = out.getWriteStream().getJavaEncoding();    if (enc == null || enc.equals("ISO8859_1"))      enc = null;    if (_config.isStaticEncoding() && enc != null) {      out.println("try {");      out.pushDepth();    }    iter = _strings.iterator();    while (iter.hasNext()) {      String text = (String) iter.next();      int j = _strings.get(text);      out.print("_jsp_string" + j + " = \"");            for (int i = 0; i < text.length(); i++) {	char ch = text.charAt(i);	switch (ch) {	case '\n':	  out.print("\\n");	  break;	case '\r':	  out.print("\\r");	  break;	case '"':	  out.print("\\\"");	  break;	case '\\':	  out.print("\\\\");	  break;	default:	  out.print(ch);	}      }      out.println("\".toCharArray();");    }    if (_config.isStaticEncoding() && enc != null) {      out.popDepth();      out.println("} catch (java.io.UnsupportedEncodingException e) {");      out.println("  e.printStackTrace();");      out.println("}");    }    out.popDepth();    out.println("}");  }  /**   * Opens a write stream to the *.java file we're generating.   *   * @param path work directory path   *   * @return the write stream   */  WriteStream openWriteStream()    throws IOException  {    Path javaPath = getGeneratedPath();        WriteStream os = javaPath.openWrite();    os.setEncoding("JAVA");        return os;  }  Path getGeneratedPath()    throws IOException  {    String name = _pkg + "." + _className;    Path dir = getJspCompiler().getClassDir().lookup(_workPath);    Path javaPath = dir.lookup(_className + ".java");        try {      javaPath.getParent().mkdirs();    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);    }    return javaPath;  }  @Override  public int uniqueId()  {    return _uniqueId++;  }    public int generateJspId()  {    return _jspId++;  }  protected void addImport(String name)  {  }  boolean hasTags()  {    // size() == 1 is the jsp: tags.    return _tagManager.hasTags();  }  /**   * Returns the tag with the given qname.   */  public TagInfo getTag(QName qname)    throws JspParseException  {    return _tagManager.getTag(qname);  }  /**   * Returns the tag with the given qname.   */  public Class getTagClass(QName qname)    throws Exception  {    return _tagManager.getTagClass(qname);  }  public Taglib addTaglib(QName qname)    throws JspParseException  {    return _tagManager.addTaglib(qname);  }  public String getSourceLines(Path source, int errorLine)  {    if (source == null || errorLine < 1)      return "";    boolean hasLine = false;    StringBuilder sb = new StringBuilder("\n\n");    ReadStream is = null;    try {      is = source.openRead();      is.setEncoding(_parseState.getPageEncoding());      int line = 0;      String text;      while ((text = is.readLine()) != null) {	line++;	if (errorLine - 2 <= line && line <= errorLine + 2) {	  sb.append(line);	  sb.append(":  ");	  sb.append(text);	  sb.append("\n");	  hasLine = true;	}      }    } catch (IOException e) {      log.log(Level.FINER, e.toString(), e);    } finally {      is.close();    }    if (hasLine)      return sb.toString();    else      return "";  }  public JspParseException error(String message)  {    JspParseException e = new JspParseException(message);    e.setErrorPage(_parseState.getErrorPage());    return e;  }  public JspParseException error(Exception e)  {    JspParseException exn = new JspParseException(e);        exn.setErrorPage(_parseState.getErrorPage());    return exn;  }  static class MethodExpr {    private String _exprString;    com.caucho.el.Expr _expr;    Class []_args;    Class _retType;    MethodExpr(String exprString,	       com.caucho.el.Expr expr, Class []args, Class retType)    {      _exprString = exprString;      _expr = expr;      _args = args;      _retType = retType;    }    String getExprString()    {      return _exprString;    }    com.caucho.el.Expr getExpr()    {      return _expr;    }    Class []getArgs()    {      return _args;    }    Class getReturnType()    {      return _retType;    }  }  static class ValueExpr {    private String _exprString;    com.caucho.el.Expr _expr;    Class _retType;    ValueExpr(String exprString, com.caucho.el.Expr expr, Class retType)    {      _exprString = exprString;      _expr = expr;      _retType = retType;    }    String getExpressionString()    {      return _exprString;    }    com.caucho.el.Expr getExpr()    {      return _expr;    }    Class getReturnType()    {      return _retType;    }  }  static {    _primitives = new HashMap<String,String>();    _primitives.put("boolean", "boolean");    _primitives.put("byte", "byte");    _primitives.put("short", "short");    _primitives.put("char", "char");    _primitives.put("int", "int");    _primitives.put("long", "long");    _primitives.put("float", "float");    _primitives.put("double", "double");        _primitiveClasses = new HashMap<String,Class>();    _primitiveClasses.put("boolean", boolean.class);    _primitiveClasses.put("byte", byte.class);    _primitiveClasses.put("short", short.class);    _primitiveClasses.put("char", char.class);    _primitiveClasses.put("int", int.class);    _primitiveClasses.put("long", long.class);    _primitiveClasses.put("float", float.class);    _primitiveClasses.put("double", double.class);  }}

⌨️ 快捷键说明

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