- /**
- * Copyright: Copyright (c) 2002-2004
- * Company: JavaResearch(http://www.javaresearch.org)
- */
-
- package org.jr.java2html;
-
- import java.io.*;
- import java.util.*;
-
- /**
- * 模版文件的抽象类。
- * 根据相应的HTML文件模版以及参数生成html文件。
- * 此类定义了一些模版文件需要使用的特殊的属性关键字,包括:<Br>
- * <ul>
- * <li>时戳关键字:JTHSYSTEMTIMESTAMP
- * <li>循环开始标记(必须独占一行):<!--%LOOP%-->
- * <li>循环结束标记(必须独占一行):<!--%LOOP_END%-->
- * <li>循环名称关键字:JTHLOOP$,请注意‘$’应该是一个数字,从0开始
- * <li>循环次数关键字:JTHLOOPSIZE
- * </ul>
- * 请注意以上的关键字是保留给模版使用的,一般的属性关键字不能使用以上内容。
- * <br>最后更新日期:2003年5月23日
- * @author cherami@javaresearch.org
- * @version 0.9
- */
-
- public class TemplateFile {
- /**
- * 保留的时戳关键字。
- */
- public static final String TIMESTAMP = TemplatePropertyMap.
- PROPERTY_KEY_PREFIX + "JTHSYSTEMTIMESTAMP" +
- TemplatePropertyMap.PROPERTY_KEY_SUFFIX;
- /**
- * 循环开始标记。
- */
- public static final String LOOP_START = "<!--%LOOP%-->";
- /**
- * 循环结束标记。
- */
- public static final String LOOP_END = "<!--%LOOP_END%-->";
- /**
- * 保留的循环次数关键字。
- */
- public static final String SIZE = TemplatePropertyMap.PROPERTY_KEY_PREFIX +
- "JTHLOOPSIZE" + TemplatePropertyMap.PROPERTY_KEY_SUFFIX;
- int loopCount = 0;
- TemplatePropertyMap properties;
- File templateFile;
- File file;
- String[] templateBuffers;
- boolean bufferable = false;
- /**
- * 根据指定的模版文件构造一个TemplateFile。
- * @param templateFile 模版文件
- */
- public TemplateFile(File templateFile) {
- this.templateFile = templateFile;
- }
- /**
- * 根据指定的模版文件构造一个TemplateFile。
- * @param templateFileName 模版文件名
- */
- public TemplateFile(String templateFileName) {
- templateFile = new File(templateFileName);
- }
- /**
- * 根据指定的参数构造一个TemplateFile。
- * @param templateFile 模版文件
- * @param file 生成的文件
- * @param properties 属性映射
- */
- public TemplateFile(File templateFile, File file,
- TemplatePropertyMap properties) {
- this.properties = properties;
- this.templateFile = templateFile;
- this.file = file;
- }
- /**
- * 设置生成的文件的文件名。
- * @param fileName 生成文件的文件名
- */
- public void setFile(String fileName) {
- file = new File(fileName);
- }
- /**
- * 设置生成的文件。
- * @param file 生成的文件
- */
- public void setFile(File file) {
- this.file = file;
- }
- /**
- * 设置属性映射。
- * @param properties 属性映射
- */
- public void setProperties(TemplatePropertyMap properties) {
- this.properties = properties;
- }
- /**
- * 设置是否使用缓存。
- * 在从一个模版文件生成大量文件是设置使用缓存可以很大的提高生成的效率。
- * @param bufferable 是否使用缓存
- */
- public void setBufferable(boolean bufferable) {
- this.bufferable = bufferable;
- }
- /**
- * 生成目标文件。
- * @return 成功生成时返回true,否则返回false
- */
- public boolean createFile() {
- loopCount = 0;
- BufferedWriter writer = null;
- if (properties == null) {
- properties = new TemplatePropertyMap();
- }
- try {
- if (templateBuffers == null || bufferable == false) {
- getTemplateContents();
- }
- file.getParentFile().mkdirs();
- properties.put(TIMESTAMP, new Date().toString());
- writer = new BufferedWriter(new FileWriter(file));
- for (int i = 0; i < templateBuffers.length; i++) {
- int loop=0;
- if ((loop=replaceLoop(i, writer))>0) {
- i += loop;
- continue;
- }
- String result = properties.replaceAllProperties(templateBuffers[i]);
- writer.write(result);
- writer.newLine();
- }
- Utility.debug("Successful create file:"+file.getAbsolutePath());
- return true;
- }
- catch (IOException e) {
- System.err.println(e.getMessage());
- return false;
- }
- finally {
- if (writer != null) {
- try {
- writer.close();
- return false;
- }
- catch (IOException ioe) {
- System.err.println(ioe.getMessage());
- return false;
- }
- }
- }
- }
- /**
- * 得到模版文件的内容并存入缓存。
- * @throws IOException
- */
- private void getTemplateContents() throws IOException {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(
- templateFile));
- ArrayList lines = new ArrayList();
- String line = reader.readLine();
- while (line != null) {
- lines.add(line);
- line = reader.readLine();
- }
- templateBuffers = (String[]) lines.toArray(new String[0]);
- }
- catch (IOException e) {
- throw e;
- }
- finally {
- if (reader != null) {
- reader.close();
- }
- }
- }
- /**
- * 替换循环的内容。
- * @param index 模版内容缓存的行数
- * @param writer 缓冲写入器
- * @return 循环部分的行数。
- * @throws IOException
- */
- private int replaceLoop(int index, BufferedWriter writer) throws
- IOException {
- int result = 0;
- String line = templateBuffers[index];
- if (line.equals(LOOP_START)) {
- result++;//增加循环开始标记应该占用的一行
- String loopName = getLoopName(loopCount);
- StringBuffer buffer =new StringBuffer(1024);
- int loopIndex=index+1;
- int loopLength=templateBuffers.length-1;
- String loopLine = templateBuffers[loopIndex];
- while ((!loopLine.equals(LOOP_END))&&loopIndex<loopLength) {
- buffer.append(loopLine);
- result++;
- loopIndex++;
- loopLine = templateBuffers[loopIndex];
- }
- result++;//增加循环结束标记应该占用的一行
- TemplatePropertyMap loopProperties = (TemplatePropertyMap) properties.get(
- loopName);
- int loopTimes = Integer.parseInt( (String) loopProperties.get(SIZE));
- loopLine=buffer.toString();
- for (int i = 0; i < loopTimes; i++) {
- createLoopLine(writer, loopLine, loopProperties, i);
- }
- loopCount++;
- }
- return result;
- }
- /**
- * 创建循环内容的一行。
- * @param writer 缓冲写入器
- * @param line 原始行的内容
- * @param loopProperties 循环的属性映射
- * @param index 循环的次数
- * @throws IOException
- */
- private void createLoopLine(BufferedWriter writer, String line,
- TemplatePropertyMap loopProperties, int index) throws
- IOException {
- Iterator keys = loopProperties.keySet().iterator();
- while (keys.hasNext()) {
- String key = (String) keys.next();
- if (line.indexOf(key) < 0) {
- continue;
- }
- else {
- line = loopProperties.replaceByList(line, key, index);
- }
- }
- line = properties.replaceAllProperties(line);
- writer.write(line);
- writer.newLine();
- }
- /**
- * 得到循环的属性关键字。
- * 参数的值必须大于等于0,否则会抛出IllegalArgumentException异常。
- * @param loop 循环的顺序号,从0开始
- * @return ##JTHLOOP$@@,$为loop转换为字符串的结果
- */
- public static String getLoopName(int loop) {
- if (loop<0) {
- throw new IllegalArgumentException(
- "Loop must be equal or more than 0!Your loop value is:"+loop);
- }
- return TemplatePropertyMap.
- PROPERTY_KEY_PREFIX + "JTHLOOP" + loop + TemplatePropertyMap.PROPERTY_KEY_SUFFIX;
- }
-
- }