View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  	// Visitor interface:
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  	// getters and setters:
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  }