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 static com.google.common.base.Strings.isNullOrEmpty;
22  import static java.util.Objects.requireNonNull;
23  
24  import java.util.List;
25  import java.util.Objects;
26  
27  import javax.annotation.Nonnull;
28  
29  import org.apache.maven.doxia.site.LinkItem;
30  import org.devacfr.maven.skins.reflow.ISkinConfig;
31  
32  import com.google.common.collect.Lists;
33  
34  /**
35   * @author Christophe Friederich
36   * @since 2.0
37   */
38  public class Menu {
39  
40      /** */
41      private final String name;
42  
43      /** */
44      private final String alt;
45  
46      /** */
47      private final String border;
48  
49      /** */
50      private final String height;
51  
52      /** */
53      private final String width;
54  
55      /** */
56      private final String href;
57  
58      /** */
59      private final String image;
60  
61      /** */
62      private final String position;
63  
64      /** */
65      private final String target;
66  
67      /** */
68      private final String title;
69  
70      /** **/
71      private final String inherit;
72  
73      /** */
74      private final boolean active;
75  
76      /** */
77      private final List<MenuItem> menuItems = Lists.newArrayList();
78  
79      /**
80       * Initialize with {@link LinkItem}.
81       *
82       * @param config
83       *            a config (can <b>not</b> be {@code null}).
84       * @param item
85       *            link item used to.
86       */
87      public Menu(@Nonnull final ISkinConfig config, @Nonnull final LinkItem item) {
88          Objects.requireNonNull(item);
89          this.alt = item.getAlt();
90          this.border = item.getBorder();
91          this.height = item.getHeight();
92          this.width = item.getWidth();
93          this.href = config.relativeLink(item.getHref());
94          this.image = item.getImg();
95          this.name = item.getName();
96          this.position = item.getPosition();
97          this.target = item.getTarget();
98          this.title = item.getTitle();
99          this.active = config.isActiveLink(this.href);
100         this.inherit = null;
101     }
102 
103     /**
104      * Initialize with {@link org.apache.maven.doxia.site.Menu}.
105      *
106      * @param config
107      *            a config (can <b>not</b> be {@code null}).
108      * @param menu
109      *            menu used to.
110      */
111     public Menu(@Nonnull final ISkinConfig config, @Nonnull final org.apache.maven.doxia.site.Menu menu) {
112         Objects.requireNonNull(menu);
113         this.alt = menu.getAlt();
114         this.border = menu.getBorder();
115         this.height = menu.getHeight();
116         this.width = menu.getWidth();
117         this.href = null;
118         this.image = menu.getImg();
119         this.name = menu.getName();
120         this.position = menu.getPosition();
121         this.target = null;
122         this.title = menu.getTitle();
123         this.inherit = menu.getInherit();
124         this.active = false;
125         final List<org.apache.maven.doxia.site.MenuItem> items = menu.getItems();
126         for (final org.apache.maven.doxia.site.MenuItem menuItem : items) {
127             if (isNullOrEmpty(menu.getName())) {
128                 continue;
129             }
130             this.menuItems.add(new MenuItem(config, menuItem));
131         }
132     }
133 
134     /**
135      * @return the menuItems
136      */
137     public List<MenuItem> getMenuItems() {
138         return menuItems;
139     }
140 
141     /**
142      * @return the name
143      */
144     public String getName() {
145         return name;
146     }
147 
148     /**
149      * @return the inherit
150      */
151     public String getInherit() {
152         return inherit;
153     }
154 
155     /**
156      * @return the active
157      */
158     public boolean isActive() {
159         boolean active = this.active;
160         if (active) {
161             return active;
162         }
163         for (final MenuItem menuItem : menuItems) {
164             active = menuItem.isActive();
165             if (active) {
166                 break;
167             }
168         }
169         return active;
170     }
171 
172     /**
173      * @return the alt
174      */
175     public String getAlt() {
176         return alt;
177     }
178 
179     /**
180      * @return the border
181      */
182     public String getBorder() {
183         return border;
184     }
185 
186     /**
187      * @return the height
188      */
189     public String getHeight() {
190         return height;
191     }
192 
193     /**
194      * @return the href
195      */
196     public String getHref() {
197         return href;
198     }
199 
200     /**
201      * @return the image
202      */
203     public String getImage() {
204         return image;
205     }
206 
207     /**
208      * @return the position
209      */
210     public String getPosition() {
211         return position;
212     }
213 
214     /**
215      * @return the target
216      */
217     public String getTarget() {
218         return target;
219     }
220 
221     /**
222      * @return the title
223      */
224     public String getTitle() {
225         return title;
226     }
227 
228     /**
229      * @return the width
230      */
231     public String getWidth() {
232         return width;
233     }
234 
235     /**
236      * Gets indicating whether menu by their ref or name, and returns the matching results. The regex is used to check
237      * the match.
238      *
239      * @param regex
240      *            regex to use.
241      * @param menu
242      *            the menu to check
243      * @return Returns {@code true} whether menu matches with regex.
244      */
245     public static boolean matches(@Nonnull final String regex, @Nonnull final org.apache.maven.doxia.site.Menu menu) {
246         requireNonNull(regex);
247         requireNonNull(menu);
248         return menu.getRef() != null && menu.getRef().matches(regex)
249                 || menu.getName() != null && menu.getName().matches(regex);
250     }
251 }