⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blockanalyzer.java

📁 实现了从Google
💻 JAVA
字号:
/**
 * Template Analyzer
 */
package com.ct.hotweb.util;

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

public class BlockAnalyzer {
  protected BufferedReader templateBlock = null;
  //块字符串
  protected String blockString = null;
  //变量列表,有顺序关系
  protected ArrayList vars = new ArrayList();
  //字符常量列表,有顺序关系
  protected ArrayList strings = new ArrayList();

  public BlockAnalyzer(BufferedReader templateBlock) {
    this.templateBlock = templateBlock;
  }

  /**
   * 预处理,进行trim处理
   * @return String
   */
  public void prepareAnalyzer() throws Exception {
    StringBuffer sb = new StringBuffer();
    String line = null;

    while ( (line = templateBlock.readLine()) != null)
      sb.append(line.trim()).append("\n");
    templateBlock.close();
    blockString = sb.toString();
  }

  /**
   * 分析块
   * @throws Exception
   */
  public void anlyzer() throws Exception {
    //reset var info list
    vars.clear();
    int start = 0, end = 0;
    int sepLength = ConstParameter.VAR_SEPARATOR.length();

    while ( (start = blockString.indexOf(ConstParameter.VAR_SEPARATOR, start)) >=
           0) {
      //ignor escape char
      if (start != 0 &&
          blockString.charAt(start - 1) == ConstParameter.ESCAPE_CHAR) {
        start += sepLength;
        continue;
      }

      String varName;
      int posStart = start + sepLength, posEnd;
      posEnd = blockString.indexOf(ConstParameter.VAR_SEPARATOR, posStart);
      //parse error
      if (posEnd < 0)
        throw new Exception(
            "parser var error.can't find left separator. at pos " +
            (posStart + 1));

      varName = blockString.substring(posStart, posEnd);

      if (varName.equals(""))
        throw new Exception("parser var error. null var name. at pos " +
                            (posStart + 1));

      VariableInfo vi = new VariableInfo(posStart, posEnd - sepLength, varName);
      vars.add(vi);
      posEnd += sepLength;
      start = posEnd;
    }
  }

  /**
   * 结束分析,进行后期strings的处理
   */
  public void afterAnlyzer() {
    int i;
    int length = ConstParameter.VAR_SEPARATOR.length();
    strings.clear();

    if (0 == vars.size()) {
      strings.add(blockString);
      return;
    }
    String tmp = blockString.substring(0, ((VariableInfo)(vars.get(0))).getPosStart() - length);
    strings.add(tmp);
    for (i = 0; i < vars.size() - 1; i ++) {
      tmp = blockString.substring(((VariableInfo)(vars.get(i))).getPosEnd() + length + 1, ((VariableInfo)(vars.get(i + 1))).getPosStart() - length);
      strings.add(tmp);
    }
    tmp = blockString.substring(((VariableInfo)(vars.get(i))).getPosEnd() + length + 1, blockString.length() - 1);
    strings.add(tmp);

    String repStr = ConstParameter.ESCAPE_CHAR + ConstParameter.VAR_SEPARATOR;

    for (i = 0; i < strings.size(); i ++) {
      tmp = (String)strings.get(i);
      strings.set(i, Utils.myReplaceAll(tmp, repStr, ConstParameter.VAR_SEPARATOR));
    }
  }

  /**
   * @return
   */
  public String getBlockString() {
    return new String(blockString);
  }

  /**
   * @return
   */
  public ArrayList getVars() {
    //return (ArrayList) vars.clone();
    return vars;
  }

  public ArrayList getStrings() {
    //return (ArrayList) strings.clone();
    return strings;
  }

/*
  public static void main(String[] args) {
    try {
      BlockAnalyzer ta = new BlockAnalyzer(new BufferedReader(new FileReader(
          "d:/tmp.txt")));
      ta.prepareAnalyzer();
      ta.anlyzer();
      ta.afterAnlyzer();
      System.out.println(ta.getBlockString());
      ArrayList al = ta.getVars();
      System.out.println("vars size: " + ta.vars.size() + "\tstrings size:" + ta.strings.size());
      System.out.println("Start output...........");
      System.out.println("string: " + ta.getStrings().get(0));
      for (int i = 0; i < al.size(); i++) {
        VariableInfo vi = (VariableInfo) al.get(i);
        System.out.println("vi info: start=" + vi.getPosStart()
                           + "; end=" + vi.getPosEnd()
                           + "; varname="
                           + vi.getVariableName());
        System.out.println("string: " + ta.getStrings().get(i + 1));
      }
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
*/
}

⌨️ 快捷键说明

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