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.Image;
26  import org.apache.maven.doxia.site.LinkItem;
27  import org.devacfr.maven.skins.reflow.ISkinConfig;
28  
29  /**
30   * @author Christophe Friederich
31   * @since 2.0
32   */
33  public class Menu {
34  
35    /** */
36    private final String name;
37  
38    /** */
39    private final String href;
40  
41    /** */
42    private final Image image;
43  
44    /** */
45    private final String target;
46  
47    /** **/
48    private final String inherit;
49  
50    /** */
51    private final boolean active;
52  
53    /** */
54    private final List<MenuItem> menuItems = Lists.newArrayList();
55  
56    /**
57     * Initialize with {@link LinkItem}.
58     *
59     * @param config
60     *          a config (can <b>not</b> be {@code null}).
61     * @param item
62     *          link item used to.
63     */
64    public Menu(@Nonnull final ISkinConfig config, @Nonnull final LinkItem item) {
65      Objects.requireNonNull(item);
66      this.href = config.relativeLink(item.getHref());
67      this.image = item.getImage();
68      this.name = item.getName();
69      this.target = item.getTarget();
70      this.active = config.isActiveLink(this.href);
71      this.inherit = null;
72    }
73  
74    /**
75     * Initialize with {@link org.apache.maven.doxia.site.Menu}.
76     *
77     * @param config
78     *          a config (can <b>not</b> be {@code null}).
79     * @param menu
80     *          menu used to.
81     */
82    public Menu(@Nonnull final ISkinConfig config, @Nonnull final org.apache.maven.doxia.site.Menu menu) {
83      Objects.requireNonNull(menu);
84      this.href = null;
85      this.image = menu.getImage();
86      this.name = menu.getName();
87      this.target = null;
88      this.inherit = menu.getInherit();
89      this.active = false;
90      final List<org.apache.maven.doxia.site.MenuItem> items = menu.getItems();
91      for (final org.apache.maven.doxia.site.MenuItem menuItem : items) {
92        if (isNullOrEmpty(menu.getName())) {
93          continue;
94        }
95        this.menuItems.add(new MenuItem(config, menuItem));
96      }
97    }
98  
99    /**
100    * @return the menuItems
101    */
102   public List<MenuItem> getMenuItems() {
103     return menuItems;
104   }
105 
106   /**
107    * @return the name
108    */
109   public String getName() {
110     return name;
111   }
112 
113   /**
114    * @return the inherit
115    */
116   public String getInherit() {
117     return inherit;
118   }
119 
120   /**
121    * @return the active
122    */
123   public boolean isActive() {
124     boolean active = this.active;
125     if (active) {
126       return active;
127     }
128     for (final MenuItem menuItem : menuItems) {
129       active = menuItem.isActive();
130       if (active) {
131         break;
132       }
133     }
134     return active;
135   }
136 
137   /**
138    * @return the href
139    */
140   public String getHref() {
141     return href;
142   }
143 
144   /**
145    * @return the image
146    */
147   public Image getImage() {
148     return image;
149   }
150 
151   /**
152    * @return the target
153    */
154   public String getTarget() {
155     return target;
156   }
157 
158   /**
159    * Gets indicating whether menu by their ref or name, and returns the matching results. The regex is used to check the
160    * match.
161    *
162    * @param regex
163    *          regex to use.
164    * @param menu
165    *          the menu to check
166    * @return Returns {@code true} whether menu matches with regex.
167    */
168   public static boolean matches(@Nonnull final String regex, @Nonnull final org.apache.maven.doxia.site.Menu menu) {
169     requireNonNull(regex);
170     requireNonNull(menu);
171     return menu.getRef() != null && menu.getRef().matches(regex)
172         || menu.getName() != null && menu.getName().matches(regex);
173   }
174 }