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