简单的JSP编译器

把jsp文件编译成一个Servlet,没有用到正则。也没有用到第三方包。不支持EL和JSTL,功能比较简单。

index.jsp

<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>123
<html>456
<head>789
 123   <title>Title</title> 456
</head> 789
<body> 123
<%! String str = "hihihi"; %>
<%
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 String str = format.format(new Date());
%>
<%--这是注释部分--%>
<%
 out.write("哈哈哈哈哈哈");
%>
<%=str %>
<h1>Hello World!</h1> 456
</body> 789
</html>

编译类

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class JSPCompiler {

    public static String readJSPFile(String path) throws IOException {
        InputStream inputStream = new FileInputStream(path);
        byte[] buf = new byte[inputStream.available()];
        inputStream.read(buf);
        return new String(buf);
    }

    public static List<String> getTagList(String jspStr){
        int startIndex;
        int endIndex = 0;
        String tag;
        char ch1;
        char ch2;
        List<String> tagList = new ArrayList<>();

        while(true) {
            startIndex = jspStr.indexOf('<',endIndex);
            endIndex = jspStr.indexOf('<',startIndex+1);
            if(endIndex == -1){
                tag = jspStr.substring(startIndex);
                tagList.add(tag);
                break;
            }
            ch1 = jspStr.charAt(startIndex+1);
            ch2 = jspStr.charAt(endIndex + 1);
            if(ch1 == '/'){
                tag = jspStr.substring(startIndex,endIndex);
                tagList.add(tag);
            }else if(ch2 == '/'){
                endIndex = jspStr.indexOf('<',endIndex+1);
                tag = jspStr.substring(startIndex,endIndex);
                tagList.add(tag);
            }else {
                tag = jspStr.substring(startIndex,endIndex);
                tagList.add(tag);
            }
        }

        return tagList;
    }

    public static String readAttr(String tag,String attr){
        int index = tag.indexOf(attr);
        if(index == -1){
            return null;
        }
        int start = tag.indexOf('\"', index);
        int end = tag.indexOf('\"',start+1);
        String contentType = tag.substring(start+1,end);
        return contentType;
    }
    
    public static String getServletString(List<String> tagList,String servletName){
        StringBuilder servletBuf = new StringBuilder("package servlet;\n\n");
        StringBuilder fieldBuf = new StringBuilder();
        StringBuilder preSend = new StringBuilder();
        StringBuilder serviceBody = new StringBuilder();

        for (String tag : tagList) {
            if(tag.startsWith("<%--")){//jsp注释
                int end = tag.indexOf('>');
                String text = tag.substring(end+1).trim();
                if(text.length() > 0){
                    String x = "\t\tout.write(\"" + text + "\");\n";
                    serviceBody.append(x);
                }
            }else if(tag.startsWith("<%@")){//page指令
                String contentType = readAttr(tag,"contentType");
                String importCode = readAttr(tag,"import");
                String pageEncoding = readAttr(tag,"pageEncoding");

                if(contentType != null){
                    String x = "\t\tresponse.setContentType(\"" + contentType + "\");\n";
                    preSend.append(x);
                }
                if(importCode != null){
                    String x = "import " + importCode + ";\n";
                    servletBuf.append(x);
                }
                if(pageEncoding != null){
                    String x = "\t\tresponse.setCharacterEncoding(\"" + pageEncoding + "\");\n";
                    preSend.append(x);
                }

                int index = tag.indexOf('>');
                String text = tag.substring(index+1).trim();
                if(text.length() > 0){
                    String x = "\t\tout.write(\"" + text + "\");\n";
                    serviceBody.append(x);
                }
            }else if(tag.startsWith("<%=")){//jsp表达式
                int end = tag.indexOf('>');
                String text = tag.substring(end+1).trim();
                String code = tag.substring(3, end-1);
                String x = "\t\tout.print(" + code + ");\n";
                serviceBody.append(x);
                if(text.length() > 0){
                    String x1 = "\t\tout.write(\"" + text + "\");\n";
                    serviceBody.append(x1);
                }
            }else if (tag.startsWith("<%!")){//全局变量
                int end = tag.indexOf('>');
                String text = tag.substring(end+1).trim();
                String code = "\tprivate "+tag.substring(3,end-1)+"\n";
                fieldBuf.append(code);
                if(text.length() > 0){
                    String x = "\t\tout.write(\"" + text + "\");\n";
                    serviceBody.append(x);
                }
            }else if(tag.startsWith("<%")){//jsp代码
                int end = tag.indexOf('>');
                String text = tag.substring(end+1).trim();
                String code = "\t\t"+tag.substring(2, end-1);
                serviceBody.append(code);
                if(text.length() > 0){
                    String x = "\t\tout.write(\"" + text + "\");\n";
                    serviceBody.append(x);
                }
            }else {
                String replace = tag.replace("\r\n", "\\r\\n");
                String x = "\t\tout.write(\"" + replace + "\");\n";
                serviceBody.append(x);
            }
        }

        servletBuf.append( "import java.io.IOException;\n"+
                "import myCat.pagecontext.JspWriter;\n" +
                "import myCat.pagecontext.PageContext;\n" +
                "import myCat.request.Request;\n" +
                "import myCat.response.Response;\n" +
                "import myCat.response.ResponseHandler;\n" +
                "import myCat.servlet.Servlet;\n"+
                "import myCat.config.Config;\n" +
                "import myCat.session.Session;\n" +
                "import myCat.servletcontext.Application;\n" +
                "import myCat.servletcontext.ServletContext;\n\n");
        servletBuf.append("\npublic final class ");
        servletBuf.append(servletName);
        servletBuf.append(" extends Servlet {\n");
        servletBuf.append(fieldBuf);
        servletBuf.append("\n\tpublic void service(Request request,Response response) throws IOException {\n"
                + "\t\tPageContext pageContext = new PageContext(request,response);\n"//生成内置对象
                +"\t\tJspWriter out = pageContext.getWriter();\n"
                + "\t\tObject page = this;\n" +
                "\t\tConfig config = request.getConfig();\n" +
                "\t\tSession session = request.getSession();\n" +
                "\t\tApplication application = ServletContext.newInstance();\n\n");
        servletBuf.append(preSend);//写出内容之前可能要做一些处理
        servletBuf.append("\t\tString responseText = ResponseHandler.generateResponseText(response);\n"
                +"\t\tout.write(responseText);\n\n");//生成响应头和消息
        servletBuf.append(serviceBody);//方法体
        servletBuf.append("\t\tout.close();\n\t}\n}");
        return servletBuf.toString();
    }

    public static void compileJSP(String jspPath,String jspName){
        try {
            String servletName = jspName.substring(0,jspName.indexOf("."))+"_jsp";
            String targetPath = servletName+".java";
            readJSPFile(jspPath);
            String jspStr = readJSPFile(jspPath);
            List<String> tagList = getTagList(jspStr);
            String servletString = getServletString(tagList, servletName);
            OutputStream outputStream = new FileOutputStream(targetPath);
            outputStream.write(servletString.getBytes());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        String jspName = "index.jsp";
        String jspPath = "C:\\Users\\ngoyat\\Desktop\\JSPCompile\\src\\"+jspName;
        compileJSP(jspPath,jspName);
    }
}

最终生成的index_jsp.java

package servlet;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.IOException;
import myCat.pagecontext.JspWriter;
import myCat.pagecontext.PageContext;
import myCat.request.Request;
import myCat.response.Response;
import myCat.response.ResponseHandler;
import myCat.servlet.Servlet;
import myCat.config.Config;
import myCat.session.Session;
import myCat.servletcontext.Application;
import myCat.servletcontext.ServletContext;


public final class index_jsp extends Servlet {
	private  String str = "hihihi"; 

	public void service(Request request,Response response) throws IOException {
		PageContext pageContext = new PageContext(request,response);
		JspWriter out = pageContext.getWriter();
		Object page = this;
		Config config = request.getConfig();
		Session session = request.getSession();
		Application application = ServletContext.newInstance();

		response.setContentType("text/html;charset=UTF-8");
		String responseText = ResponseHandler.generateResponseText(response);
		out.write(responseText);

		out.write("123");
		out.write("<html>456\r\n");
		out.write("<head>789\r\n 123   ");
		out.write("<title>Title</title> 456\r\n");
		out.write("</head> 789\r\n");
		out.write("<body> 123\r\n");
		
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 String str = format.format(new Date());
		
 out.write("哈哈哈哈哈哈");
		out.print(str );
		out.write("<h1>Hello World!</h1> 456\r\n");
		out.write("</body> 789\r\n");
		out.write("</html>");
		out.close();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值