1 package de.matthias_burbach.mosaique.core.model;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 /***
7 * Is a Tiles definition as defined in a Tiles definitions file.
8 *
9 * @author Matthias Burbach
10 */
11 public class Definition extends AbstractFileItem {
12 /***
13 * The identifying name of this definition.
14 */
15 private String name;
16
17 /***
18 * The name of the definition this definition is derived from.
19 */
20 private String extendz;
21
22 /***
23 * The relative path of the JSP this definition defines to be associated
24 * with. Is <code>null</code> if this definition inherits the page
25 * attribute from a base definition.
26 */
27 private String page;
28
29 /***
30 * The put attributes of type {@link Put} this definition directly defines
31 * (excluding inherited ones).
32 */
33 private List puts;
34
35 /***
36 * The Tiles context of this definition defined by its own attributes plus
37 * the ones inherited from the extension hierarchy.
38 */
39 private TilesContext tilesContext;
40
41 /***
42 * @param context The context to use by this instance.
43 */
44 public Definition(final Context context) {
45 setContext(context);
46 }
47
48 /***
49 * @return The name of the definition this definition is derived from.
50 */
51 public String getExtendz() {
52 return extendz;
53 }
54
55 /***
56 * @param extendz The name of the definition this definition is derived
57 * from.
58 */
59 public void setExtendz(final String extendz) {
60 this.extendz = extendz;
61 }
62
63 /***
64 * @return The relative path of the JSP this definition defines to be
65 * associated with. Is <code>null</code> if this definition inherits
66 * the page attribute from a base definition.
67 */
68 public String getPage() {
69 return page;
70 }
71
72 /***
73 * @param page The relative path of the JSP this definition defines to be
74 * associated with. Is <code>null</code> if this definition
75 * inherits the page attribute from a base definition.
76 */
77 public void setPage(final String page) {
78 this.page = page;
79 }
80
81 /***
82 * @return The JSP resolved from the self-defined or inherited page
83 * attribute.
84 */
85 public Jsp getJsp() {
86 String inheritedPage = getInheritedPage();
87 Jsp jsp = null;
88 if (inheritedPage != null) {
89 jsp = getJspResolver().resolve(inheritedPage);
90 }
91 if (jsp == null) {
92 setErroneous(true);
93 setMessage(
94 "Could not resolve the JSP for page='"
95 + inheritedPage
96 + "'.");
97 }
98 return jsp;
99 }
100
101 /***
102 * @return The identifying name of this definition.
103 */
104 public String getName() {
105 return name;
106 }
107
108 /***
109 * @param name The identifying name of this definition.
110 */
111 public void setName(final String name) {
112 this.name = name;
113 }
114
115 /***
116 * @return The parent definition this definition extends.
117 * Is <code>null</code> if there is no parent.
118 */
119 public Definition getParent() {
120 Definition parent = null;
121 if (extendz != null) {
122 parent = getDefinitionResolver().resolve(extendz);
123 if (parent == null) {
124 setErroneous(true);
125 setMessage(
126 "Could not resolve the parent definition for extends='"
127 + extendz
128 + "'.");
129 }
130 }
131 return parent;
132 }
133
134 /***
135 * @return The put attributes of type {@link Put} this definition directly
136 * defines (excluding inherited ones).
137 */
138 public List getPuts() {
139 return puts;
140 }
141
142 /***
143 * @param puts The put attributes of type {@link Put} this definition
144 * directly defines (excluding inherited ones).
145 */
146 public void setPuts(final List puts) {
147 this.puts = puts;
148 }
149
150 /***
151 * @return The self-defined or inherited page attribute.
152 */
153 public String getInheritedPage() {
154 String result = page;
155 if (result == null) {
156 Definition parent = getParent();
157 if (parent != null) {
158 result = parent.getInheritedPage();
159 }
160 }
161 return result;
162 }
163
164 /***
165 * @return The Tiles context of this definition defined by its own
166 * attributes plus the ones inherited from the extension hierarchy.
167 */
168 public TilesContext getTilesContext() {
169 if (tilesContext == null) {
170 Definition parent = getParent();
171 if (parent != null) {
172 tilesContext = parent.getTilesContext();
173 } else {
174 tilesContext = new TilesContext();
175 }
176 for (Iterator iter = puts.iterator(); iter.hasNext();) {
177 Put put = (Put) iter.next();
178 tilesContext.getPuts().put(put.getName(), put);
179 }
180 }
181 return tilesContext;
182 }
183
184 /***
185 * @return The level of this definition in its extension hierarchy.
186 * Is 0 if this definition is a root definition. Is n if this
187 * definition has n parents.
188 */
189 public int getLevel() {
190 int result = 0;
191 Definition parent = getParent();
192 if (parent != null) {
193 result = parent.getLevel() + 1;
194 }
195 return result;
196 }
197 }