001package gu.sql2java.generator; 002 003import java.io.BufferedReader; 004import java.io.File; 005import java.io.FileReader; 006import java.util.Enumeration; 007import java.util.Hashtable; 008 009public class UserCodeParser { 010 private static final String START = "// "; 011 private static final String BLOCK_BEGIN = "+"; 012 private static final String BLOCK_END = "-"; 013 private static final String LINE_SEP = System.getProperty("line.separator"); 014 private Hashtable<String, String> codeHash; 015 private String filename; 016 private boolean isNew; 017 018 public UserCodeParser(String filename) throws Exception { 019 this.parse(filename); 020 } 021 022 public String getFilename() { 023 return this.filename; 024 } 025 026 public boolean isNew() { 027 return this.isNew; 028 } 029 030 public void parse(String parsedFileName) throws Exception { 031 this.codeHash = new Hashtable<>(); 032 boolean inBlock = false; 033 String blockName = null; 034 StringBuffer code = new StringBuffer(); 035 this.isNew = true; 036 File file = new File(parsedFileName); 037 if (file.exists()) { 038 this.filename = parsedFileName; 039 this.isNew = false; 040 BufferedReader reader = new BufferedReader(new FileReader(file)); 041 String line = reader.readLine(); 042 while (line != null) { 043 if (inBlock) { 044 code.append(line).append(LINE_SEP); 045 } 046 if (line.indexOf(START) != -1) { 047 if (inBlock) { 048 if (line.equals(START + blockName + BLOCK_END)) { 049 this.codeHash.put(blockName, code.toString()); 050 inBlock = false; 051 } 052 } else { 053 blockName = this.parseName(line); 054 if (!"".equals(blockName)) { 055 inBlock = true; 056 code.setLength(0); 057 code.append(line).append(LINE_SEP); 058 } 059 } 060 } 061 line = reader.readLine(); 062 } 063 reader.close(); 064 } 065 } 066 067 private String parseName(String line) { 068 int startPos = line.indexOf(START); 069 if (startPos == -1) { 070 return ""; 071 } 072 if ((startPos += START.length()) >= line.length() + 1) { 073 return ""; 074 } 075 int endPos = line.lastIndexOf(BLOCK_BEGIN, startPos); 076 if (endPos != line.length() - BLOCK_BEGIN.length()) { 077 return ""; 078 } 079 String name = line.substring(startPos, endPos); 080 return name.trim(); 081 } 082 083 public boolean hasBlock(String name) { 084 return this.codeHash.get(name) != null; 085 } 086 087 public String getBlock(String name) { 088 String code = null; 089 if (name != null) { 090 code = (String) this.codeHash.get(name); 091 } 092 if (code == null) { 093 code = this.generateNewBlock(name); 094 this.codeHash.put(name, code); 095 } 096 return code; 097 } 098 099 public String[] getBlockNames() { 100 String[] list = new String[this.codeHash.size()]; 101 int i = 0; 102 Enumeration<String> e = this.codeHash.keys(); 103 while (e.hasMoreElements()) { 104 list[i++] = (String) e.nextElement(); 105 } 106 return list; 107 } 108 109 private String generateNewBlock(String name) { 110 StringBuffer str = new StringBuffer(512); 111 str.append(START); 112 str.append(name); 113 str.append(BLOCK_BEGIN); 114 str.append(LINE_SEP).append(LINE_SEP); 115 str.append(START); 116 str.append(name); 117 str.append(BLOCK_END); 118 return str.toString(); 119 } 120}