001: package com.mockrunner.gen;
002:
003: import java.io.File;
004: import java.io.FileNotFoundException;
005: import java.io.FileReader;
006: import java.io.FileWriter;
007: import java.io.IOException;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.Map;
011:
012: import com.mockrunner.gen.proc.GeneratorUtil;
013: import com.mockrunner.gen.proc.JavaLineProcessor;
014: import com.mockrunner.util.common.StreamUtil;
015:
016: public class VersionGenerator {
017: private Map processorMap;
018: private String generatorName;
019: private String rootTargetDir;
020: private String rootSourceDir;
021: private String[] processedPackages;
022:
023: public VersionGenerator(Map processorMap, String generatorName,
024: String rootTargetDir, String rootSourceDir,
025: String[] processedPackages) {
026: this .processorMap = processorMap;
027: this .generatorName = generatorName;
028: this .rootTargetDir = rootTargetDir;
029: this .rootSourceDir = rootSourceDir;
030: this .processedPackages = processedPackages;
031: }
032:
033: public void doSynchronize() throws Exception {
034: System.out.println("Start processing for " + generatorName);
035: GeneratorUtil util = new GeneratorUtil();
036: deleteContent(new File(rootTargetDir));
037: Map srcMap = new HashMap();
038: for (int ii = 0; ii < processedPackages.length; ii++) {
039: File currentFile = new File(rootSourceDir + "/"
040: + processedPackages[ii]);
041: util.addJavaSrcFiles(rootSourceDir, currentFile, srcMap);
042: }
043: processFiles(processorMap, srcMap, rootTargetDir);
044: System.out.println("Sucessfully finished processing for "
045: + generatorName);
046: }
047:
048: private void deleteContent(File file) {
049: if (!file.isDirectory()) {
050: file.delete();
051: } else {
052: File[] files = file.listFiles();
053: for (int ii = 0; ii < files.length; ii++) {
054: deleteContent(files[ii]);
055: }
056: file.delete();
057: }
058: }
059:
060: private void processFiles(Map procMap, Map map, String targetDir)
061: throws FileNotFoundException, IOException {
062: Iterator sourceIterator = map.keySet().iterator();
063: while (sourceIterator.hasNext()) {
064: String currentFileName = (String) sourceIterator.next();
065: File currentSourceFile = (File) map.get(currentFileName);
066: File currentDestFile = new File(targetDir + currentFileName);
067: String sourceFileContent = StreamUtil
068: .getReaderAsString(new FileReader(currentSourceFile));
069: System.out.println("Processing file " + currentSourceFile);
070: String processedFileContent = processFile(currentFileName,
071: sourceFileContent, procMap);
072: if (null != processedFileContent) {
073: writeFileContent(processedFileContent, currentDestFile);
074: }
075: }
076: }
077:
078: private String processFile(String currentFileName,
079: String fileContent, Map jdbcProcMap) {
080: currentFileName = currentFileName.replace('\\', '.');
081: currentFileName = currentFileName.replace('/', '.');
082: currentFileName = currentFileName.substring(1);
083: currentFileName = currentFileName.substring(0, currentFileName
084: .length() - 5);
085: Object currentObject = (Object) jdbcProcMap
086: .get(currentFileName);
087: if (null == currentObject) {
088: return fileContent;
089: } else if (currentObject instanceof JavaLineProcessor) {
090: return ((JavaLineProcessor) currentObject)
091: .process(fileContent);
092: } else if (currentObject instanceof Boolean) {
093: if (!((Boolean) currentObject).booleanValue()) {
094: return null;
095: } else {
096: return fileContent;
097: }
098: }
099: return null;
100: }
101:
102: private void writeFileContent(String fileContent,
103: File currentDestFile) throws FileNotFoundException,
104: IOException {
105: if (!currentDestFile.getParentFile().exists()) {
106: currentDestFile.getParentFile().mkdirs();
107: }
108: System.out.println("Writing file " + currentDestFile);
109: FileWriter currentDestFileWriter = new FileWriter(
110: currentDestFile);
111: currentDestFileWriter.write(fileContent);
112: currentDestFileWriter.flush();
113: currentDestFileWriter.close();
114: }
115: }
|