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.Map;
19
20 import org.javagen.agile.core.context.Keys;
21 import org.javagen.agile.core.model.Model;
22 import org.javagen.agile.core.util.ModelUtil;
23
24 /***
25 * Takes two model trees and sets all the non-null values of one (ie <code>customizingModels</code>)
26 * onto the equivalent instances of the other (ie <code>input</code> parameter on <code>gen</code> method) using the
27 * <code>copyTo</code> method. Instances are matched by ID using an index (usually) stored
28 * in the root node. Visitors are available to both generate IDs and build the index prior to
29 * calling this generator.
30 *
31 * @author Richard Easterling
32 */
33 public class IdOverrideVisitor extends DefaultVisitor {
34
35 private Map<String, Model> index = null;
36 private Model customizingModels;
37 private String idPrefixFilter;
38 private boolean ignoreUnboundMetadata = true;
39 private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(IdOverrideVisitor.class);
40
41
42
43
44
45 @SuppressWarnings("unchecked")
46 public Model gen(Model input) {
47 Model root = ModelUtil.getRootNode(input);
48 if (index==null)
49 index = (Map<String, Model>)root.get(Keys.MODEL_ID_INDEX.toString());
50 if (index==null)
51 throw new IllegalStateException("root node ("+root.getName()+
52 ") does not contain ID index in context under "+
53 Keys.MODEL_ID_INDEX.toString()+" key. Run IndexBuilderVisitor first.");
54 GenericWalker.walk(customizingModels, this);
55 return input;
56 }
57
58
59
60
61
62 public void visit(Model model) {
63 String id = model.getId();
64 if (id==null)
65 throw new IllegalStateException("customized Model node '"+model.getName()+"' does not contain an ID");
66 if (idPrefixFilter==null || id.startsWith(idPrefixFilter)) {
67 Model target = index.get(id);
68 if (target==null) {
69 if (ignoreUnboundMetadata)
70 log.warn("merge metadata id "+id+" not found in model tree");
71 else
72 throw new IllegalStateException("No Model found with the '"+id+"' ID");
73 } else {
74 model.copyTo(target);
75 }
76 }
77 }
78
79
80
81
82
83 public Model getCustomizingModels() {
84 return customizingModels;
85 }
86
87 public void setCustomizingModels(Model customizedModels) {
88 this.customizingModels = customizedModels;
89 }
90
91 public Map<String, Model> getIndex() {
92 return index;
93 }
94
95 public void setIndex(Map<String, Model> index) {
96 this.index = index;
97 }
98
99 public String getIdPrefixFilter() {
100 return idPrefixFilter;
101 }
102
103 public void setIdPrefixFilter(String idPrefixFilter) {
104 this.idPrefixFilter = idPrefixFilter;
105 }
106
107 public boolean isIgnoreUnboundMetadata() {
108 return ignoreUnboundMetadata;
109 }
110
111 public void setIgnoreUnboundMetadata(boolean ignoreUnboundMetadata) {
112 this.ignoreUnboundMetadata = ignoreUnboundMetadata;
113 }
114
115
116 }