1 21 22 package com.jaspersoft.jasperserver.war.common; 23 24 import java.io.InputStream ; 25 import javax.xml.parsers.*; 26 import org.w3c.dom.*; 27 import java.util.*; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 35 36 public class SiteMenu { 37 38 private static final Log log = LogFactory 39 .getLog(SiteMenu.class); 40 41 private final String URL_ATTR = "url"; 42 private final String SERVLETPARAMS_ATTR = "servletParams"; 43 private final String NAME_ATTR = "name"; 44 private final String MENU_ITEM_NODE_NAME = "menu-item"; 45 private final String SUB_ITEMS_NODE_NAME = "sub-items"; 46 private final String ROLES_NODE_NAME = "roles"; 47 private final String ROLE_NODE_NAME = "role"; 48 private final String MENU_XML_URI = "/JI-menu.xml"; 49 private static SiteMenu theInstance; 50 51 public static SiteMenu instance() { 52 if (theInstance == null) { 53 theInstance = new SiteMenu(); 54 } 55 return theInstance; 56 } 57 58 private MenuItem mMenu = null; 59 public MenuItem getMenu() throws Exception { 60 if (mMenu == null) { 61 mMenu = parse(MENU_XML_URI); 62 } 63 return mMenu; 64 } 65 66 protected MenuItem parse(String menuXmlUri) throws Exception { 67 MenuItem result = null; 68 try { 69 InputStream siteXml = this.getClass().getResourceAsStream(menuXmlUri); 70 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 71 DocumentBuilder domBuilder = domFactory.newDocumentBuilder(); 72 Document menuDoc = domBuilder.parse(siteXml); 73 result = parseMenuItem(menuDoc.getDocumentElement()); 74 } catch (Exception e) { 75 log.error(e); 76 throw(e); 77 } 78 return result; 79 } 80 81 public void printNode( org.w3c.dom.Node n, int indent ) { 83 for (int i=0; i<indent; i++) { 84 System.out.print(" "); 85 } 86 System.out.println(n); 87 if (n.hasChildNodes()) { 88 for (int j=0; j<n.getChildNodes().getLength(); j++) { 89 printNode( n.getChildNodes().item(j), indent+2 ); 90 } 91 } 92 } 93 94 protected MenuItem parseMenuItem( Element itemNode ) { 95 MenuItem menuItem = new MenuItem(); 96 menuItem.name = itemNode.getAttribute(NAME_ATTR); 97 if (itemNode.hasAttribute(URL_ATTR)) { 98 menuItem.url = itemNode.getAttribute(URL_ATTR); 99 } 100 if (itemNode.hasAttribute(SERVLETPARAMS_ATTR)) { 101 menuItem.servletParams = itemNode.getAttribute(SERVLETPARAMS_ATTR); 102 } 103 List subItems = new ArrayList(); 104 List roles = new ArrayList(); 105 106 if (itemNode.hasChildNodes()) { 107 for (int i=0; i<itemNode.getChildNodes().getLength(); i++) { 108 Node child = itemNode.getChildNodes().item(i); 109 if (SUB_ITEMS_NODE_NAME.equals(child.getNodeName())) { 110 for (int j=0; j<child.getChildNodes().getLength(); j++) { 111 Node menuChild = child.getChildNodes().item(j); 112 if (MENU_ITEM_NODE_NAME.equals(menuChild.getNodeName())) { 113 subItems.add(parseMenuItem( (Element)menuChild )); 114 } 115 } 116 } 117 if (ROLES_NODE_NAME.equals(child.getNodeName())) { 118 for (int k=0; k<child.getChildNodes().getLength(); k++) { 119 Node roleChild = child.getChildNodes().item(k); 120 if (ROLE_NODE_NAME.equals(roleChild.getNodeName())) { 121 roles.add(roleChild.getFirstChild().getNodeValue()); 122 } 123 } 124 } 125 } 126 } 127 menuItem.subItems = (MenuItem[])subItems.toArray(new MenuItem[subItems.size()]); 128 menuItem.roles = (String [])roles.toArray(new String [roles.size()]); 129 return menuItem; 130 } 131 132 public static class MenuItem { 133 String name; 134 public String getName() { return name; } 135 136 String url; 137 public String getUrl() { return url; } 138 139 String servletParams; 140 public String getServletParams() { return servletParams; } 141 142 MenuItem[] subItems; 143 public MenuItem[] getSubItems() { return subItems; } 144 public boolean getHasSubItems() { 145 return (subItems != null && subItems.length > 0); 146 } 147 148 149 String [] roles; 150 public String [] getRoles() { return roles; } 151 public String getRolesStr() { 154 StringBuffer b = new StringBuffer (); 155 for (int i=0; i<roles.length; i++) { 156 b.append(roles[i]); 157 if (i<roles.length-1) { 158 b.append(","); 159 } 160 } 161 return b.toString(); 162 } 163 164 public String toString() { 165 StringBuffer buff = new StringBuffer (); 166 buff.append("MenuItem( name=").append(name); 167 buff.append(" url=").append(url); 168 buff.append(" servletParams=").append(servletParams); 169 buff.append(" roles="); 170 for (int i=0; i<roles.length; i++) { 171 buff.append(roles[i]); 172 if (i<(roles.length-1)) { 173 buff.append(","); 174 } 175 } 176 buff.append(" sub-items="); 177 for (int j=0; j<subItems.length; j++) { 178 buff.append(subItems[j].toString()); 179 if (j<(subItems.length-1)) { 180 buff.append(","); 181 } 182 } 183 buff.append(" )"); 184 return buff.toString(); 185 } 186 187 } 188 189 } | Popular Tags |