1 11 12 package org.jivesoftware.admin; 13 14 import org.jivesoftware.util.StringUtils; 15 import org.jivesoftware.util.LocaleUtils; 16 import org.dom4j.Element; 17 18 import javax.servlet.jsp.tagext.BodyTagSupport ; 19 import javax.servlet.jsp.JspException ; 20 import javax.servlet.jsp.JspWriter ; 21 import javax.servlet.http.HttpServletRequest ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.io.IOException ; 25 26 60 public class SidebarTag extends BodyTagSupport { 61 62 private String bean; 63 private String css; 64 private String currentcss; 65 private String headercss; 66 private SubSidebarTag subsidebarTag; 67 68 71 public String getBean() { 72 return bean; 73 } 74 75 78 public void setBean(String bean) { 79 this.bean = bean; 80 } 81 82 85 public String getCss() { 86 return clean(css); 87 } 88 89 92 public void setCss(String css) { 93 this.css = css; 94 } 95 96 100 public String getCurrentcss() { 101 return clean(currentcss); 102 } 103 104 107 public void setCurrentcss(String currentcss) { 108 this.currentcss = currentcss; 109 } 110 111 114 public String getHeadercss() { 115 return headercss; 116 } 117 118 121 public void setHeadercss(String headercss) { 122 this.headercss = headercss; 123 } 124 125 128 public SubSidebarTag getSubsidebarTag() { 129 return subsidebarTag; 130 } 131 132 135 public void setSubSidebar(SubSidebarTag subsidebarTag) { 136 this.subsidebarTag = subsidebarTag; 137 } 138 139 142 public int doStartTag() throws JspException { 143 return EVAL_BODY_BUFFERED; 144 } 145 146 155 public int doEndTag() throws JspException { 156 HttpServletRequest request = (HttpServletRequest )pageContext.getRequest(); 158 159 if (getBodyContent().getString() == null) { 161 throw new JspException ("Error, no template (body value) set for the sidebar tag."); 162 } 163 if (subsidebarTag.getBody() == null) { 164 throw new JspException ("Error, no template (body value) set for the subsidebar tag"); 165 } 166 167 AdminPageBean pageInfo = (AdminPageBean)request.getAttribute(getBean()); 169 170 if (pageInfo != null) { 172 173 String subPageID = pageInfo.getSubPageID(); 175 String pageID = pageInfo.getPageID(); 176 177 if (subPageID != null || pageID != null) { 180 181 if (pageID == null) { 182 Element subPage = AdminConsole.getElemnetByID(subPageID); 183 pageID = subPage.getParent().getParent().attributeValue("id"); 184 } 185 186 if (AdminConsole.getModel().elements().size() > 0) { 188 JspWriter out = pageContext.getOut(); 189 StringBuilder buf = new StringBuilder (); 190 191 Element current = null; 192 Element subcurrent = null; 193 if (subPageID != null) { 194 subcurrent = AdminConsole.getElemnetByID(subPageID); 195 } 196 current = AdminConsole.getElemnetByID(pageID); 197 198 Element currentTab = (Element)AdminConsole.getModel().selectSingleNode( 199 "//*[@id='" + pageID + "']/ancestor::tab"); 200 201 boolean isSubmenu = false; 202 if (subcurrent != null) { 203 isSubmenu = subcurrent.getParent().getParent().getName().equals("item"); 204 } 205 206 if (currentTab != null) { 208 Collection items = currentTab.elements(); 209 if (items.size() > 0) { 210 buf.append("<ul>"); 211 for (Iterator iter=items.iterator(); iter.hasNext(); ) { 212 Element sidebar = (Element)iter.next(); 213 String header = sidebar.attributeValue("name"); 214 String hcss = getHeadercss(); 216 if (hcss == null) { 217 hcss = ""; 218 } 219 buf.append("<li class=\"").append(hcss).append("\">").append(clean(i18n(header))).append("</li>"); 220 222 for (Iterator subitems=sidebar.elementIterator(); subitems.hasNext(); ) { 223 Element item = (Element)subitems.next(); 224 String subitemID = item.attributeValue("id"); 225 String subitemName = item.attributeValue("name"); 226 String subitemURL = item.attributeValue("url"); 227 String subitemDescr = item.attributeValue("description"); 228 String value = getBodyContent().getString(); 229 if (value != null) { 230 value = StringUtils.replace(value, "[id]", clean(subitemID)); 231 value = StringUtils.replace(value, "[name]", clean(i18n(subitemName))); 232 value = StringUtils.replace(value, "[description]", clean(i18n(subitemDescr))); 233 value = StringUtils.replace(value, "[url]", 234 request.getContextPath() + "/" + clean(subitemURL)); 235 } 236 String css = getCss(); 237 boolean isCurrent = item.equals(current); 238 boolean showSubmenu = subPageID != null; 239 if (isCurrent && !showSubmenu) { 240 css = getCurrentcss(); 241 } 242 buf.append("<li class=\"").append(css).append("\">").append(value).append("</li>"); 243 244 if (isSubmenu && isCurrent) { 246 Iterator siblings = subcurrent.getParent().elementIterator(); 249 boolean hadNext = siblings.hasNext(); 250 if (hadNext) { 251 buf.append("<ul class=\"subitems\">\n"); 253 String subheader = subcurrent.getParent().attributeValue("name"); 255 buf.append("<li class=\"").append(hcss).append("\">").append(clean(i18n(subheader))).append("</li>"); 256 } 257 String extraParams = pageInfo.getExtraParams(); 258 while (siblings.hasNext()) { 259 Element sibling = (Element)siblings.next(); 260 String sibID = sibling.attributeValue("id"); 261 String sibName = sibling.attributeValue("name"); 262 String sibDescr = sibling.attributeValue("description"); 263 String sibURL = sibling.attributeValue("url"); 264 if (extraParams != null) { 265 sibURL += ((sibURL.indexOf('?') > -1 ? "&" : "?") + extraParams); 266 } 267 boolean isSubCurrent = sibling.equals(subcurrent); 268 String subcss = getCss(); 269 if (isSubCurrent) { 270 subcss = getCurrentcss(); 271 } 272 String svalue = getSubsidebarTag().getBody(); 273 if (svalue != null) { 274 svalue = StringUtils.replace(svalue, "[id]", clean(sibID)); 275 svalue = StringUtils.replace(svalue, "[name]", clean(i18n(sibName))); 276 svalue = StringUtils.replace(svalue, "[description]", clean(i18n(sibDescr))); 277 svalue = StringUtils.replace(svalue, "[url]", 278 request.getContextPath() + "/" + clean(sibURL)); 279 } 280 buf.append("<li class=\"").append(subcss).append("\">").append(svalue).append("</li>\n"); 281 } 282 if (hadNext) { 283 buf.append("<br></ul>\n"); 285 } 286 } 287 } 288 } 289 buf.append("</ul>"); 290 try { 291 out.write(buf.toString()); 292 } 293 catch (IOException e) { 294 throw new JspException (e); 295 } 296 } 297 } 298 } 299 } 300 } 301 return EVAL_PAGE; 302 } 303 304 311 private String clean(String in) { 312 return (in == null ? "" : StringUtils.replace(in, "'", "\\'")); 313 } 314 315 private static String i18n(String in) { 316 if (in == null) { 317 return null; 318 } 319 if (in.indexOf("${") == 0 && in.indexOf("}") == in.length()-1) { 321 return LocaleUtils.getLocalizedString(in.substring(2, in.length()-1)); 322 } 323 return in; 324 } 325 } | Popular Tags |