01: package com.mockrunner.gen;
02:
03: import java.io.File;
04: import java.io.FileNotFoundException;
05: import java.io.FileOutputStream;
06: import java.io.IOException;
07: import java.io.OutputStreamWriter;
08: import java.io.Writer;
09: import java.util.ArrayList;
10: import java.util.Iterator;
11: import java.util.List;
12:
13: import com.mockrunner.gen.proc.AdapterProcessor;
14:
15: public abstract class AbstractAdapterGenerator {
16: protected void generate() throws Exception {
17: List units = initialize();
18: Iterator iterator = units.iterator();
19: while (iterator.hasNext()) {
20: ProcessingUnit nextUnit = (ProcessingUnit) iterator.next();
21: AdapterProcessor processor = nextUnit.getProcessor();
22: processor.process(nextUnit.getModule(), nextUnit
23: .getExcludedMethods());
24: writeOutputFile(processor);
25: }
26: System.out.println("Adapters successfully created");
27: }
28:
29: private void writeOutputFile(AdapterProcessor processor)
30: throws FileNotFoundException, IOException {
31: System.out
32: .println("Writing output file " + processor.getName());
33: File currentFile = new File(getSrcDir() + "/"
34: + processor.getName());
35: FileOutputStream currentStream = new FileOutputStream(
36: currentFile);
37: Writer currentWriter = new OutputStreamWriter(currentStream,
38: "ISO-8859-1");
39: currentWriter.write(processor.getOutput());
40: currentWriter.flush();
41: currentWriter.close();
42: }
43:
44: protected abstract List initialize();
45:
46: protected abstract String getSrcDir();
47:
48: protected class ProcessingUnit {
49: private Class module;
50: private AdapterProcessor processor;
51: private List excludedMethods;
52:
53: public ProcessingUnit(Class module, AdapterProcessor processor) {
54: this (module, processor, new ArrayList());
55: }
56:
57: public ProcessingUnit(Class module, AdapterProcessor processor,
58: List excludedMethods) {
59: this .module = module;
60: this .processor = processor;
61: this .excludedMethods = excludedMethods;
62: }
63:
64: public Class getModule() {
65: return module;
66: }
67:
68: public List getExcludedMethods() {
69: return excludedMethods;
70: }
71:
72: public AdapterProcessor getProcessor() {
73: return processor;
74: }
75: }
76: }
|