1 13 package info.magnolia.cms.taglibs.util; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ContentHandler; 17 import info.magnolia.cms.core.ItemType; 18 import info.magnolia.cms.util.Resource; 19 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.jcr.RepositoryException; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.JspWriter ; 30 import javax.servlet.jsp.tagext.TagSupport ; 31 32 import org.apache.commons.lang.StringEscapeUtils; 33 import org.apache.commons.lang.StringUtils; 34 import org.apache.commons.lang.exception.NestableRuntimeException; 35 import org.apache.log4j.Logger; 36 37 38 70 public class SimpleNavigationTag extends TagSupport { 71 72 75 private static final String NODEDATA_NAVIGATIONTITLE = "navTitle"; 77 80 public static final String NODEDATA_ACCESSKEY = "accessKey"; 82 85 public static final String DEFAULT_OPENMENU_NODEDATA = "openMenu"; 87 90 public static final String DEFAULT_HIDEINNAV_NODEDATA = "hideInNav"; 92 95 private static final long serialVersionUID = 222L; 96 97 100 private static Logger log = Logger.getLogger(SimpleNavigationTag.class); 101 102 105 private int startLevel; 106 107 110 private int endLevel; 111 112 115 private String hideInNav; 116 117 120 private String openMenu; 121 122 125 private String style; 126 127 130 private boolean expandAll; 131 132 136 public void setStartLevel(int startLevel) { 137 this.startLevel = startLevel; 138 } 139 140 144 public void setEndLevel(int endLevel) { 145 this.endLevel = endLevel; 146 } 147 148 152 public void setStyle(String style) { 153 this.style = style; 154 } 155 156 160 public void setHideInNav(String hideInNav) { 161 this.hideInNav = hideInNav; 162 } 163 164 168 public void setOpenMenu(String openMenu) { 169 this.openMenu = openMenu; 170 } 171 172 175 public void setExpandAll(boolean expandAll) { 176 this.expandAll = expandAll; 177 } 178 179 182 public int doEndTag() throws JspException { 183 Content activePage = Resource.getActivePage((HttpServletRequest ) this.pageContext.getRequest()); 184 JspWriter out = this.pageContext.getOut(); 185 try { 186 if (style != null) { 187 out.println("<span class=\"" + style + "\">"); } 189 drawChildren(activePage.getAncestor(this.startLevel), activePage, out); 190 if (style != null) { 191 out.println("</span>"); } 193 } 194 catch (RepositoryException e) { 195 log.error("RepositoryException caught while drawing navigation: " + e.getMessage(), e); return EVAL_PAGE; 197 } 198 catch (IOException e) { 199 throw new NestableRuntimeException(e); 201 } 202 return EVAL_PAGE; 203 } 204 205 208 public void release() { 209 this.startLevel = 0; 210 this.hideInNav = null; 211 this.openMenu = null; 212 super.release(); 213 } 214 215 223 private void drawChildren(Content page, Content activePage, JspWriter out) throws IOException , RepositoryException { 224 225 Collection children = page.getChildren(ItemType.CONTENT, ContentHandler.SORT_BY_SEQUENCE); 226 227 if (children.size() == 0) { 228 return; 229 } 230 231 if (startLevel > endLevel) { 232 endLevel = 0; 233 } 234 235 out.print("<ul class=\"level"); out.print(page.getLevel()); 237 out.print("\">"); 239 Iterator it = children.iterator(); 240 while (it.hasNext()) { 241 Content child = (Content) it.next(); 242 243 if (!expandAll) { 244 if (child 245 .getNodeData(StringUtils.defaultString(this.hideInNav, DEFAULT_HIDEINNAV_NODEDATA)) 246 .getBoolean()) { 247 continue; 248 } 249 } 250 251 List cssClasses = new ArrayList (3); 252 253 String title = child.getNodeData(NODEDATA_NAVIGATIONTITLE).getString(StringUtils.EMPTY); 254 255 if (StringUtils.isEmpty(title)) { 257 title = child.getTitle(); 258 } 259 260 if (StringUtils.isEmpty(title)) { 262 title = child.getName(); 263 } 264 265 boolean showChildren; 266 boolean self = false; 267 268 if (expandAll) { 269 showChildren = true; 270 } 271 else { 272 273 if (activePage.getHandle().equals(child.getHandle())) { 274 showChildren = true; 276 self = true; 277 cssClasses.add("active"); } 279 else { 280 showChildren = (child.getLevel() <= activePage.getAncestors().size() && activePage.getAncestor( 281 child.getLevel()).getHandle().equals(child.getHandle())); 282 } 283 284 if (!showChildren) { 285 showChildren = child.getNodeData( 286 StringUtils.defaultString(this.openMenu, DEFAULT_OPENMENU_NODEDATA)).getBoolean(); 287 } 288 } 289 290 if (endLevel > 0) { 291 showChildren &= child.getLevel() < endLevel; 292 } 293 294 cssClasses.add(hasVisibleChildren(child) ? (showChildren ? "open" : "closed") : "leaf"); 296 StringBuffer css = new StringBuffer (cssClasses.size() * 10); 297 Iterator iterator = cssClasses.iterator(); 298 while (iterator.hasNext()) { 299 css.append(iterator.next()); 300 if (iterator.hasNext()) { 301 css.append(" "); } 303 } 304 305 out.print("<li class=\""); out.print(css.toString()); 307 out.print("\">"); 309 if (self) { 310 out.println("<strong>"); } 312 313 String accesskey = child.getNodeData(NODEDATA_ACCESSKEY).getString(StringUtils.EMPTY); 314 315 out.print("<a HREF=\""); out.print(((HttpServletRequest ) this.pageContext.getRequest()).getContextPath()); 317 out.print(child.getHandle()); 318 out.print(".html\""); 320 if (StringUtils.isNotEmpty(accesskey)) { 321 out.print(" accesskey=\""); out.print(accesskey); 323 out.print("\""); } 325 326 out.print(">"); 328 out.print(StringEscapeUtils.escapeHtml(title)); 329 out.print(" </a>"); 331 if (self) { 332 out.println("</strong>"); } 334 335 if (showChildren) { 336 drawChildren(child, activePage, out); 337 } 338 out.print("</li>"); } 340 341 out.print("</ul>"); } 343 344 350 private boolean hasVisibleChildren(Content page) { 351 Iterator it = page.getChildren().iterator(); 352 if (it.hasNext() && expandAll) { 353 return true; 354 } 355 while (it.hasNext()) { 356 Content ch = (Content) it.next(); 357 if (!ch.getNodeData(StringUtils.defaultString(this.hideInNav, DEFAULT_HIDEINNAV_NODEDATA)).getBoolean()) { 358 return true; 359 } 360 } 361 return false; 362 } 363 364 } 365 | Popular Tags |