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