1 22 23 package org.meshcms.taglib; 24 25 import java.io.*; 26 import java.util.*; 27 import org.meshcms.core.*; 28 import org.meshcms.util.*; 29 30 33 public final class ListMenu extends AbstractTag { 34 public static final String ITEMS_ALL = "all"; 35 public static final String ITEMS_ON_PATH = "onpath"; 36 public static final String ITEMS_FIRST_LEVEL = "firstlevel"; 37 public static final String ITEMS_LAST_LEVEL = "lastlevel"; 38 public static final String ITEMS_CHILDREN = "children"; 39 public static final String ITEMS_INTERMEDIATE_LEVELS = "intermediatelevels"; 40 public static final String TEXT = "text"; 41 public static final String LINK = "link"; 42 43 private String indentation = " "; 44 private String indentBuffer = ""; 45 46 private String path; 47 private String items; 48 private String style; 49 private String current; 50 private String currentStyle; 51 private String currentPathStyle; 52 private String allowHiding; 53 54 boolean itemsAll; 55 boolean itemsOnPath; 56 boolean itemsFirstLevel; 57 boolean itemsLastLevel; 58 boolean itemsChildren; 59 boolean itemsIntermediateLevels; 60 61 public void writeTag() throws IOException { 62 if (items == null) { 63 itemsAll = itemsIntermediateLevels = false; 64 itemsOnPath = itemsFirstLevel = itemsLastLevel = itemsChildren = true; 65 } else { 66 StringTokenizer st = new StringTokenizer(items.toLowerCase(), ",;: "); 67 68 while (st.hasMoreTokens()) { 69 String token = st.nextToken(); 70 71 if (token.equals(ITEMS_ALL)) { 72 itemsAll = true; 73 } else if (token.equals(ITEMS_CHILDREN)) { 74 itemsChildren = true; 75 } else if (token.equals(ITEMS_FIRST_LEVEL)) { 76 itemsFirstLevel = true; 77 } else if (token.equals(ITEMS_INTERMEDIATE_LEVELS)) { 78 itemsIntermediateLevels = true; 79 } else if (token.equals(ITEMS_LAST_LEVEL)) { 80 itemsLastLevel = true; 81 } else if (token.equals(ITEMS_ON_PATH)) { 82 itemsOnPath = true; 83 } 84 } 85 } 86 87 boolean linkCurrent = current != null && current.equalsIgnoreCase(LINK); 88 89 SiteMap siteMap = webSite.getSiteMap(); 90 SiteInfo siteInfo = webSite.getSiteInfo(); 91 Path rootPath = (path == null) ? siteInfo.getThemeRoot(pagePath) : new Path(path); 92 Path pathInMenu = webSite.getSiteMap().getPathInMenu(pagePath); 93 int baseLevel = rootPath.getElementCount() + 1; 94 Writer outWriter = getOut(); 95 int lastLevel = rootPath.getElementCount(); 96 SiteMapIterator iter = new SiteMapIterator(webSite, rootPath); 97 iter.setSkipHiddenSubPages(Utils.isTrue(allowHiding)); 98 boolean styleNotApplied = !Utils.isNullOrEmpty(style); 99 int showLastLevel = -1; 100 101 while (iter.hasNext()) { 102 PageInfo current = (PageInfo) iter.next(); 103 Path currentPath = current.getPath(); 104 int level = Math.max(baseLevel, current.getLevel()); 105 106 if ( current.getLevel() <= showLastLevel ) 107 showLastLevel = -1; 108 109 if ( siteInfo.getHideSubmenu(currentPath) && showLastLevel == -1 ) 110 showLastLevel = current.getLevel(); 111 112 boolean add = false; 113 114 if (itemsAll) { 115 add = true; 116 } else { 117 Path parentPath = currentPath.getParent(); 118 119 if (parentPath.isRelative() || pathInMenu.isContainedIn(parentPath)) { 120 if (itemsOnPath && pathInMenu.isContainedIn(currentPath)) { 121 add = true; 122 } else if (level <= baseLevel) { 123 add = itemsFirstLevel; 124 } else if (currentPath.getElementCount() == pathInMenu.getElementCount()) { 125 add = itemsLastLevel; 126 } else if (currentPath.getElementCount() > pathInMenu.getElementCount()) { 127 add = itemsChildren; 128 } else { 129 add = itemsIntermediateLevels; 130 } 131 } 132 } 133 134 if (add) { 135 for (int i = lastLevel - 1; i >= level; i--) { 137 writeIndented(outWriter, "</li>", i + 1); 138 writeIndented(outWriter, "</ul>", i); 139 } 140 141 for (int i = lastLevel; i < level; i++) { 143 if (styleNotApplied) { 144 writeIndented(outWriter, "<ul class=\"" + style + "\">", i); 145 styleNotApplied = false; 146 } else { 147 writeIndented(outWriter, "<ul>", i); 148 } 149 writeIndented(outWriter, "<li>", i + 1); 150 } 151 152 if (level <= lastLevel) { 154 writeIndented(outWriter, "</li>", level); 155 writeIndented(outWriter, "<li>", level); 156 } 157 158 if ( current.getLevel() > 0 && pathInMenu.isContainedIn(currentPath) 159 && ! current.getPath().equals(pathInMenu) 160 && ! Utils.isNullOrEmpty(currentPathStyle)) { 161 outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) + 162 "\" class='" + currentPathStyle + "'>" + 163 siteInfo.getPageTitle(current) + "</a>"); 164 } else if (current.getPath().equals(pathInMenu)) { 165 if (isEdit || linkCurrent) { 166 if (! Utils.isNullOrEmpty(currentStyle) || ! Utils.isNullOrEmpty(currentPathStyle)) { 167 outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) + 168 "\" class='" + (currentPathStyle+" "+currentStyle).trim() + "'>" + 169 siteInfo.getPageTitle(current) + "</a>"); 170 } else { 171 outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) + "\">" + 172 siteInfo.getPageTitle(current) + "</a>"); 173 } 174 } else { 175 if (Utils.isNullOrEmpty(currentStyle)) { 176 outWriter.write(siteInfo.getPageTitle(current)); 177 } else { 178 outWriter.write("<span class='" + currentStyle + "'>" + 179 siteInfo.getPageTitle(current) + "</span>"); 180 } 181 } 182 } else { 183 outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) + "\">" + 184 siteInfo.getPageTitle(current) + "</a>"); 185 } 186 187 lastLevel = level; 188 } 189 } 190 191 for (int i = lastLevel - 1; i >= rootPath.getElementCount(); i--) { 192 writeIndented(outWriter, "</li>", i + 1); 193 writeIndented(outWriter, "</ul>", i); 194 } 195 } 196 197 private void writeIndented(Writer w, String s, int level) throws IOException { 198 while (indentBuffer.length() < indentation.length() * level) { 199 indentBuffer += indentation; 200 } 201 202 w.write('\n'); 203 w.write(indentBuffer, 0, indentation.length() * level); 204 w.write(s); 205 } 206 207 public String getPath() { 208 return path; 209 } 210 211 public void setPath(String path) { 212 this.path = path; 213 } 214 215 public String getItems() { 216 return items; 217 } 218 219 public void setItems(String items) { 220 this.items = items; 221 } 222 223 public String getStyle() { 224 return style; 225 } 226 227 public void setStyle(String style) { 228 this.style = style; 229 } 230 231 public String getCurrent() { 232 return current; 233 } 234 235 public void setCurrent(String current) { 236 this.current = current; 237 } 238 239 public String getCurrentStyle() { 240 return currentStyle; 241 } 242 243 public void setCurrentStyle(String currentStyle) { 244 this.currentStyle = currentStyle; 245 } 246 247 public String getCurrentPathStyle() { 248 return currentPathStyle; 249 } 250 251 public void setCurrentPathStyle(String currentPathStyle) { 252 this.currentPathStyle = currentPathStyle; 253 } 254 public String getAllowHiding() { 255 return allowHiding; 256 } 257 258 public void setAllowHiding(String allowHiding) { 259 this.allowHiding = allowHiding; 260 } 261 } 262 | Popular Tags |