KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > common > SiteMenu


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.war.common;
23
24 import java.io.InputStream JavaDoc;
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 /**
33  * @author sbirney
34  */

35
36 public class SiteMenu {
37
38     private static final Log log = LogFactory
39     .getLog(SiteMenu.class);
40
41     private final String JavaDoc URL_ATTR = "url";
42     private final String JavaDoc SERVLETPARAMS_ATTR = "servletParams";
43     private final String JavaDoc NAME_ATTR = "name";
44     private final String JavaDoc MENU_ITEM_NODE_NAME = "menu-item";
45     private final String JavaDoc SUB_ITEMS_NODE_NAME = "sub-items";
46     private final String JavaDoc ROLES_NODE_NAME = "roles";
47     private final String JavaDoc ROLE_NODE_NAME = "role";
48     private final String JavaDoc 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 JavaDoc {
60     if (mMenu == null) {
61         mMenu = parse(MENU_XML_URI);
62     }
63     return mMenu;
64     }
65
66     protected MenuItem parse(String JavaDoc menuXmlUri) throws Exception JavaDoc {
67     MenuItem result = null;
68     try {
69         InputStream JavaDoc 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 JavaDoc e) {
75         log.error(e);
76         throw(e);
77     }
78     return result;
79     }
80
81     // just for debugging
82
public void printNode( org.w3c.dom.Node JavaDoc 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 JavaDoc[])roles.toArray(new String JavaDoc[roles.size()]);
129     return menuItem;
130     }
131
132     public static class MenuItem {
133     String JavaDoc name;
134     public String JavaDoc getName() { return name; }
135
136     String JavaDoc url;
137     public String JavaDoc getUrl() { return url; }
138
139     String JavaDoc servletParams;
140     public String JavaDoc 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 JavaDoc[] roles;
150     public String JavaDoc[] getRoles() { return roles; }
151     // acegi tag seems to want a comma separated String,
152
// rather than a String[] of roles, so....
153
public String JavaDoc getRolesStr() {
154         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
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 JavaDoc toString() {
165         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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