1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 package com.macvu.tiles.xmlDefinition;
63
64 import com.macvu.tiles.CacheComponentDefinition;
65 import org.apache.commons.logging.Log;
66 import org.apache.commons.logging.LogFactory;
67 import org.apache.struts.tiles.NoSuchDefinitionException;
68 import org.apache.struts.tiles.xmlDefinition.XmlAttribute;
69
70 import java.util.Iterator;
71
72
73 public class XmlCacheDefinition extends CacheComponentDefinition {
74
75 /***
76 * Commons Logging instance.
77 */
78 protected static Log log = LogFactory.getLog(XmlCacheDefinition.class);
79
80 /***
81 * Used for resolving inheritance.
82 */
83 private boolean isVisited = false;
84
85 /***
86 * Constructor.
87 */
88 public XmlCacheDefinition() {
89 super();
90 }
91
92 /***
93 * Add an attribute to this component.
94 *
95 * @param attribute Attribute to add.
96 */
97 public void addAttribute(XmlAttribute attribute) {
98 putAttribute(attribute.getName(), attribute.getValue());
99 }
100
101 public void addCacheInformation(XmlCacheInformation info) {
102 super.setCacheInformation(info);
103 }
104
105 /***
106 * Set isVisited.
107 */
108 public void setIsVisited(boolean isVisited) {
109 this.isVisited = isVisited;
110 }
111
112 /***
113 * Resolve inheritance.
114 * First, resolve parent's inheritance, then set path to the parent's path.
115 * Also copy attributes setted in parent, and not set in child
116 * If instance doesn't extend anything, do nothing.
117 *
118 * @throws org.apache.struts.tiles.NoSuchDefinitionException
119 * If an inheritance can not be solved.
120 */
121 public void resolveInheritance(XmlCacheDefinitionSet definitionsSet)
122 throws NoSuchDefinitionException {
123
124 if (isVisited || !isExtending())
125 return;
126
127 if (log.isDebugEnabled())
128 log.debug("Resolve definition for child name='" + getName()
129 + "' extends='" + getExtends() + "'.");
130
131
132 setIsVisited(true);
133
134
135 XmlCacheDefinition parent = definitionsSet.getDefinition(getExtends());
136 if (parent == null) {
137 String msg = "Error while resolving definition inheritance: child '"
138 + getName() + "' can't find its ancestor '"
139 + getExtends() + "'. Please check your description file.";
140 log.error(msg);
141
142 throw new NoSuchDefinitionException(msg);
143 }
144
145 parent.resolveInheritance(definitionsSet);
146
147
148 Iterator parentAttributes = parent.getAttributes().keySet().iterator();
149 while (parentAttributes.hasNext()) {
150 String name = (String) parentAttributes.next();
151 if (!getAttributes().containsKey(name))
152 putAttribute(name, parent.getAttribute(name));
153 }
154
155 if (path == null)
156 setPath(parent.getPath());
157 if (role == null)
158 setRole(parent.getRole());
159 if (controller == null) {
160 setController(parent.getController());
161 setControllerType(parent.getControllerType());
162 }
163 }
164
165 /***
166 * Overload this definition with passed child.
167 * All attributes from child are copied to this definition. Previous attributes with
168 * same name are disguarded.
169 * Special attribute 'path','role' and 'extends' are overloaded if defined in child.
170 *
171 * @param child Child used to overload this definition.
172 */
173 public void overload(XmlCacheDefinition child) {
174 if (child.getPath() != null) {
175 path = child.getPath();
176 }
177 if (child.getExtends() != null) {
178 inherit = child.getExtends();
179 }
180 if (child.getRole() != null) {
181 role = child.getRole();
182 }
183 if (child.getController() != null) {
184 controller = child.getController();
185 controllerType = child.getControllerType();
186 }
187
188 attributes.putAll(child.getAttributes());
189 }
190
191 }