1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.javagen.agile.core.visitor;
17
18 import java.util.Set;
19
20 import org.javagen.agile.core.context.ContextHolderStack;
21 import org.javagen.agile.core.emitter.Emitter;
22 import org.javagen.agile.core.emitter.binding.Binder;
23 import org.javagen.agile.core.emitter.binding.ModelTypeBinder;
24 import org.javagen.agile.core.emitter.context.EmitterContextStackLoader;
25 import org.javagen.agile.core.emitter.context.EmitterContextLoader;
26 import org.javagen.agile.core.model.Model;
27
28 /***
29 * Walks the arbitrary model tree and invokes code generator modules attatched to model nodes.
30 *
31 * @author Richard Easterling
32 */
33 public class EmitterVisitor extends DefaultVisitor {
34
35 protected Binder binder;
36 protected EmitterContextLoader contextLoader;
37 protected ContextHolderStack contextHolderStack = new ContextHolderStack();
38
39 public EmitterVisitor() {}
40
41 public EmitterVisitor(Binder binder) { this.binder = binder; }
42
43
44
45
46
47 public void visit(Model model) {
48 Set<Emitter> emitters = binder.boundEmitters(model);
49 if (emitters!=null)
50 for(Emitter emitter : emitters) {
51 contextLoader.pushAll(contextHolderStack, model, emitter);
52 try {
53 emitter.emit(contextHolderStack);
54 } finally {
55 contextLoader.popAll(contextHolderStack, model, emitter);
56 }
57 }
58 }
59
60
61
62
63
64 public Binder getBinder() {
65 if (binder==null)
66 binder = new ModelTypeBinder();
67 return binder;
68 }
69
70 public void setBinder(Binder binder) {
71 this.binder = binder;
72 }
73
74 public EmitterContextLoader getContextLoader() {
75 if (contextLoader==null)
76 contextLoader = new EmitterContextStackLoader();
77 return contextLoader;
78 }
79
80 public void setContextLoader(EmitterContextLoader contextLoader) {
81 this.contextLoader = contextLoader;
82 }
83
84 }