1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.devacfr.maven.skins.reflow.model;
20
21 import javax.annotation.Nonnull;
22
23 import java.util.List;
24 import java.util.Objects;
25
26 import com.google.common.collect.Lists;
27 import org.apache.maven.doxia.site.SiteModel;
28 import org.apache.maven.doxia.site.LinkItem;
29 import org.apache.maven.project.MavenProject;
30 import org.codehaus.plexus.util.xml.Xpp3Dom;
31 import org.devacfr.maven.skins.reflow.ISkinConfig;
32 import org.devacfr.maven.skins.reflow.Xpp3Utils;
33
34 import static com.google.common.base.Strings.isNullOrEmpty;
35 import static java.util.Objects.requireNonNull;
36
37
38
39
40
41
42
43 public class Navbar extends BsComponent {
44
45
46 public static final String COMPONENT = "navbar";
47
48
49 private String brandName;
50
51
52 private String brandHref;
53
54
55 private final String filterMenu;
56
57
58 private final boolean center;
59
60
61 private final String alignMenu;
62
63
64 private final ImageBrand image;
65
66
67 private final List<Menu> menus = Lists.newArrayList();
68
69
70 private final Xpp3Dom additionalMenu;
71
72
73
74
75
76
77
78 public Navbar(@Nonnull final ISkinConfig config) {
79 super(COMPONENT);
80 requireNonNull(config);
81 final MavenProject project = config.getProject();
82 final Xpp3Dom brand = config.get("brand");
83 if (brand != null) {
84 final Xpp3Dom name = brand.getChild("name");
85 if (name != null) {
86 brandName = name.getValue();
87 }
88 final Xpp3Dom href = brand.getChild("href");
89 if (href != null) {
90 brandHref = config.relativeLink(href.getValue());
91 }
92 } else {
93 brandName = project.getName();
94 }
95 if (isNullOrEmpty(brandName)) {
96 brandName = project.getArtifactId();
97 }
98 this.center = config.getAttributeValue(COMPONENT, "center", Boolean.class, true);
99 this.alignMenu = config.getAttributeValue(COMPONENT, "alignMenu", String.class, "right");
100 this.setTheme(config.getAttributeValue(COMPONENT, "theme", String.class, "light"));
101 this.setBackground(config.getAttributeValue(COMPONENT, "background", String.class, "light"));
102 this.setCssClass(config.getAttributeValue(COMPONENT, "cssClass", String.class, null));
103 this.filterMenu = config.getAttributeValue(COMPONENT, "filterMenu", String.class, null);
104 Xpp3Dom element = config.get(COMPONENT);
105 final Xpp3Dom img = Xpp3Utils.getFirstChild(element, "image", config.getNamespace());
106 if (img != null) {
107 this.image = new ImageBrand(config, img);
108 } else {
109 this.image = null;
110 }
111 this.additionalMenu = Xpp3Utils.getFirstChild(element, "additionalMenu", config.getNamespace());
112
113
114 final SiteModel model = config.getSiteModel();
115 if (model.getBody() != null && model.getBody().getLinks() != null) {
116 final List<LinkItem> items = model.getBody().getLinks();
117 for (final LinkItem item : items) {
118 this.menus.add(new Menu(config, item));
119 }
120 }
121
122 if (model.getBody() != null && model.getBody().getMenus() != null) {
123 final List<org.apache.maven.doxia.site.Menu> menus = model.getBody().getMenus();
124 for (final org.apache.maven.doxia.site.Menu menu : menus) {
125 if (isNullOrEmpty(menu.getName())) {
126 continue;
127 }
128 if (isNullOrEmpty(this.filterMenu)) {
129 this.menus.add(new Menu(config, menu));
130 } else if (Menu.matches(this.filterMenu, menu)) {
131 this.menus.add(new Menu(config, menu));
132 }
133 }
134 }
135 }
136
137
138
139
140 public String getBrandName() {
141 return brandName;
142 }
143
144
145
146
147 public String getBrandHref() {
148 return brandHref;
149 }
150
151
152
153
154 public boolean isCenter() {
155 return center;
156 }
157
158
159
160
161 public String getAlignMenu() {
162 return alignMenu;
163 }
164
165
166
167
168 public ImageBrand getImage() {
169 return image;
170 }
171
172
173
174
175 public String getFilterMenu() {
176 return filterMenu;
177 }
178
179
180
181
182 public Xpp3Dom getAdditionalMenu() {
183 return additionalMenu;
184 }
185
186
187
188
189 public List<Menu> getMenus() {
190 return menus;
191 }
192
193
194
195
196
197 public static class ImageBrand {
198
199
200 private final String src;
201
202
203 private final String width;
204
205
206 private final String height;
207
208
209
210
211
212
213
214 ImageBrand(@Nonnull final ISkinConfig config, @Nonnull final Xpp3Dom element) {
215 Objects.requireNonNull(config);
216 Objects.requireNonNull(element);
217 final String link = config.getAttributeValue(element, "src", String.class, null);
218 if (isNullOrEmpty(link)) {
219 throw new IllegalArgumentException("the attribute 'href' of image element is required");
220 }
221 this.src = config.eval(link, String.class);
222 this.width = config.getAttributeValue(element, "width", String.class, "30");
223 this.height = config.getAttributeValue(element, "height", String.class, "30");
224 }
225
226
227
228
229 public String getSrc() {
230 return src;
231 }
232
233
234
235
236 public String getHeight() {
237 return height;
238 }
239
240
241
242
243 public String getWidth() {
244 return width;
245 }
246 }
247 }