Package org.internna.iwebmvc.core.xml

Source Code of org.internna.iwebmvc.core.xml.MenuManagerImpl

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.core.xml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Element;
import org.internna.iwebmvc.core.AbstractResourceLoader;
import org.internna.iwebmvc.core.crypto.Cipherer;
import org.internna.iwebmvc.model.core.Menu;
import org.internna.iwebmvc.model.core.MenuBar;
import org.internna.iwebmvc.model.core.MenuItem;
import org.internna.iwebmvc.utils.Dom4JUtils;
import org.internna.iwebmvc.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

/**
*
* @author Jose Noheda
* @since 1.0
*/
@Component
@Scope("session")
public class MenuManagerImpl extends AbstractResourceLoader implements MenuManager, ServletContextAware {

    private static Log log = LogFactory.getLog(MenuManagerImpl.class);

    private MenuBar menuBar;
    private ServletContext servletContext;
    @Autowired protected Cipherer cipherer;
    protected String menuPath = "/WEB-INF/resources/menu/menu.xml";

    public MenuBar getMenuBar() {
        return menuBar;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @PostConstruct
    public void init() throws Exception {
        InputStream in = getResource(servletContext, menuPath);
        Element root = Dom4JUtils.parse(in).getRootElement();
        menuBar = new MenuBar();
        for (Object menu : root.elements("menu")) menuBar.addMenu(buildMenu((Element) menu));
    }

    protected Menu buildMenu(Element element) {
        Menu m = null;
        if (element != null) {
            m = new Menu();
            m.setKey(element.attributeValue("menukey"));
            List<MenuItem> items = new ArrayList<MenuItem>();
            for (Object item : element.elements("menuitem")) items.add(buildMenuItem((Element) item));
            m.setAction(getAction(element));
            if (items.size() > 0) m.setItems(items);
        }
        return m;
    }

    protected MenuItem buildMenuItem(Element element) {
        MenuItem item = new MenuItem();
        item.setKey(element.attributeValue("menukey"));
        item.setMenu(buildMenu(element.element("menu")));
        item.setAction(getAction(element));
        return item;
    }

    protected String getAction(Element element) {
        String action = element.elementText("jsaction");
        if (action != null) action = action.replace("<context_path>", servletContext.getContextPath());
        List<String> split = StringUtils.splitBetweenDelimiters(action, "{", "}");
        return split.size() > 0 ? split.get(0) + cipherer.encrypt(split.get(1)) + split.get(2) : action;
    }

}
TOP

Related Classes of org.internna.iwebmvc.core.xml.MenuManagerImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.