Source Code Cross Referenced for MainMenu.java in  » J2EE » Jaffa » org » jaffa » components » menu » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Jaffa » org.jaffa.components.menu 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * ====================================================================
003:         * JAFFA - Java Application Framework For All
004:         *
005:         * Copyright (C) 2002 JAFFA Development Group
006:         *
007:         *     This library is free software; you can redistribute it and/or
008:         *     modify it under the terms of the GNU Lesser General Public
009:         *     License as published by the Free Software Foundation; either
010:         *     version 2.1 of the License, or (at your option) any later version.
011:         *
012:         *     This library is distributed in the hope that it will be useful,
013:         *     but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         *     Lesser General Public License for more details.
016:         *
017:         *     You should have received a copy of the GNU Lesser General Public
018:         *     License along with this library; if not, write to the Free Software
019:         *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020:         *
021:         * Redistribution and use of this software and associated documentation ("Software"),
022:         * with or without modification, are permitted provided that the following conditions are met:
023:         * 1.	Redistributions of source code must retain copyright statements and notices.
024:         *         Redistributions must also contain a copy of this document.
025:         * 2.	Redistributions in binary form must reproduce the above copyright notice,
026:         * 	this list of conditions and the following disclaimer in the documentation
027:         * 	and/or other materials provided with the distribution.
028:         * 3.	The name "JAFFA" must not be used to endorse or promote products derived from
029:         * 	this Software without prior written permission. For written permission,
030:         * 	please contact mail to: jaffagroup@yahoo.com.
031:         * 4.	Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032:         * 	appear in their names without prior written permission.
033:         * 5.	Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034:         *
035:         * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046:         * SUCH DAMAGE.
047:         * ====================================================================
048:         */
049:
050:        package org.jaffa.components.menu;
051:
052:        import java.util.*;
053:        import java.net.*;
054:        import java.io.*;
055:        import java.lang.reflect.*;
056:        import javax.servlet.http.HttpServletRequest;
057:        import javax.servlet.jsp.PageContext;
058:        import org.jdom.*;
059:        import org.jdom.input.SAXBuilder;
060:        import org.apache.struts.action.Action;
061:        import org.apache.log4j.Logger;
062:        import org.jaffa.security.SecurityTag;
063:        import org.jaffa.config.Config;
064:        import org.jaffa.util.URLHelper;
065:        import org.jaffa.util.MessageHelper;
066:        import org.jaffa.util.DefaultEntityResolver;
067:        import org.jaffa.util.DefaultErrorHandler;
068:
069:        /** MainMenu Class - Reads the XML file containing the Menu Groups and Options
070:         * and builds a LinkedHashMap of Groups / Options that the User logged in has
071:         * security access to.
072:         *
073:         * @author MegS
074:         * @version 1.0
075:         */
076:        public class MainMenu {
077:            /** Set up Logging for Log4J */
078:            private static Logger log = Logger.getLogger(MainMenu.class);
079:
080:            // If no property is specified in the framework.properties file, this is where the system
081:            // will look for the menulist.xml policy file.
082:            private static final String DEFAULT_MENULIST_LOCATION = "classpath:///resources/menu-list.xml";
083:
084:            private Option m_option = null;
085:            private Group m_group = null;
086:            private Option[] m_options = null;
087:            private Collection coll = new ArrayList();
088:            // XML Elements referred
089:            private static final String XML_GROUP = "group";
090:            private static final String XML_OPTION = "option";
091:            private static final String XML_GROUPNAME = "group-name";
092:            private static final String XML_DESC = "description";
093:            private static final String XML_GROUPICON = "group-icon";
094:            private static final String XML_OPTIONNAME = "option-name";
095:            private static final String XML_OPTIONICON = "option-icon";
096:            private static final String XML_COMPONENT = "component";
097:            private static final String XML_TITLE = "title";
098:            private static final String XML_URL = "url";
099:            private Map hm;
100:
101:            /** Default constructor.
102:             */
103:            public MainMenu() {
104:                reset();
105:            }
106:
107:            /** Builds the HashMap of the Menu Groups / Options the User logged in has
108:             * security access to, if the HashMap hasnt already been built.
109:             * @param request  The web server request to use for context */
110:            public void determineComp(HttpServletRequest request,
111:                    PageContext pageContext) {
112:                if (hm == null) {
113:                    try {
114:                        this .process(request, pageContext);
115:                    } catch (JDOMException j) {
116:                        log.error("Error Loading Menu XML", j);
117:                    } catch (FileNotFoundException f) {
118:                        log.error("Error Loading Menu XML - File Not Found");
119:                    }
120:                }
121:            }
122:
123:            private void process(HttpServletRequest request,
124:                    PageContext pageContext) throws JDOMException,
125:                    FileNotFoundException {
126:                String prop = (String) Config.getProperty(
127:                        Config.PROP_MENULIST_URL, DEFAULT_MENULIST_LOCATION);
128:                URL roleUrl = null;
129:                try {
130:                    // Create a URL for the resource file...
131:                    roleUrl = URLHelper.newExtendedURL(prop);
132:                } catch (MalformedURLException e) {
133:                    log
134:                            .fatal("Can't Find Menu List File, Bad URL - "
135:                                    + prop, e);
136:                    throw new SecurityException();
137:                }
138:                // parse the tempFileName.. Force XML validation
139:                SAXBuilder builder = new SAXBuilder(true);
140:
141:                // Specifies the EntityResolver to resolve DTD used in XML documents
142:                builder.setEntityResolver(new DefaultEntityResolver());
143:
144:                // Specifies the ErrorHandler to handle warning/error/fatalError conditions
145:                builder.setErrorHandler(new DefaultErrorHandler());
146:
147:                Document document = builder.build(roleUrl);
148:                Element root = document.getRootElement();
149:                hm = new LinkedHashMap();
150:                List components = root.getChildren();
151:
152:                List groups = new ArrayList();
153:
154:                for (Iterator itr = components.iterator(); itr.hasNext();) {
155:
156:                    if (m_group == null)
157:                        m_group = new Group();
158:                    else {
159:                        if (!(coll.isEmpty())) {
160:                            hm.put(m_group, coll);
161:                            coll = new ArrayList();
162:                            m_group = new Group();
163:                        }
164:                    }
165:
166:                    Element group = (Element) itr.next();
167:                    String groupName = group.getChildTextTrim(XML_GROUPNAME);
168:                    String description = group.getChildTextTrim(XML_DESC);
169:                    String groupIcon = group.getChildTextTrim(XML_GROUPICON);
170:                    String title = group.getChildTextTrim(XML_TITLE);
171:                    String gTitle = null;
172:                    if (MessageHelper.hasTokens(title)) {
173:                        gTitle = MessageHelper
174:                                .replaceTokens(pageContext, title);
175:                        title = gTitle;
176:                    }
177:                    m_group.setGroupName(groupName);
178:                    m_group.setDescription(description);
179:                    m_group.setGroupIcon(groupIcon);
180:                    m_group.setGroupTitle(title);
181:
182:                    List groupElements = group.getChildren(XML_OPTION);
183:                    int i = 0;
184:                    for (Iterator itr1 = groupElements.iterator(); itr1
185:                            .hasNext();) {
186:                        Element build = (Element) itr1.next();
187:                        String optionName = build
188:                                .getChildTextTrim(XML_OPTIONNAME);
189:                        String optionIcon = build
190:                                .getChildTextTrim(XML_OPTIONICON);
191:                        String component = build
192:                                .getChildTextTrim(XML_COMPONENT);
193:                        String comp_URL;
194:                        boolean compExist = false;
195:                        if (build.getChildTextTrim(XML_COMPONENT) == null
196:                                || build.getChildTextTrim(XML_COMPONENT)
197:                                        .length() == 0)
198:                            comp_URL = build.getChildTextTrim(XML_URL);
199:                        else {
200:                            comp_URL = build.getChildTextTrim(XML_COMPONENT);
201:                            compExist = true;
202:                        }
203:
204:                        if (log.isDebugEnabled())
205:                            log.debug(XML_COMPONENT + ":" + comp_URL);
206:                        m_option = new Option();
207:                        String name = null;
208:                        if (MessageHelper.hasTokens(optionName)) {
209:                            name = MessageHelper.replaceTokens(pageContext,
210:                                    optionName);
211:                            optionName = name;
212:                        }
213:                        if (log.isDebugEnabled())
214:                            log.debug("The Option Name is : " + optionName);
215:                        m_option.setName(optionName);
216:                        m_option.setIcon(optionIcon);
217:                        m_option.setCompURL(comp_URL);
218:                        if (compExist) {
219:                            m_option.setURLTrue(false);
220:                            // check if user has access to the component
221:                            if (SecurityTag.hasComponentAccess(request,
222:                                    comp_URL))
223:                                coll.add(m_option);
224:                        } else {
225:                            m_option.setURLTrue(true);
226:                            coll.add(m_option);
227:                        }
228:                    }
229:                }
230:                if (!(coll.isEmpty()))
231:                    hm.put(m_group, coll);
232:            }
233:
234:            /** Reset the HashMap to null
235:             */
236:            public void reset() {
237:                hm = null;
238:            }
239:
240:            /** Return Map containing list of Groups / Options User logged in has access.
241:             * @return Map - Groups and Options User has access to.
242:             */
243:            public Map getComponents() {
244:                return (Map) hm;
245:            }
246:
247:            /** Test that the menu can be read ok */
248:            public static void main(String[] args) {
249:                MainMenu menu = new MainMenu();
250:                menu.determineComp(null, null);
251:                Map mp = menu.getComponents();
252:                if (mp.isEmpty()) {
253:                    System.out.println("HashMap is empty");
254:                } else {
255:                    Collection coll = new ArrayList();
256:
257:                    Set set = mp.entrySet();
258:                    Iterator i = set.iterator();
259:                    while (i.hasNext()) {
260:                        Map.Entry me = (Map.Entry) i.next();
261:                        Group group = (Group) me.getKey();
262:                        ArrayList al = new ArrayList();
263:                        al = (ArrayList) me.getValue();
264:                        System.out.println("Group = " + group);
265:                        String icon = group.getGroupIcon();
266:                        String desc = group.getDescription();
267:                        System.out.println("Desc = " + desc);
268:                        System.out.println("Option = " + al);
269:                        for (Iterator it = al.iterator(); it.hasNext();) {
270:                            Option opt = (Option) it.next();
271:                            String optIcon = opt.getIcon();
272:                            String optLink = opt.getCompURL();
273:                            boolean URL = opt.getURLTrue();
274:                            System.out.println("Each option is: " + opt);
275:                            System.out.println("Icon is: " + optIcon);
276:                        }
277:                    }
278:                }
279:            }
280:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.