| 1 25 26 package com.j2biz.blogunity.web; 27 28 import java.util.Enumeration ; 29 import java.util.Locale ; 30 import java.util.Stack ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 import com.j2biz.blogunity.BlogunityManager; 38 import com.j2biz.blogunity.IConstants; 39 import com.j2biz.blogunity.i18n.I18NMessageManager; 40 41 46 public class NavigationStack { 47 50 private static final Log log = LogFactory.getLog(NavigationStack.class); 51 52 private Stack stack; 53 54 57 public NavigationStack() { 58 59 if (log.isDebugEnabled()) { 60 log.debug("new navigation stack created."); 61 } 62 stack = new Stack (); 63 } 64 65 public IActionResult push(IActionResult item) { 66 67 if (log.isDebugEnabled()) { 68 log.debug("Pushing to stack: " + item.getPath()); 69 } 70 71 int len = size(); 73 if (len == 0) stack.push(item); 74 else { 75 IActionResult last = peek(); 76 int indx1 = item.getPath().indexOf("?"); 77 if (indx1 < 0) indx1 = item.getPath().length(); 78 String p1 = item.getPath().substring(0, indx1); 79 80 int indx2 = last.getPath().indexOf("?"); 81 if (indx2 < 0) indx2 = last.getPath().length(); 82 String p2 = last.getPath().substring(0, indx2); 83 84 if (!p1.equals(p2)) { 85 if (len >= 2) { 86 IActionResult r = (IActionResult) stack.elementAt(len - 2); 87 if (r.getPath().equals(item.getPath())) { 88 pop(); 89 } else { 90 stack.push(item); 91 } 92 } else 93 stack.push(item); 94 } 95 96 } 97 98 return item; 99 } 100 101 public synchronized IActionResult pop() { 102 return (IActionResult) stack.pop(); 103 } 104 105 public synchronized void popAllExceptFirst() { 106 if (size() > 1) { 107 while (size() != 1) 109 pop(); 110 } 111 } 112 113 public synchronized IActionResult peek() { 114 return (IActionResult) stack.peek(); 115 } 116 117 public synchronized IActionResult peekNextToLast() { 118 int len = size(); 119 if (len < 2) return null; 120 return (IActionResult) stack.elementAt(len - 2); 121 } 122 123 public boolean empty() { 124 return stack.size() == 0; 125 } 126 127 public int size() { 128 return stack.size(); 129 } 130 131 public void clear() { 132 stack.clear(); 133 } 134 135 public Enumeration elements() { 136 return stack.elements(); 137 } 138 139 144 public String toString() { 145 StringBuffer out = new StringBuffer (); 146 147 if (empty()) { 148 out.append("Navigation stack is empty"); 149 } else { 150 Enumeration e = elements(); 151 while (e.hasMoreElements()) { 152 IActionResult r = (IActionResult) e.nextElement(); 153 out.append("["); 154 out.append(r.getKey()); 155 out.append(":"); 156 out.append(r.getPath()); 157 out.append("] - "); 158 } 159 } 160 return out.toString(); 161 } 162 163 public String toHTML(HttpServletRequest request) { 164 165 Locale loc = Locale.ENGLISH; 166 loc = (Locale ) request.getAttribute(IConstants.Request.LOCALE); 167 if (loc == null) loc = BlogunityManager.getSystemConfiguration().getSystemLocale(); 168 169 StringBuffer out = new StringBuffer (); 170 171 if (!empty()) { 172 String ctx = request.getContextPath(); 173 Enumeration e = elements(); 174 while (e.hasMoreElements()) { 175 176 IActionResult r = (IActionResult) e.nextElement(); 177 178 out.append("<a class=\"naviLink\" HREF=\""); 179 out.append(ctx); 180 out.append(r.getPath()); 181 out.append("\">"); 182 out.append(I18NMessageManager.getInstance().getNavigationText(r.getKey(), loc)); 183 out.append("</a>\n"); 184 185 } 186 } 187 return out.toString(); 188 } 189 190 } | Popular Tags |