⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsjspnavbuilder.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspNavBuilder.java,v $
 * Date   : $Date: 2005/06/28 13:30:16 $
 * Version: $Revision: 1.23 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.jsp;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;

/**
 * Bean to provide a convenient way to build navigation structures based on the
 * <code>{@link org.opencms.jsp.CmsJspNavElement}</code>.<p>
 *
 * Use this together with the <code>{@link org.opencms.jsp.CmsJspActionElement}</code>
 * to obtain navigation information based on the current users permissions.
 * For example, use <code>{@link #getNavigationForFolder(String)}</code> and pass the 
 * value of the current OpenCms user context uri obtained 
 * from <code>{@link org.opencms.file.CmsRequestContext#getUri()}</code> as argument to obtain a list
 * of all items in the navigation of the current folder. Then use a simple scriptlet to 
 * iterate over these items and create a HTML navigation.<p>
 *
 * @author  Alexander Kandzior 
 * 
 * @version $Revision: 1.23 $ 
 * 
 * @since 6.0.0 
 * 
 * @see org.opencms.jsp.CmsJspNavElement
 */
public class CmsJspNavBuilder {

    /**
     * Internal helper class to get a title - comparable CmsResource for channels.<p>
     */
    private static class ResourceTitleContainer implements Comparable {

        /** The resource. */
        protected CmsResource m_res;

        /** The title of the resource. */
        protected String m_title;

        /**
         * @param cms context provider for the current request
         * @param res the resource to compare
         */
        ResourceTitleContainer(CmsObject cms, CmsResource res) {

            m_res = res;
            try {
                cms.getRequestContext().saveSiteRoot();
                cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
                m_title = cms.readPropertyObject(
                    res,
                    org.opencms.file.CmsPropertyDefinition.PROPERTY_TITLE,
                    false).getValue();
                cms.getRequestContext().restoreSiteRoot();
            } catch (Exception e) {
                m_title = "";
            }
        }

