0deca95871842954d2d88dd491904ec5ebe47622
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / network / navigation / NavigationItem.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.network.navigation;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import eu.svjatoslav.commons.network.Locale;
18 import eu.svjatoslav.commons.string.WildCardMatcher;
19
20 public class NavigationItem {
21
22         private String matchingPattern;
23         private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
24         ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
25         private Locale[] localeOrder;
26         private NavigationItem parent;
27         private final String linkUrl;
28
29         public NavigationItem(final Locale... localeOrder) {
30                 if (localeOrder.length == 0)
31                         this.localeOrder = new Locale[] { Locale.ENG };
32                 else
33                         this.localeOrder = localeOrder;
34
35                 matchingPattern = null;
36                 linkUrl = null;
37         }
38
39         public NavigationItem(final NavigationItem parent, final String linkUrl,
40                         final String... titles) {
41                 this.parent = parent;
42                 this.linkUrl = linkUrl;
43                 matchingPattern = linkUrl;
44
45                 initializeLocalizedTitles(titles);
46         }
47
48         public NavigationItem addChild(final String linkUrl, final String... titles) {
49
50                 final NavigationItem item = new NavigationItem(this, linkUrl, titles);
51                 subElements.add(item);
52
53                 return item;
54         }
55
56         public String getLinkUrl() {
57                 return linkUrl;
58         }
59
60         public Locale[] getLocaleOrder() {
61                 if (localeOrder != null)
62                         return localeOrder;
63
64                 return parent.getLocaleOrder();
65         }
66
67         NavigationItem getMatchingNavigationItem(final String requestPath) {
68                 if (matchesUrl(requestPath))
69                         return this;
70
71                 for (final NavigationItem childNavigationItem : subElements) {
72                         final NavigationItem match = childNavigationItem
73                                         .getMatchingNavigationItem(requestPath);
74
75                         if (match != null)
76                                 return match;
77                 }
78                 return null;
79         }
80
81         public List<NavigationItem> getSubElements() {
82                 return subElements;
83         }
84
85         public String getTitle() {
86                 if (localeToTitle.size() != 1)
87                         throw new RuntimeException("there shall be exactly one title");
88
89                 return localeToTitle.values().iterator().next();
90         }
91
92         public String getTitle(final Locale locale) {
93                 return localeToTitle.get(locale);
94         }
95
96         private void initializeLocalizedTitles(final String... titles) {
97                 final Locale[] locales = getLocaleOrder();
98                 if (locales.length != titles.length)
99                         throw new RuntimeException("There should be exactly "
100                                         + locales.length + " title(s).");
101
102                 for (int i = 0; i < titles.length; i++)
103                         localeToTitle.put(locales[i], titles[i]);
104         }
105
106         public boolean matchesUrl(final String url) {
107                 return WildCardMatcher.match(url, matchingPattern);
108         }
109
110         public NavigationItem setPattern(final String pattern) {
111                 matchingPattern = pattern;
112                 return this;
113         }
114
115 }