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