📄 jspcompiler.java
字号:
out.println("\tprivate static java.lang.String __valueOf(long l) { return java.lang.String.valueOf(l); }"); out.println("\tprivate static java.lang.String __valueOf(float f) { return java.lang.String.valueOf(f); }"); out.println("\tprivate static java.lang.String __valueOf(double d) { return java.lang.String.valueOf(d); }"); out.println(); // generate bean functions out.println("\tprivate void __beanError(javax.servlet.http.HttpServletResponse response, java.lang.String msg) throws java.io.IOException {"); out.println("\t\tresponse.setStatus(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR, \"JSP run-time error\");"); out.println("\t\tresponse.setContentType(\"text/html\");"); out.println("\t\tjava.io.PrintWriter errOut = response.getWriter();"); out.println("\t\terrOut.println(\"<HTML><HEAD><TITLE>\" + javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR + \" JSP run-time error</TITLE></HEAD><BODY>\" +"); out.println("\t\t\t\"<H2>\" + javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR + \" JSP run-time error</H2>\" +"); out.println("\t\t\t\"The JSP page you requested could not be served because the following error(s) occured:<BR><PRE>\");"); out.println("\t\terrOut.println(msg);"); out.println("\t\terrOut.println(\"</PRE></BODY></HTML>\");"); out.println("\t\terrOut.close();"); out.println("\t}"); out.println(); out.println("\tprivate boolean __beanSetProperty(java.lang.Object bean, java.beans.PropertyDescriptor[] pds, java.lang.String key, java.lang.String value) {"); out.println("\t\tjava.lang.reflect.Method meth;"); out.println("\t\tfor(int i = 0; i < pds.length; i++) {"); out.println("\t\t\tif(pds[i].getName().equals(key) && !pds[i].isHidden()) {"); out.println("\t\t\t\tmeth = pds[i].getWriteMethod();"); out.println("\t\t\t\tif(meth != null) {"); out.println("\t\t\t\t\ttry {"); out.println("\t\t\t\t\t\tmeth.invoke(bean, new Object[]{value});"); out.println("\t\t\t\t\t\treturn true;"); out.println("\t\t\t\t\t} catch(java.lang.IllegalAccessException iaccexc) {"); out.println("\t\t\t\t\t\treturn false;"); out.println("\t\t\t\t\t} catch(java.lang.IllegalArgumentException iargexc) {"); out.println("\t\t\t\t\t\treturn false;"); out.println("\t\t\t\t\t} catch(java.lang.reflect.InvocationTargetException itexc) {"); out.println("\t\t\t\t\t\treturn false;"); out.println("\t\t\t\t\t}"); out.println("\t\t\t\t}"); out.println("\t\t\t}"); out.println("\t\t}"); out.println("\t\treturn false;"); out.println("\t}"); out.println(); out.println("\tprivate void __beanIntrospect(java.lang.Object bean, java.beans.PropertyDescriptor[] pds, javax.servlet.http.HttpServletRequest request) {"); out.println("\t\tjava.lang.reflect.Method meth;"); out.println("\t\tjava.lang.String value;"); out.println("\t\tfor(int i = 0; i < pds.length; i++) {"); out.println("\t\t\tif(!pds[i].isHidden()) {"); out.println("\t\t\t\tvalue = request.getParameter(pds[i].getName());"); out.println("\t\t\t\tif(value != null) {"); out.println("\t\t\t\t\tmeth = pds[i].getWriteMethod();"); out.println("\t\t\t\t\tif(meth != null) {"); out.println("\t\t\t\t\t\ttry {"); out.println("\t\t\t\t\t\t\tmeth.invoke(bean, new Object[]{value});"); out.println("\t\t\t\t\t\t} catch(java.lang.IllegalAccessException iaccexc) {"); out.println("\t\t\t\t\t\t\t//"); out.println("\t\t\t\t\t\t} catch(java.lang.IllegalArgumentException iargexc) {"); out.println("\t\t\t\t\t\t\t//"); out.println("\t\t\t\t\t\t} catch(java.lang.reflect.InvocationTargetException itexc) {"); out.println("\t\t\t\t\t\t\t//"); out.println("\t\t\t\t\t\t}"); out.println("\t\t\t\t\t}"); out.println("\t\t\t\t}"); out.println("\t\t\t}"); out.println("\t\t}"); out.println("\t}"); out.println(); // generate dependency check code out.println("\tpublic static final int __compilerVersionNr = " + COMPILER_VERSION_NR + ";"); out.println(); out.println("\tprivate static java.lang.Object[] __dependencies = new java.lang.Object[]{"); e = dependencies.elements(); for(;;) { f = (File) e.nextElement(); l = (Long) e.nextElement(); out.println("\t\tnew java.io.File(\"" + javaStringEncode(f.toString()) + "\"),"); out.print("\t\tnew java.lang.Long(" + l + "L)"); if(e.hasMoreElements()) { out.println(","); } else { out.println(); break; } } out.println("\t};"); out.println(); out.println("\tpublic static boolean __checkDependencies() {"); out.println("\t\tfor(int i = 0; i < __dependencies.length; i += 2) {"); out.println("\t\t\tif(!((java.io.File) __dependencies[i]).exists() ||"); out.println("\t\t\t\t\t((java.io.File) __dependencies[i]).lastModified() > ((java.lang.Long) __dependencies[i+1]).longValue()) {"); out.println("\t\t\t\treturn true;"); out.println("\t\t\t}"); out.println("\t\t}"); out.println("\t\treturn false;"); out.println("\t}"); // generate end of class out.println("}"); } finally { out.close(); } } catch(IOException ioexc) { throw new JSPException("Could not write java file " + javaFile + ": " + ioexc.getMessage()); } } private void compileJavaFile() throws JSPException { String[] compilerArgs; ByteArrayOutputStream compilerOut; PrintStream compilerOutStream; String s, t; StringBuffer buf; int i, j, k, l; compilerOut = new ByteArrayOutputStream(); compilerOutStream = new PrintStream(compilerOut, true); // build compiler command line if(jspServlet.compiler[0].equals("builtin-javac")) { compilerArgs = new String[jspServlet.compiler.length-1]; j = 0; } else { compilerArgs = new String[jspServlet.compiler.length]; compilerArgs[0] = jspServlet.compiler[0]; j = 1; } compilerOutStream.print(jspServlet.compiler[0]); for(i = 1; i < jspServlet.compiler.length; i++) { s = jspServlet.compiler[i]; buf = new StringBuffer(s.length() + 16); k = s.indexOf('%'); l = -1; while(k != -1) { buf.append(s.substring(l+1, k)); l = s.indexOf('%', k+1); if(l == -1) { l = k-1; break; } else { t = s.substring(k+1, l); if(t.length() == 0) { t = "%"; } else if(t.equalsIgnoreCase("classpath")) { t = Utils.calculateClassPath(getClass().getClassLoader()); } else if(t.equalsIgnoreCase("repository")) { t = jspServlet.repository.toString(); } else if(t.equalsIgnoreCase("source")) { t = javaFile.toString(); } else { t = jspServlet.getServletConfig().getInitParameter(t); } if(t != null) { buf.append(t); } k = s.indexOf('%', l+1); } } buf.append(s.substring(l+1)); compilerArgs[j] = buf.toString(); compilerOutStream.print((char) ' '); compilerOutStream.print(compilerArgs[j]); j++; } compilerOutStream.println(); if(jspServlet.compiler[0].equals("builtin-javac")) { // use builtin compiler
try { Object javaCompiler = Class.forName("sun.tools.javac.Main").getConstructor(new Class[] {OutputStream.class, String.class}).newInstance(new Object[]{compilerOutStream, "javac"}); if(Boolean.FALSE.equals(javaCompiler.getClass().getMethod("compile", new Class[]{String[].class}).invoke (javaCompiler, new Object[] {compilerArgs}))) { throw new JSPException(transcribeErrors(htmlEncode(compilerOut.toString()))); }
} catch(Exception e) {
if (e instanceof JSPException)
throw (JSPException)e; throw new JSPException(transcribeErrors(htmlEncode("Can't instantiate Java compiler, "+e+"/"+e.getCause())));
} } else { // use external compiler Process p; BufferedReader stdout = null, stderr = null; String line; int exitValue = -1; long classLastModified; classLastModified = classFile.lastModified(); try { p = Runtime.getRuntime().exec(compilerArgs); stdout = new BufferedReader(new InputStreamReader(p.getInputStream())); stderr = new BufferedReader(new InputStreamReader(p.getErrorStream())); // polling should be threaded while((line = stdout.readLine()) != null) { compilerOutStream.println(line); } while((line = stderr.readLine()) != null) { compilerOutStream.println(line); } try { p.waitFor(); exitValue = p.exitValue(); } catch(InterruptedException ix) { ; } } catch(IOException ioexc) { ioexc.printStackTrace(compilerOutStream); } if(classFile.lastModified() == classLastModified) { compilerOutStream.println("[no class file has been written]"); exitValue = -1; } if(exitValue != 0) { throw new JSPException(transcribeErrors(htmlEncode(compilerOut.toString()))); } } } private static String htmlEncode(String val) { StringBuffer buf = new StringBuffer(val.length() + 8); char c; for(int i = 0; i < val.length(); i++) { c = val.charAt(i); switch(c) { case '<': buf.append("<"); break; case '>': buf.append(">"); break; case '&': buf.append("&"); break; default: buf.append(c); break; } } return buf.toString(); } private static class MapEntry { int javaLineNr; String jspFile; int jspLineNr; MapEntry(int javaLineNr, String jspFile, int jspLineNr) { this.javaLineNr = javaLineNr; this.jspFile = jspFile; this.jspLineNr = jspLineNr; } } /** * Transcribe error messages */ private String transcribeErrors(String errors) { LineNumberReader in; BufferedReader in2; Vector map; Enumeration e; MapEntry entry; StringBuffer errBuf; String s, jspFile; int i, j, k, l, javaLineNr, jspLineNr; try { /* Build mapping from java line numbers to jsp file/line# combinations. * We could have done this will writing the java file, but using * LineNumberReader is easier, and it would take extra time when * no errors occur. */ in = new LineNumberReader(new FileReader(javaFile)); in.setLineNumber(1); map = new Vector(); try { while((s = in.readLine()) != null) { if(s.startsWith("//line ")) { i = s.indexOf(':'); if(i >= 0) { try { map.addElement(new MapEntry(in.getLineNumber(), s.substring(7, i), Integer.parseInt(s.substring(i+1)))); } catch(NumberFormatException nfexc) { } } } } } finally { in.close(); } /* Now we read every line of the error messages and translate any * file references there. */ in2 = new BufferedReader(new StringReader(errors)); errBuf = new StringBuffer(); try { while((s = in2.readLine()) != null) { i = s.indexOf(javaFile.getPath()); if(i != -1) { j = i + javaFile.getPath().length(); if(j < s.length()-1 && s.charAt(j) == ':' && Character.isDigit(s.charAt(j+1))) { j++; k = j; while(k < s.length() && Character.isDigit(s.charAt(k))) { k++; } l = k; while(l+1 < s.length() && s.charAt(l) == ':' && Character.isDigit(s.charAt(l+1))) { l += 2; while(l < s.length() && Character.isDigit(s.charAt(l))) { l++; } } try { javaLineNr = Integer.parseInt(s.substring(j, k)); jspFile = null; jspLineNr = 0; for(e = map.elements(); e.hasMoreElements(); ) { entry = (MapEntry) e.nextElement(); if(entry.javaLineNr > javaLineNr) { break; } jspFile = entry.jspFile; jspLineNr = entry.jspLineNr + (javaLineNr - entry.javaLineNr); } // valid translation found: use it if(jspFile != null) { errBuf.append(s.substring(0, i)); errBuf.append("<B>").append(jspFile).append((char) ':').append(jspLineNr).append("</B>"); errBuf.append("<!-- ").append(s.substring(i, l)).append(" -->"); errBuf.append(s.substring(l)).append(lineSeparator); continue; } } catch(NumberFormatException nfexc2) { } } } errBuf.append(s).append(lineSeparator); } return errBuf.toString(); } finally { in2.close(); } } catch(IOException ioexc) { return errors; } } private static String javaStringEncode(String val) { StringBuffer res; String s; int i; char c; res = new StringBuffer(val.length()); for(i = 0; i < val.length(); i++) { c = val.charAt(i); switch(c) { case '\n': res.append("\\n"); break; case '\r': res.append("\\r"); break; case '\'': res.append("\\\'"); break; case '\"': res.append("\\\""); break; case '\\': res.append("\\\\"); break; default: if(c > 0xFF) { s = "00" + Integer.toHexString(c); res.append("\\u").append(s.substring(s.length() - 4)); } else if(c < ' ' || c >= 0x7F) { s = "00" + Integer.toOctalString(c); res.append((char) '\\').append(s.substring(s.length() - 3)); } else { res.append((char) c); } break; } } return res.toString(); } public static String getClassNameForJspFile(File jspFile) throws JSPException { StringTokenizer toker; StringBuffer buf; String s = jspFile.toString(); char c; int i; toker = new StringTokenizer(s, String.valueOf(File.separatorChar)); buf = new StringBuffer(s.length() + 32); buf.append("_jsp"); while(toker.hasMoreTokens()) { s = toker.nextToken(); buf.append("._"); for(i = 0; i < s.length(); i++) { c = s.charAt(i); if(Character.isJavaIdentifierPart(c)) { buf.append((char) c); } else { buf.append((char) '_').append(Integer.toHexString(c)); } } } return buf.toString(); } public static File getFileForClass(File repository, String className, String suffix) { return new File(repository, className.replace('.', File.separatorChar) + suffix); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -