根据文件模板用ant设值

最近有个需求,要求用java生成一段js的变量,因为变量是动态的,项目也是global的。因此想到了用ant.刚写完,现在乘热打铁。
我用的是filter去截取用户请求路径。主要业务是在doFilter里面,只是个demo哈。
package com.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;

public class TestFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		System.out.println("--------------");
		HttpServletRequest req = (HttpServletRequest) arg0;
		String uri = req.getRequestURI();
		System.out.println(uri);
//要生成的jsp,把var 变量就放在这里了,其他JSP直接include这个jsp就行
		String reportFileName = "lala.jsp";
		System.out.println("reportFileName == " + reportFileName);
		//ant 模板
		String tempPathAnt = "jsVarable.xml";
		//build的路径
		String buildPath = "D:\\study\\myeclipse6.0\\test\\src\\com\\test\\"
				+ tempPathAnt;
//要生成的jsp的路径
		String destPath = "D:\\study\\myeclipse6.0\\test\\WebRoot\\"
				+ reportFileName;
		System.out.println("destPath " + destPath);
		Map map = new HashMap();
		// set content value
		String content = "<script>var test=\">"+uri+"<\"</script>";
		//设值,以便ant能得到
		map.put("destPath", destPath);
		map.put("content", content);
		build(buildPath, map, true);
		try {
			//因为是文件更新,所以用点时间去等他更新, 必须修改
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		arg2.doFilter(arg0, arg1);
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	public static boolean build(String buildFile, Map propertyMap,
			boolean isDebug) {

		File file = new File(buildFile);
		Project project = new Project();
		ProjectHelper helper = ProjectHelper.getProjectHelper();

		if (isDebug) {
			DefaultLogger consoleLogger = new DefaultLogger();
			consoleLogger.setErrorPrintStream(System.err);
			consoleLogger.setOutputPrintStream(System.out);
			consoleLogger.setMessageOutputLevel(Project.MSG_ERR);
			project.addBuildListener(consoleLogger);
		}

		project.fireBuildStarted();
		project.init();
//遍历传来的map
		Iterator iterator = propertyMap.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			Object value = propertyMap.get(key);
			if (value instanceof String) {
				project.setProperty(key, (String) value);
			}
		}

		helper.parse(project, file);

		project.executeTarget(project.getDefaultTarget());
		project.fireBuildFinished(null);
		return true;

	}
}




jsVarable.xml
<?xml version="1.0" encoding="UTF-8"?>

<project name="test" default="test" basedir=".">

	<!-- 1、过滤参数 -->
	<target name="setParam">

		<!-- 内容 -->
		<filter token="content" value="${content}" />
	</target>
	<!-- 2、删除原有文件 -->
	<target name="removeFile" depends="setParam">
		<delete file="${destPath}" />
	</target>

	<!-- 3、创建文件 -->
	<target name="test" depends="setParam,removeFile">
		<copyfile dest="${destPath}" src="jsTemplate.jsp" filtering="true" forceoverwrite="true" />
		<echo message="copyfile ${destPath} success" />
			 
	</target>
</project>


jsTemplate.jsp


@content@





你可能感兴趣的:(java,apache,jsp,ant,servlet)