View Javadoc
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      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         // add links
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         // add menus
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      * @return the brandName
139      */
140     public String getBrandName() {
141         return brandName;
142     }
143 
144     /**
145      * @return the brandHref
146      */
147     public String getBrandHref() {
148         return brandHref;
149     }
150 
151     /**
152      * @return the center
153      */
154     public boolean isCenter() {
155         return center;
156     }
157 
158     /**
159      * @return the alignMenu
160      */
161     public String getAlignMenu() {
162         return alignMenu;
163     }
164 
165     /**
166      * @return the image
167      */
168     public ImageBrand getImage() {
169         return image;
170     }
171 
172     /**
173      * @return the filterMenu
174      */
175     public String getFilterMenu() {
176         return filterMenu;
177     }
178 
179     /**
180      * @return the additionalMenu
181      */
182     public Xpp3Dom getAdditionalMenu() {
183         return additionalMenu;
184     }
185 
186     /**
187      * @return the menus
188      */
189     public List<Menu> getMenus() {
190         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         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          * @return the src
228          */
229         public String getSrc() {
230             return src;
231         }
232 
233         /**
234          * @return the height
235          */
236         public String getHeight() {
237             return height;
238         }
239 
240         /**
241          * @return the width
242          */
243         public String getWidth() {
244             return width;
245         }
246     }
247 }