Commit ef24ae04 authored by Bill Lynch's avatar Bill Lynch Committed by bill

Initial checkin


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@256 b35dd754-fafc-0310-a699-88a17e54d16e
parent 3ae2484c
/**
* $RCSfile$
* $Revision
* $Date$
*
* Copyright (C) 1999-2004 Jive Software. All rights reserved.
*
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.admin;
import org.jivesoftware.util.ClassUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.XMLProperties;
import java.util.*;
import java.io.InputStream;
import java.net.URL;
/**
* <p>A model for admin tab and sidebar info. This class loads in xml definitions of the data and
* produces an in-memory model. There is an internal class, {@link Item} which is the main part
* of the model. Items hold info like name, id, url and description as well as an arbritrary number
* of sub items. Based on this we can make a tree model of the data.</p>
*
* <p>This class loads its data from the <tt>admin-sidebar.xml</tt> file which is assumed to be in
* the main application jar file. In addition, it will load files from
* <tt>META-INF/admin-sidebar.xml</tt> if they're found. This allows developers to extend the
* functionality of the admin console to provide more options. See the main
* <tt>admin-sidebar.xml</tt> file for documentation of its format.</p>
*
* <p>Note: IDs in the XML file must be unique because an internal mapping is kept of IDs to
* nodes.</p>
*
* TODO: Add other property customizers (page title, image urls, etc)
*/
public class AdminConsole {
private static Collection items;
private static Map idMap; // map of item ids -> item objs
static {
items = new ArrayList();
idMap = new HashMap();
load();
}
/** Not instantiatable */
private AdminConsole() {
}
/**
* Returns all items starting from the root. Getting the iterator from this collection returns
* all root items (should be used as tabs in the admin tool).
*
* @return a collection of all items - the root items are returned by calling the
* <tt>iterator()</tt> method.
*/
public static Collection getItems() {
return items;
}
/**
* Returns an item given its ID or <tt>null</tt> if it can't be found.
*
* @param id the ID of the item.
* @return an item given its ID or <tt>null</tt> if it can't be found.
*/
public static Item getItem(String id) {
return (Item)idMap.get(id);
}
/**
* Returns the root item given a child item. In other words, a lookup is done on the ID for
* the corresponding item - that item is assumed to be a leaf and this method returns the
* root ancestor of it.
*
* @param id the ID of the child item.
* @return the root ancestor of the specified child item.
*/
public static Item getRootByChildID(String id) {
if (id == null) {
return null;
}
Item child = getItem(id);
Item root = null;
if (child != null) {
Item parent = child.getParent();
root = parent;
while (parent != null) {
parent = parent.getParent();
if (parent != null) {
root = parent;
}
}
}
return root;
}
/**
* Returns <tt>true</tt> if the given item is a sub-menu item.
*
* @param item the item to test.
* @return <tt>true</tt> if the given item is a sub-menu item, <tt>false</tt> otherwise.
*/
public static boolean isSubMenItem(Item item) {
int parentCount = 0;
Item parent = item.getParent();
while (parent != null) {
parentCount++;
parent = parent.getParent();
}
return parentCount >= 3;
}
/**
* Returns the ID of the page ID associated with this sub page ID.
* @param subPageID the subPageID to use to look up the page ID.
* @return the associated pageID or <tt>null</tt> if it can't be found.
*/
public static String lookupPageID(String subPageID) {
String pageID = null;
Item item = getItem(subPageID);
if (item != null) {
Item parent = item.getParent();
while (parent.getId() == null) {
parent = parent.getParent();
}
pageID = parent.getId();
}
return pageID;
}
/**
* A simple class to model an item. Each item has attributes used by the admin console to
* display it like ID, name, URL and description. Also, from each item you can get its parent
* (because an Item goes in a tree structure) and any children items it has.
*/
public static class Item {
private String id;
private String name;
private String description;
private String url;
private boolean active;
private Collection items;
private Item parent;
/**
* Creates a new item given its main attributes.
*/
public Item(String id, String name, String description, String url) {
this.id = id;
this.name = name;
this.description = description;
this.url = url;
init();
}
/**
* Creates a new item given its main attributes and the parent item (this helps set up
* the tree structure).
*/
public Item(String id, String name, String description, String url, Item parent) {
this.id = id;
this.name = name;
this.description = description;
this.url = url;
this.parent = parent;
init();
}
private void init() {
items = Collections.synchronizedList(new ArrayList());
if (id != null && !"".equals(id.trim())) {
idMap.put(id, this);
}
}
/**
* Returns the ID of the item.
*/
public String getId() {
return id;
}
/**
* Returns the name of the item - this is the display name.
*/
public String getName() {
return name;
}
/**
* Returns the description of the item.
*/
public String getDescription() {
return description;
}
/**
* Returns the URL for this item.
*/
public String getUrl() {
return url;
}
/**
* Sets the URL for this item.
*/
public void setUrl(String url) {
this.url = url;
}
/**
* Returns true if this items is active - in the admin console this would mean it's selected.
*/
public boolean isActive() {
return active;
}
/**
* Sets the item as active - in the admin console this would mean it's selected.
*/
public void setActive(boolean active) {
this.active = active;
}
/**
* Returns the parent item or <tt>null</tt> if this is a root item.
*/
public Item getParent() {
return parent;
}
/**
* Sets the parent item.
*/
public void setParent(Item parent) {
this.parent = parent;
}
/**
* Returns the items as a collection. Use the Collection API to get/set/remove items.
*/
public Collection getItems() {
return items;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (!(o instanceof Item)) {
return false;
}
Item i = (Item) o;
if (!id.equals(i.id)) {
return false;
}
return true;
}
/**
* Returns the ID of the item.
*/
public String toString() {
return id;
}
}
private static void load() {
// Load the admin-sidebar.xml file from the jiveforums.jar file:
InputStream in = ClassUtils.getResourceAsStream("/admin-sidebar.xml");
if (in == null) {
Log.error("Failed to load admin-sidebar.xml file from Jive Forums classes - admin "
+ "console will not work correctly.");
return;
}
try {
addToModel(in);
}
catch (Exception e) {
Log.error("Failure when parsing main admin-sidebar.xml file", e);
}
try {
in.close();
}
catch (Exception ignored) {}
// Load other admin-sidebar.xml files from the classpath
ClassLoader[] classLoaders = getClassLoaders();
for (int i=0; i<classLoaders.length; i++) {
URL url = null;
try {
if (classLoaders[i] != null) {
Enumeration e = classLoaders[i].getResources("META-INF/admin-sidebar.xml");
while (e.hasMoreElements()) {
url = (URL)e.nextElement();
in = url.openStream();
addToModel(in);
try {
in.close();
}
catch (Exception ignored) {}
}
}
}
catch (Exception e) {
String msg = "Failed to load admin-sidebar.xml";
if (url != null) {
msg += " from resource: " + url.toString();
}
Log.warn(msg, e);
}
}
}
private static void addToModel(InputStream in) throws Exception {
// Build an XMLPropertiesTest object from the input stream:
XMLProperties xmlTest = new XMLProperties(in);
// Get all children of the 'tabs' element - should be 'tab' items:
String[] tabs = xmlTest.getChildrenProperties("tabs");
for (int i=0; i<tabs.length; i++) {
String propName = "tabs." + tabs[i];
// Create a new top level item with data from the xmlTest file:
String id = xmlTest.getProperty(propName + ".id");
String name = xmlTest.getProperty(propName + ".name");
String description = xmlTest.getProperty(propName + ".description");
Item item = new Item(id, name, description, null);
// Add that item to the item collection
getItems().add(item);
// Delve down into this item's sidebars - build up a model of these then add into
// the item above.
String[] sidebars = xmlTest.getChildrenProperties(propName + ".sidebars");
for (int j=0; j<sidebars.length; j++) {
String sidebarName = propName + ".sidebars." + sidebars[j];
name = xmlTest.getProperty(sidebarName + ".name");
// Create a new item, set its name
Item subItem = new Item(null, name, null, null);
// Now iterate down another level, get the items for this item - this will be the
// specific links on the sidebar
String[] subitems = xmlTest.getChildrenProperties(sidebarName + ".items");
for (int k=0; k<subitems.length; k++) {
String subitemName = sidebarName + ".items." + subitems[k];
// Get the id, name, descr and url attributes:
String subID = xmlTest.getProperty(subitemName + ".id");
String subName = xmlTest.getProperty(subitemName + ".name");
String subDescr = xmlTest.getProperty(subitemName + ".description");
String subURL = xmlTest.getProperty(subitemName + ".url");
// Build an item with this, add it to the subItem we made above
Item kItem = new Item(subID, subName, subDescr, subURL, subItem);
subItem.getItems().add(kItem);
// Build any sub-sub menus:
subAddtoModel(subitemName, xmlTest, kItem);
// If this is the first item, set the root menu item's URL as this URL:
if (j==0 && k == 0) {
item.setUrl(subURL);
}
}
// Add the subItem to the item created above
subItem.setParent(item);
item.getItems().add(subItem);
}
}
}
private static void subAddtoModel(String path, XMLProperties props, Item parent) {
String propName = path + ".subsidebars";
String[] subsidebars = props.getChildrenProperties(propName);
for (int i=0; i<subsidebars.length; i++) {
String child = propName + "." + subsidebars[i];
String[] subsidebarchildren = props.getChildrenProperties(child);
for (int j=0; j<subsidebarchildren.length; j++) {
String root = propName + ".subsidebar" + i + "." + subsidebarchildren[j];
String name = props.getProperty(root + ".name");
Item header = new Item(null, name, null, null, parent);
// Get the children of this
String subPath = root + ".items";
String[] allitems = props.getChildrenProperties(subPath);
for (int k=0; k<allitems.length; k++) {
String subName = subPath + "." + allitems[k];
String itemID = props.getProperty(subName + ".id");
String itemName = props.getProperty(subName + ".name");
String itemDescr = props.getProperty(subName + ".description");
String itemURL = props.getProperty(subName + ".url");
Item si = new Item(itemID, itemName, itemDescr, itemURL, header);
header.getItems().add(si);
}
parent.getItems().add(header);
}
}
}
/**
* Returns an array of class loaders to load resources from.
*/
private static ClassLoader[] getClassLoaders() {
ClassLoader[] classLoaders = new ClassLoader[3];
classLoaders[0] = AdminConsole.class.getClass().getClassLoader();
classLoaders[1] = Thread.currentThread().getContextClassLoader();
classLoaders[2] = ClassLoader.getSystemClassLoader();
return classLoaders;
}
}
/**
* $RCSfile$
* $Revision
* $Date$
*
* Copyright (C) 1999-2004 Jive Software. All rights reserved.
*
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.admin;
import org.jivesoftware.util.StringUtils;
import java.util.Collection;
import java.util.ArrayList;
public class AdminPageBean {
private String title;
private Collection breadcrumbs;
private String pageID;
private String subPageID;
private String extraParams;
private Collection scripts;
public AdminPageBean() {
}
/**
* Returns the title of the page with HTML escaped.
*/
public String getTitle() {
if (title != null) {
return StringUtils.escapeHTMLTags(title);
}
else {
return title;
}
}
/**
* Sets the title of the admin console page.
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Returns a collection of breadcrumbs. Use the Collection API to get/set/remove crumbs.
*/
public Collection getBreadcrumbs() {
if (breadcrumbs == null) {
breadcrumbs = new ArrayList();
}
return breadcrumbs;
}
/**
* Returns the page ID (corresponds to sidebar ID's).
*/
public String getPageID() {
return pageID;
}
/**
* Sets the ID of the page (corresponds to sidebar ID's).
* @param pageID
*/
public void setPageID(String pageID) {
this.pageID = pageID;
}
/**
* Returns the subpage ID (corresponds to sidebar ID's).
*/
public String getSubPageID() {
return subPageID;
}
/**
* Sets the subpage ID (corresponds to sidebar ID's).
* @param subPageID
*/
public void setSubPageID(String subPageID) {
this.subPageID = subPageID;
}
/**
* Returns a string of extra parameters for the URLs - these might be specific IDs for resources.
*/
public String getExtraParams() {
return extraParams;
}
/**
* Sets the string of extra parameters for the URLs.
*/
public void setExtraParams(String extraParams) {
this.extraParams = extraParams;
}
/**
* Returns a collection of scripts. Use the Collection API to get/set/remove scripts.
*/
public Collection getScripts() {
if (scripts == null) {
scripts = new ArrayList();
}
return scripts;
}
/**
* A simple model of a breadcrumb. A bread crumb is a link with a display name.
*/
public static class Breadcrumb {
private String name;
private String url;
/**
* Creates a crumb given a name an URL.
*/
public Breadcrumb(String name, String url) {
this.name = name;
this.url = url;
}
/**
* Returns the name, with HTML escaped.
*/
public String getName() {
if (name != null) {
return StringUtils.escapeHTMLTags(name);
}
else {
return name;
}
}
/**
* Returns the URL.
*/
public String getUrl() {
return url;
}
}
}
/**
* $RCSfile$
* $Revision
* $Date$
*
* Copyright (C) 1999-2004 Jive Software. All rights reserved.
*
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.admin;
import org.jivesoftware.util.StringUtils;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.ServletRequest;
import java.util.Collection;
import java.util.Iterator;
import java.io.IOException;
public class SidebarTag extends BodyTagSupport {
private String bean;
private String role;
private String minEdition;
private String css;
private String currentcss;
private String headercss;
private SubSidebarTag subsidebarTag;
/**
* The name of the request attribute which holds a {@link AdminPageBean} instance.
*/
public String getBean() {
return bean;
}
/**
* Sets the name of the request attribute to hold a {@link AdminPageBean} instance.
*/
public void setBean(String bean) {
this.bean = bean;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getMinEdition() {
return minEdition;
}
public void setMinEdition(String minEdition) {
this.minEdition = minEdition;
}
/**
* Returns the value of the CSS class to be used for tab decoration. If not set will return a blank string.
*/
public String getCss() {
return clean(css);
}
/**
* Sets the CSS used for tab decoration.
*/
public void setCss(String css) {
this.css = css;
}
/**
* Returns the value of the CSS class to be used for the currently selected LI (tab). If not set will
* return a blank string.
*/
public String getCurrentcss() {
return clean(currentcss);
}
/**
* Sets the CSS class value for the currently selected tab.
*/
public void setCurrentcss(String currentcss) {
this.currentcss = currentcss;
}
/**
* Returns the value of the CSS class to be used for sidebar header sections.
*/
public String getHeadercss() {
return headercss;
}
/**
* Sets the CSS value used for the sidebar header sections.
*/
public void setHeadercss(String headercss) {
this.headercss = headercss;
}
/**
* Returns the subsidebar tag - should be declared in the body of this tag (see class description).
*/
public SubSidebarTag getSubsidebarTag() {
return subsidebarTag;
}
/**
* Sets the subsidebar tag - used by the container.
*/
public void setSubSidebar(SubSidebarTag subsidebarTag) {
this.subsidebarTag = subsidebarTag;
}
/**
* Does nothing, returns {@link #EVAL_BODY_BUFFERED} always.
*/
public int doStartTag() throws JspException {
return EVAL_BODY_BUFFERED;
}
/**
* Gets the {@link AdminPageBean} instance from the request. If it doesn't exist then execution is stopped
* and nothing is printed. If it exists, retrieve values from it and render the sidebar items. The body content
* of the tag is assumed to have an A tag in it with tokens to replace (see class description) as well
* as having a subsidebar tag..
*
* @return {@link #EVAL_PAGE} after rendering the tabs.
* @throws JspException if an exception occurs while rendering the sidebar items.
*/
public int doEndTag() throws JspException {
// Start by getting the request from the page context
ServletRequest request = pageContext.getRequest();
// Check for body of this tag and the child tag
if (getBodyContent().getString() == null) {
throw new JspException("Error, no template (body value) set for the sidebar tag.");
}
if (subsidebarTag.getBody() == null) {
throw new JspException("Error, no template (body value) set for the subsidebar tag");
}
// Get the page data bean from the request:
AdminPageBean pageInfo = (AdminPageBean)request.getAttribute(getBean());
// If the page info bean is not in the request then no tab will be selected - so, it'll fail gracefully
if (pageInfo != null) {
// Get the initial subpage and page IDs
String subPageID = pageInfo.getSubPageID();
String pageID = pageInfo.getPageID();
// If the pageID is null, use the subPageID to set it. If both the pageID and subPageIDs are null,
// return because these are key to execution of the tag.
if (subPageID != null || pageID != null) {
if (pageID == null) {
pageID = AdminConsole.lookupPageID(subPageID);
}
// Top level menu items
if (AdminConsole.getItems().size() > 0) {
JspWriter out = pageContext.getOut();
StringBuffer buf = new StringBuffer();
AdminConsole.Item current = null;
AdminConsole.Item subcurrent = null;
if (subPageID != null) {
subcurrent = AdminConsole.getItem(subPageID);
// Lookup the pageID based on the subPageID:
pageID = AdminConsole.lookupPageID(subPageID);
}
AdminConsole.Item root = AdminConsole.getRootByChildID(pageID);
current = AdminConsole.getItem(pageID);
boolean isSubmenu = false;
if (subPageID != null && current != null) {
isSubmenu = AdminConsole.isSubMenItem(subcurrent);
}
// Loop through all items in the root, print them out
if (root != null) {
Collection items = root.getItems();
if (items.size() > 0) {
buf.append("<ul>");
for (Iterator iter=items.iterator(); iter.hasNext(); ) {
AdminConsole.Item item = (AdminConsole.Item)iter.next();
String header = item.getName();
// Print the header:
String hcss = getHeadercss();
if (hcss == null) {
hcss = "";
}
buf.append("<li class=\"").append(hcss).append("\">").append(clean(header)).append("</li>");
// Now print all subitems:
for (Iterator subitems=item.getItems().iterator(); subitems.hasNext(); ) {
AdminConsole.Item subitem = (AdminConsole.Item)subitems.next();
String subitemID = subitem.getId();
String subitemName = subitem.getName();
String subitemURL = subitem.getUrl();
String subitemDescr = subitem.getDescription();
String value = getBodyContent().getString();
if (value != null) {
value = StringUtils.replace(value, "[id]", clean(subitemID));
value = StringUtils.replace(value, "[name]", clean(subitemName));
value = StringUtils.replace(value, "[description]", clean(subitemDescr));
value = StringUtils.replace(value, "[url]", clean(subitemURL));
}
String css = getCss();
boolean isCurrent = subitem.equals(current);
boolean showSubmenu = subPageID != null;
if (isCurrent && !showSubmenu) {
css = getCurrentcss();
}
buf.append("<li class=\"").append(css).append("\">").append(value).append("</li>");
// Print out a submenu if one exists:
if (isSubmenu && isCurrent) {
// Get the parent of the current item so we can get its items - those will be
// siblings of the current item:
Iterator siblings = subcurrent.getParent().getItems().iterator();
boolean hadNext = siblings.hasNext();
if (hadNext) {
// Print out beginning UL
buf.append("<ul class=\"subitems\">\n");
// Print the header LI
String subheader = subcurrent.getParent().getName();
buf.append("<li class=\"").append(hcss).append("\">").append(clean(subheader)).append("</li>");
}
String extraParams = pageInfo.getExtraParams();
while (siblings.hasNext()) {
AdminConsole.Item sibling = (AdminConsole.Item)siblings.next();
String sibID = sibling.getId();
String sibName = sibling.getName();
String sibDescr = sibling.getDescription();
String sibURL = sibling.getUrl();
if (extraParams != null) {
sibURL += ((sibURL.indexOf('?') > -1 ? "&" : "?") + extraParams);
}
boolean isSubCurrent = sibling.equals(subcurrent);
String subcss = getCss();
if (isSubCurrent) {
subcss = getCurrentcss();
}
String svalue = getSubsidebarTag().getBody();
if (svalue != null) {
svalue = StringUtils.replace(svalue, "[id]", clean(sibID));
svalue = StringUtils.replace(svalue, "[name]", clean(sibName));
svalue = StringUtils.replace(svalue, "[description]", clean(sibDescr));
svalue = StringUtils.replace(svalue, "[url]", clean(sibURL));
}
buf.append("<li class=\"").append(subcss).append("\">").append(svalue).append("</li>\n");
}
if (hadNext) {
// Print out ending UL
buf.append("<br></ul>\n");
}
}
}
}
buf.append("</ul>");
try {
out.write(buf.toString());
}
catch (IOException e) {
throw new JspException(e);
}
}
}
}
}
}
return EVAL_PAGE;
}
/**
* Cleans the given string - if it's null, it's converted to a blank string. If it has ' then those are
* converted to double ' so HTML isn't screwed up.
*
* @param in the string to clean
* @return a cleaned version - not null and with escaped characters.
*/
private String clean(String in) {
return (in == null ? "" : StringUtils.replace(in, "'", "\\'"));
}
}
\ No newline at end of file
/**
* $RCSfile$
* $Revision
* $Date$
*
* Copyright (C) 1999-2004 Jive Software. All rights reserved.
*
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.admin;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
public class SubSidebarTag extends SidebarTag {
private SidebarTag parent;
private String body;
/**
* Returns the body content of this tag.
*/
public String getBody() {
return body;
}
/**
* Sets the body content of this tag.
*/
public void setBody(String body) {
this.body = body;
}
/**
* Looks for the parent SidebarTag class, throws an error if not found. If found,
* {@link #EVAL_BODY_BUFFERED} is returned.
*
* @return {@link #EVAL_BODY_BUFFERED} if no errors.
* @throws javax.servlet.jsp.JspException if a parent SidebarTag is not found.
*/
public int doStartTag() throws JspException {
// The I18nTag should be our parent Tag
parent = (SidebarTag)findAncestorWithClass(this, SidebarTag.class);
// If I18nTag was not our parent, throw Exception
if (parent == null) {
throw new JspTagException("SubSidebarTag with out a parent which is expected to be a SidebarTag");
}
return EVAL_BODY_BUFFERED;
}
/**
* Sets the 'body' property to be equal to the body content of this tag. Calls the
* {@link SidebarTag#setSubSidebar(SubSidebarTag)} method of the parent tag.
* @return {@link #EVAL_PAGE}
* @throws JspException if an error occurs.
*/
public int doEndTag() throws JspException {
setBody(bodyContent.getString());
parent.setSubSidebar(this);
return EVAL_PAGE;
}
}
\ No newline at end of file
/**
* $RCSfile$
* $Revision
* $Date$
*
* Copyright (C) 1999-2004 Jive Software. All rights reserved.
*
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.admin;
import org.jivesoftware.util.StringUtils;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.ServletRequest;
import java.util.Collection;
import java.util.Iterator;
import java.io.IOException;
public class TabsTag extends BodyTagSupport {
private String bean;
private String role;
private String minEdition;
private String css;
private String currentcss;
/**
* The name of the request attribute which holds a {@link AdminPageBean} instance.
*/
public String getBean() {
return bean;
}
/**
* Sets the name of the request attribute to hold a {@link AdminPageBean} instance.
*/
public void setBean(String bean) {
this.bean = bean;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getMinEdition() {
return minEdition;
}
public void setMinEdition(String minEdition) {
this.minEdition = minEdition;
}
/**
* Returns the value of the CSS class to be used for tab decoration. If not set will return a blank string.
*/
public String getCss() {
return clean(css);
}
/**
* Sets the CSS used for tab decoration.
*/
public void setCss(String css) {
this.css = css;
}
/**
* Returns the value of the CSS class to be used for the currently selected LI (tab). If not set will
* return a blank string.
*/
public String getCurrentcss() {
return clean(currentcss);
}
/**
* Sets the CSS class value for the currently selected tab.
*/
public void setCurrentcss(String currentcss) {
this.currentcss = currentcss;
}
/**
* Does nothing, returns {@link #EVAL_BODY_BUFFERED} always.
*/
public int doStartTag() throws JspException {
return EVAL_BODY_BUFFERED;
}
/**
* Gets the {@link AdminPageBean} instance from the request. If it doesn't exist then execution is stopped
* and nothing is printed. If it exists, retrieve values from it and render the tabs. The body content
* of the tag is assumed to have an A tag in it with tokens to replace (see class description).
*
* @return {@link #EVAL_PAGE} after rendering the tabs.
* @throws JspException if an exception occurs while rendering the tabs.
*/
public int doEndTag() throws JspException {
ServletRequest request = pageContext.getRequest();
String beanName = getBean();
// Get the page data bean from the request:
AdminPageBean pageInfo = (AdminPageBean)request.getAttribute(beanName);
// If the page info bean is not in the request then no tab will be selected - so, it'll fail gracefully
String pageID = null;
if (pageInfo != null) {
pageID = pageInfo.getPageID();
}
// Get root items from the data model:
Collection items = AdminConsole.getItems();
if (items.size() > 0) {
JspWriter out = pageContext.getOut();
// Build up the output in a buffer (is probably faster than a bunch of out.write's)
StringBuffer buf = new StringBuffer();
buf.append("<ul>");
String body = getBodyContent().getString();
// For each root item, print out an LI
AdminConsole.Item root = AdminConsole.getRootByChildID(pageID);
for (Iterator iter=items.iterator(); iter.hasNext(); ) {
AdminConsole.Item item = (AdminConsole.Item)iter.next();
String value = body;
if (value != null) {
value = StringUtils.replace(value, "[id]", clean(item.getId()));
value = StringUtils.replace(value, "[url]", clean(item.getUrl()));
value = StringUtils.replace(value, "[name]", clean(item.getName()));
value = StringUtils.replace(value, "[description]", clean(item.getDescription()));
}
String css = getCss();
if (item.equals(root)) {
css = getCurrentcss();
}
buf.append("<li class=\"").append(css).append("\">");
buf.append(value);
buf.append("</li>");
}
buf.append("</ul>");
try {
out.write(buf.toString());
}
catch (IOException ioe) {
throw new JspException(ioe.getMessage());
}
}
return EVAL_PAGE;
}
/**
* Cleans the given string - if it's null, it's converted to a blank string. If it has ' then those are
* converted to double ' so HTML isn't screwed up.
*
* @param in the string to clean
* @return a cleaned version - not null and with escaped characters.
*/
private String clean(String in) {
return (in == null ? "" : StringUtils.replace(in, "'", "\\'"));
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Tag Library for Jive Forums 4.0</shortname>
<uri>http://www.jivesoftware.com/</uri>
<info>Tab Library for Jive Messenger Admin Console</info>
<tag>
<name>tabs</name>
<tagclass>org.jivesoftware.admin.TabsTag</tagclass>
<bodycontent>JSP</bodycontent>
<info />
<attribute>
<name>css</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>currentcss</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bean</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>role</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>minEdition</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>sidebar</name>
<tagclass>org.jivesoftware.admin.SidebarTag</tagclass>
<bodycontent>JSP</bodycontent>
<info />
<attribute>
<name>css</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>currentcss</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headercss</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bean</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>role</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>minEdition</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>subsidebar</name>
<tagclass>org.jivesoftware.admin.SubSidebarTag</tagclass>
<bodycontent>JSP</bodycontent>
<info />
<attribute>
<name>css</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>currentcss</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>role</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>minEdition</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment