001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (http://www.alkacon.com)
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 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.jsp.util;
029
030import org.opencms.file.CmsObject;
031import org.opencms.util.CmsCollectionsGenericWrapper;
032import org.opencms.xml.containerpage.CmsDynamicFunctionBean;
033import org.opencms.xml.containerpage.CmsDynamicFunctionBean.Format;
034
035import java.util.Collections;
036import java.util.Map;
037
038import org.apache.commons.collections.Transformer;
039
040/**
041 * A wrapper class for using dynamic function beans inside JSPs via the EL.<p>
042 */
043public class CmsDynamicFunctionBeanWrapper {
044
045    /** The internal CMS object to use. */
046    protected CmsObject m_cms;
047
048    /** The dynamic function bean which is being wrapped. */
049    protected CmsDynamicFunctionBean m_functionBean;
050
051    /**
052     * Creates a new wrapper instance.<p>
053     *
054     * @param cms the CMS context to use
055     * @param functionBean the dynamic function bean to wrap
056     */
057    public CmsDynamicFunctionBeanWrapper(CmsObject cms, CmsDynamicFunctionBean functionBean) {
058
059        m_cms = cms;
060        m_functionBean = functionBean;
061    }
062
063    /**
064     * Gets the lazy map for accessing the various function formats.<p>
065     *
066     * @return a map which allows access to the various function formats
067     */
068    public Object getFormatFor() {
069
070        Transformer mapFunction = new Transformer() {
071
072            public Object transform(Object param) {
073
074                if (m_functionBean == null) {
075                    return new CmsDynamicFunctionFormatWrapper(m_cms, null);
076                }
077                int width = -1;
078                String type = null;
079                boolean isWidth = false;
080                if (param instanceof Long) {
081                    width = (int)((Long)param).longValue();
082                    isWidth = true;
083                } else if (param instanceof Integer) {
084                    width = ((Integer)param).intValue();
085                    isWidth = true;
086                } else {
087                    type = param.toString();
088                }
089                Format format;
090                if (isWidth) {
091                    format = m_functionBean.getFormatForContainer(m_cms, "", width);
092                } else {
093                    format = m_functionBean.getFormatForContainer(m_cms, type, -1);
094                }
095                CmsDynamicFunctionFormatWrapper wrapper = new CmsDynamicFunctionFormatWrapper(m_cms, format);
096                return wrapper;
097            }
098        };
099        return CmsCollectionsGenericWrapper.createLazyMap(mapFunction);
100    }
101
102    /**
103     * Gets the JSP file name of the wrapped dynamic function bean's main format.<p>
104     *
105     * @return a jsp file name
106     */
107    public String getJsp() {
108
109        if (m_functionBean == null) {
110            return "";
111        }
112        Format format = m_functionBean.getMainFormat();
113        CmsDynamicFunctionFormatWrapper wrapper = new CmsDynamicFunctionFormatWrapper(m_cms, format);
114        return wrapper.getJsp();
115    }
116
117    /**
118     * Gets the parameters of the wrapped dynamic function bean's main format.<p>
119     *
120     * @return the map of parameters
121     */
122    public Map<String, String> getParam() {
123
124        return getParameters();
125    }
126
127    /**
128     * Gets the parameters of the wrapped dynamic function bean's main format.<p>
129     *
130     * @return the map of parameters
131     */
132    public Map<String, String> getParameters() {
133
134        if (m_functionBean == null) {
135            return Collections.emptyMap();
136        }
137        Format format = m_functionBean.getMainFormat();
138        CmsDynamicFunctionFormatWrapper wrapper = new CmsDynamicFunctionFormatWrapper(m_cms, format);
139        return wrapper.getParameters();
140    }
141}