Class |
Line # |
Actions |
|||||
---|---|---|---|---|---|---|---|
Navbar | 43 | 48 | 0% | 21 | 24 | ||
Navbar.ImageBrand | 197 | 11 | 0% | 5 | 8 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
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 | * Represents the navbar component. | |
39 | * | |
40 | * @author devacfr | |
41 | * @since 2.0 | |
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 | * Default constructor. | |
74 | * | |
75 | * @param config | |
76 | * a config (can <b>not</b> be {@code null}). | |
77 | */ | |
78 | 17 | public Navbar(@Nonnull final ISkinConfig config) { |
79 | 17 | super(COMPONENT); |
80 | 17 | requireNonNull(config); |
81 | 17 | final MavenProject project = config.getProject(); |
82 | 17 | final Xpp3Dom brand = config.get("brand"); |
83 | 17 | if (brand != null) { |
84 | 6 | final Xpp3Dom name = brand.getChild("name"); |
85 | 6 | if (name != null) { |
86 | 6 | brandName = name.getValue(); |
87 | } | |
88 | 6 | final Xpp3Dom href = brand.getChild("href"); |
89 | 6 | if (href != null) { |
90 | 6 | brandHref = config.relativeLink(href.getValue()); |
91 | } | |
92 | } else { | |
93 | 11 | brandName = project.getName(); |
94 | } | |
95 | 17 | if (isNullOrEmpty(brandName)) { |
96 | 0 | brandName = project.getArtifactId(); |
97 | } | |
98 | 17 | this.center = config.getAttributeValue(COMPONENT, "center", Boolean.class, true); |
99 | 17 | this.alignMenu = config.getAttributeValue(COMPONENT, "alignMenu", String.class, "right"); |
100 | 17 | this.setTheme(config.getAttributeValue(COMPONENT, "theme", String.class, "light")); |
101 | 17 | this.setBackground(config.getAttributeValue(COMPONENT, "background", String.class, "light")); |
102 | 17 | this.setCssClass(config.getAttributeValue(COMPONENT, "cssClass", String.class, null)); |
103 | 17 | this.filterMenu = config.getAttributeValue(COMPONENT, "filterMenu", String.class, null); |
104 | 17 | Xpp3Dom element = config.get(COMPONENT); |
105 | 17 | final Xpp3Dom img = Xpp3Utils.getFirstChild(element, "image", config.getNamespace()); |
106 | 17 | if (img != null) { |
107 | 6 | this.image = new ImageBrand(config, img); |
108 | } else { | |
109 | 11 | this.image = null; |
110 | } | |
111 | 17 | this.additionalMenu = Xpp3Utils.getFirstChild(element, "additionalMenu", config.getNamespace()); |
112 | ||
113 | // add links | |
114 | 17 | final SiteModel model = config.getSiteModel(); |
115 | 17 | if (model.getBody() != null && model.getBody().getLinks() != null) { |
116 | 6 | final List<LinkItem> items = model.getBody().getLinks(); |
117 | 6 | for (final LinkItem item : items) { |
118 | 6 | this.menus.add(new Menu(config, item)); |
119 | } | |
120 | } | |
121 | // add menus | |
122 | 17 | if (model.getBody() != null && model.getBody().getMenus() != null) { |
123 | 6 | final List<org.apache.maven.doxia.site.Menu> menus = model.getBody().getMenus(); |
124 | 6 | for (final org.apache.maven.doxia.site.Menu menu : menus) { |
125 | 30 | if (isNullOrEmpty(menu.getName())) { |
126 | 0 | continue; |
127 | } | |
128 | 30 | if (isNullOrEmpty(this.filterMenu)) { |
129 | 0 | this.menus.add(new Menu(config, menu)); |
130 | 30 | } else if (Menu.matches(this.filterMenu, menu)) { |
131 | 12 | this.menus.add(new Menu(config, menu)); |
132 | } | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | /** | |
138 | * @return the brandName | |
139 | */ | |
140 | 0 | public String getBrandName() { |
141 | 0 | return brandName; |
142 | } | |
143 | ||
144 | /** | |
145 | * @return the brandHref | |
146 | */ | |
147 | 0 | public String getBrandHref() { |
148 | 0 | return brandHref; |
149 | } | |
150 | ||
151 | /** | |
152 | * @return the center | |
153 | */ | |
154 | 0 | public boolean isCenter() { |
155 | 0 | return center; |
156 | } | |
157 | ||
158 | /** | |
159 | * @return the alignMenu | |
160 | */ | |
161 | 0 | public String getAlignMenu() { |
162 | 0 | return alignMenu; |
163 | } | |
164 | ||
165 | /** | |
166 | * @return the image | |
167 | */ | |
168 | 0 | public ImageBrand getImage() { |
169 | 0 | return image; |
170 | } | |
171 | ||
172 | /** | |
173 | * @return the filterMenu | |
174 | */ | |
175 | 0 | public String getFilterMenu() { |
176 | 0 | return filterMenu; |
177 | } | |
178 | ||
179 | /** | |
180 | * @return the additionalMenu | |
181 | */ | |
182 | 0 | public Xpp3Dom getAdditionalMenu() { |
183 | 0 | return additionalMenu; |
184 | } | |
185 | ||
186 | /** | |
187 | * @return the menus | |
188 | */ | |
189 | 0 | public List<Menu> getMenus() { |
190 | 0 | return menus; |
191 | } | |
192 | ||
193 | /** | |
194 | * @author devacfr | |
195 | * @since 2.0 | |
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 | * @param config | |
210 | * a config (can <b>not</b> be {@code null}). | |
211 | * @param element | |
212 | * the element assiciated to image brand. | |
213 | */ | |
214 | 6 | ImageBrand(@Nonnull final ISkinConfig config, @Nonnull final Xpp3Dom element) { |
215 | 6 | Objects.requireNonNull(config); |
216 | 6 | Objects.requireNonNull(element); |
217 | 6 | final String link = config.getAttributeValue(element, "src", String.class, null); |
218 | 6 | if (isNullOrEmpty(link)) { |
219 | 0 | throw new IllegalArgumentException("the attribute 'href' of image element is required"); |
220 | } | |
221 | 6 | this.src = config.eval(link, String.class); |
222 | 6 | this.width = config.getAttributeValue(element, "width", String.class, "30"); |
223 | 6 | this.height = config.getAttributeValue(element, "height", String.class, "30"); |
224 | } | |
225 | ||
226 | /** | |
227 | * @return the src | |
228 | */ | |
229 | 0 | public String getSrc() { |
230 | 0 | return src; |
231 | } | |
232 | ||
233 | /** | |
234 | * @return the height | |
235 | */ | |
236 | 0 | public String getHeight() { |
237 | 0 | return height; |
238 | } | |
239 | ||
240 | /** | |
241 | * @return the width | |
242 | */ | |
243 | 0 | public String getWidth() { |
244 | 0 | return width; |
245 | } | |
246 | } | |
247 | } |