reusable logic for web site navigation
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 21 Apr 2013 13:18:13 +0000 (16:18 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 21 Apr 2013 13:18:13 +0000 (16:18 +0300)
pom.xml
src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java [new file with mode: 0755]
src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java [new file with mode: 0755]

diff --git a/pom.xml b/pom.xml
index 2cfd823..127bbe7 100755 (executable)
--- a/pom.xml
+++ b/pom.xml
                <snapshotRepository>
                        <id>svjatoslav.eu</id>
                        <name>svjatoslav.eu</name>
-                       <url>scp://svjatoslav.eu:7022/var/www/maven</url>
+                       <url>scp://svjatoslav.eu/var/www/svjatoslav.eu/maven</url>
                </snapshotRepository>
                <repository>
                        <id>svjatoslav.eu</id>
                        <name>svjatoslav.eu</name>
-                       <url>scp://svjatoslav.eu:7022/var/www/maven</url>               
+                       <url>scp://svjatoslav.eu/var/www/svjatoslav.eu/maven</url>              
                </repository>
        </distributionManagement>
 
        <scm>
-               <connection>scm:git:ssh://git@svjatoslav.eu:7022/home/git/repositories/svjatoslav_commons.git</connection>
-               <developerConnection>scm:git:ssh://git@svjatoslav.eu:7022/home/git/repositories/svjatoslav_commons.git</developerConnection>
+               <connection>scm:git:ssh://git@svjatoslav.eu/home/git/repositories/svjatoslav_commons.git</connection>
+               <developerConnection>scm:git:ssh://git@svjatoslav.eu/home/git/repositories/svjatoslav_commons.git</developerConnection>
        </scm>
 
 </project>
diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java
new file mode 100755 (executable)
index 0000000..7a1f711
--- /dev/null
@@ -0,0 +1,60 @@
+package eu.svjatoslav.commons.network.navigation;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class Navigation {
+
+       private NavigationItem rootNavigationItem;
+
+       private NavigationItem defaultNavigationItem;
+
+       public NavigationItem getRootNavigationItem() {
+               return rootNavigationItem;
+       }
+
+       public NavigationItem getSelectedItem(final String requestUrl) {
+
+               try {
+                       final String requestPath = new URL(requestUrl).getPath();
+
+                       for (final NavigationItem item : rootNavigationItem.subElements)
+                               if (item.matchesUrl(requestPath))
+                                       return item;
+               } catch (final MalformedURLException e) {
+                       System.out.println("Cannot handle URL: " + requestUrl);
+                       e.printStackTrace();
+               }
+
+               return defaultNavigationItem;
+       }
+
+       public String getTopMenu(final String requestUrl) {
+
+               final NavigationItem selectedItem = getSelectedItem(requestUrl);
+               final StringBuffer result = new StringBuffer();
+
+               result.append("<div id=\"topNavigation\">");
+
+               for (final NavigationItem item : rootNavigationItem.getSubElements()) {
+
+                       if (item == selectedItem)
+                               result.append("<a class=\"selected\" href=\"" + item.getLink()
+                                               + "\">");
+                       else
+                               result.append("<a href=\"" + item.getLink() + "\">");
+
+                       result.append(item.getTitle());
+                       result.append("</a>");
+               }
+
+               result.append("</div>");
+
+               return result.toString();
+       }
+
+       public void setRootNavigationItem(final NavigationItem rootNavigationItem) {
+               this.rootNavigationItem = rootNavigationItem;
+       }
+
+}
diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java
new file mode 100755 (executable)
index 0000000..6392fa9
--- /dev/null
@@ -0,0 +1,51 @@
+package eu.svjatoslav.commons.network.navigation;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import eu.svjatoslav.commons.string.WildCardMatcher;
+
+public class NavigationItem {
+
+       private final String title;
+       private final String link;
+       private final String matchingWildcard;
+
+       ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
+
+       public NavigationItem(final String title, final String link) {
+               this.title = title;
+               this.link = link;
+               matchingWildcard = link;
+       }
+
+       public NavigationItem(final String title, final String link,
+                       final String wildcard) {
+               this.title = title;
+               this.link = link;
+               matchingWildcard = wildcard;
+       }
+
+       public void addSubNavigation(final NavigationItem item) {
+               subElements.add(item);
+       }
+
+       public String getLink() {
+               return link;
+       }
+
+       public List<NavigationItem> getSubElements() {
+               return subElements;
+       }
+
+       public String getTitle() {
+               return title;
+       }
+
+       public boolean matchesUrl(final String url) {
+
+               return WildCardMatcher.match(url, matchingWildcard);
+
+       }
+
+}