        /**
         * @see java.lang.Comparable#compareTo(Object)
         */
        public int compareTo(Object obj) {

            if (obj == this) {
                return 0;
            }
            if (obj instanceof ResourceTitleContainer) {
                if (m_title == null) {
                    return 1;
                }
                return (m_title.toLowerCase().compareTo(((ResourceTitleContainer)obj).m_title.toLowerCase()));
            }
            return 0;
        }
    }

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsJspNavBuilder.class);

    // Member variables
    private CmsObject m_cms;
    private String m_requestUri;
    private String m_requestUriFolder;

    /**
     * Empty constructor, so that this bean can be initialized from a JSP.<p> 
     * 
     * @see java.lang.Object#Object()
     */
    public CmsJspNavBuilder() {

        // empty
    }

    /**
     * Default constructor.<p>
     * 
     * @param cms context provider for the current request
     */
    public CmsJspNavBuilder(CmsObject cms) {

        init(cms);
    }

    /**
     * Returns all subfolders of a channel, or an empty array if 
     * the folder does not exist or has no subfolders.<p>
     * 
     * @param cms context provider for the current request
     * @param channel the channel to look for subfolders in
     * @return an unsorted list of CmsResources
     */
    public static List getChannelSubFolders(CmsObject cms, String channel) {

        if (!channel.startsWith("/")) {
            channel = "/" + channel;
        }
        if (!channel.endsWith("/")) {
            channel += "/";
        }

        // Now read all subchannels of this channel    
        List subChannels = new ArrayList();
        cms.getRequestContext().saveSiteRoot();
        try {
            cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
            subChannels = cms.getSubFolders(channel);
        } catch (CmsException e) {
            if (LOG.isErrorEnabled()) {
                // will be localized if the CmsException was constructoed localized.
                LOG.error(e.getLocalizedMessage());
            }
        } finally {
            cms.getRequestContext().restoreSiteRoot();
        }

        // Create an ArrayList out of the Vector        
        java.util.ArrayList list = new java.util.ArrayList(subChannels.size());
        list.addAll(subChannels);
        return list;
    }

    /**
     * Returns all subfolders of a sub channel that has 
     * the given parent channel, or an empty array if 
     * that combination does not exist or has no subfolders.<p>
     * 
     * @param cms context provider for the current request
     * @param parentChannel the parent channel
     * @param subChannel the sub channel
     * @return an unsorted list of CmsResources
     */
    public static List getChannelSubFolders(CmsObject cms, String parentChannel, String subChannel) {

        String channel = null;
        if (subChannel == null) {
            subChannel = "";
        } else if (subChannel.startsWith("/")) {
            subChannel = subChannel.substring(1);
        }
        if (parentChannel == null) {
            parentChannel = "";
        }
        if (parentChannel.endsWith("/")) {
            channel = parentChannel + subChannel;
        } else {
            channel = parentChannel + "/" + subChannel;
        }
        return getChannelSubFolders(cms, channel);
    }

    /**
     * Returns all subfolders of a channel, 
     * sorted by "Title" property ascending, or an empty array if 
     * the folder does not exist or has no subfolders.
     * 
     * @param cms context provider for the current request
     * @param channel the parent channel
     * @param subChannel the sub channel
     * @return a sorted list of CmsResources
     */
    public static List getChannelSubFoldersSortTitleAsc(CmsObject cms, String channel, String subChannel) {

        List subChannels = getChannelSubFolders(cms, channel, subChannel);
        // Create an ArrayList out of the Vector        
        ArrayList tmpList = new java.util.ArrayList(subChannels.size());
        for (int i = 0; i < subChannels.size(); i++) {
            CmsResource res = (CmsResource)subChannels.get(i);
            ResourceTitleContainer container = new ResourceTitleContainer(cms, res);
            tmpList.add(container);
        }
        Collections.sort(tmpList);
        java.util.ArrayList list = new java.util.ArrayList(subChannels.size());
        for (int i = 0; i < tmpList.size(); i++) {
            ResourceTitleContainer container = (ResourceTitleContainer)tmpList.get(i);
            list.add(container.m_res);
        }
        return list;
    }

    /**
     * Returns the full name (including vfs path) of the default file for this nav element 
     * or <code>null</code> if the nav element is not a folder.<p>
     * 
     * The default file of a folder is determined by the value of the property 
     * <code>default-file</code> or the systemwide property setting.
     * 
     * @param cms the cms object
     * @param folder full name of the folder
     * 
     * @return the name of the default file
     */
    public static String getDefaultFile(CmsObject cms, String folder) {

        if (folder.endsWith("/")) {
            List defaultFolders = new ArrayList();
            try {
                CmsProperty p = cms.readPropertyObject(folder, CmsPropertyDefinition.PROPERTY_DEFAULT_FILE, false);
                defaultFolders.add(p.getValue());
            } catch (CmsException exc) {
                // noop
            }

            defaultFolders.addAll(OpenCms.getDefaultFiles());

            for (Iterator i = defaultFolders.iterator(); i.hasNext();) {
                String defaultName = (String)i.next();
                if (cms.existsResource(folder + defaultName)) {
                    return folder + defaultName;
                }
            }

            return folder;
        }

        return null;
    }

    /**
     * Collect all navigation elements from the files in the given folder,
     * navigation elements are of class CmsJspNavElement.<p>
     *
     * @param cms context provider for the current request
     * @param folder the selected folder
     * @return a sorted (ascending to nav position) ArrayList of navigation elements
     */
    public static List getNavigationForFolder(CmsObject cms, String folder) {

        folder = CmsResource.getFolderPath(folder);
        List result = new ArrayList();

        List resources;
        try {
            resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
        } catch (Exception e) {
            return Collections.EMPTY_LIST;
        }

        for (int i = 0; i < resources.size(); i++) {
            CmsResource r = (CmsResource)resources.get(i);
            CmsJspNavElement element = getNavigationForResource(cms, cms.getSitePath(r));
            if ((element != null) && element.isInNavigation()) {
                result.add(element);
            }
        }
        Collections.sort(result);
        return result;
    }

    /** 
     * Build a navigation for the folder that is either minus levels up 
     * from the given folder, or that is plus levels down from the 
     * root folder towards the given folder.<p> 
     * 
     * If level is set to zero the root folder is used by convention.<p>
     * 
     * @param cms context provider for the current request
     * @param folder the selected folder
     * @param level if negative, walk this many levels up, if positive, walk this many 
     * levels down from root folder 
     * @return a sorted (ascending to nav position) ArrayList of navigation elements
     */
    public static List getNavigationForFolder(CmsObject cms, String folder, int level) {

        folder = CmsResource.getFolderPath(folder);
        // If level is one just use root folder
        if (level == 0) {
            return getNavigationForFolder(cms, "/");
        }
        String navfolder = CmsResource.getPathPart(folder, level);
        // If navfolder found use it to build navigation
        if (navfolder != null) {
            return getNavigationForFolder(cms, navfolder);
        }
        // Nothing found, return empty list
        return Collections.EMPTY_LIST;
    }

    /**
     * Returns a CmsJspNavElement for the named resource.<p>
     * 
     * @param cms context provider for the current request
     * @param resource the resource name to get the nav information for, 
     * must be a full path name, e.g. "/docs/index.html".
     * @return a CmsJspNavElement for the given resource
     */
    public static CmsJspNavElement getNavigationForResource(CmsObject cms, String resource) {

        List properties;
        try {
            properties = cms.readPropertyObjects(resource, false);
        } catch (Exception e) {
            return null;
        }
        int level = CmsResource.getPathLevel(resource);
        if (resource.endsWith("/")) {
            level--;
        }
        return new CmsJspNavElement(resource, CmsProperty.toMap(properties), level);
    }

    /**
     * Builds a tree navigation for the folders between the provided start and end level.<p>
     * 
     * A tree navigation includes all nav elements that are required to display a tree structure.
     * However, the data structure is a simple list.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -