001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (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 GmbH & Co. KG, 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;
029
030import org.opencms.file.CmsObject;
031import org.opencms.flex.CmsFlexController;
032import org.opencms.jsp.util.CmsJspNavigationBean;
033import org.opencms.main.CmsIllegalArgumentException;
034import org.opencms.main.CmsLog;
035import org.opencms.util.CmsStringUtil;
036
037import java.util.Locale;
038
039import javax.servlet.jsp.PageContext;
040
041import org.apache.commons.lang3.LocaleUtils;
042import org.apache.commons.logging.Log;
043
044/**
045 * Implementation of the <code>&lt;cms:navigation var="..." /&gt;</code> tag,
046 * used to access OpenCms VFS navigation information on a JSP with the EL.<p>
047 *
048 * @since 8.0
049 */
050public class CmsJspTagNavigation extends CmsJspScopedVarBodyTagSuport {
051
052    /** Constants for <code>type</code> attribute interpretation. */
053    public enum Type {
054        /** Bread crumb navigation. */
055        breadCrumb,
056        /** Navigation for folder. */
057        forFolder,
058        /** Navigation for resource. */
059        forResource,
060        /** Navigation for a site. */
061        forSite,
062        /** Navigation tree for folder. */
063        treeForFolder;
064
065        /**
066         * Parses a string into an enumeration element.<p>
067         *
068         * @param name the name of the enumeration element
069         *
070         * @return the enumeration element with the given name
071         *
072         * @throws IllegalArgumentException in case of an invalid enumeration name
073         */
074        public static Type parse(String name) throws IllegalArgumentException {
075
076            return Enum.valueOf(Type.class, name);
077        }
078    }
079
080    /** The log object for this class. */
081    private static final Log LOG = CmsLog.getLog(CmsJspTagNavigation.class);
082
083    /** Serial version UID required for safe serialization. */
084    private static final long serialVersionUID = 8589202895748764705L;
085
086    /** The CmsObject for the current user. */
087    protected transient CmsObject m_cms;
088
089    /** The optional end level for the navigation. */
090    protected String m_endLevel;
091
092    /** The optional parameter for the navigation. */
093    protected String m_param;
094
095    /** The optional resource for the navigation. */
096    protected String m_resource;
097
098    /** The optional start level for the navigation. */
099    protected String m_startLevel;
100
101    /** The navigation type. */
102    protected Type m_type;
103
104    /** The locale for which the property should be read. */
105    protected Locale m_locale;
106
107    /**
108     * Empty constructor, required for JSP tags.<p>
109     */
110    public CmsJspTagNavigation() {
111
112        super();
113    }
114
115    /**
116     * Constructor used for scriptlet code.<p>
117     *
118     * @param context the JSP page context
119     */
120    public CmsJspTagNavigation(PageContext context) {
121
122        setPageContext(context);
123        init();
124    }
125
126    /**
127     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
128     */
129    @Override
130    public int doStartTag() throws CmsIllegalArgumentException {
131
132        // initialize the content load tag
133        init();
134        return SKIP_BODY;
135    }
136
137    /**
138     * Returns the (optional) end level for the navigation.<p>
139     *
140     * @return the (optional) end level for the navigation
141     */
142    public String getEndLevel() {
143
144        return m_endLevel;
145    }
146
147    /**
148     * Returns the optional parameter for the navigation.<p>
149     *
150     * @return the optional parameter for the navigation
151     */
152    public String getParam() {
153
154        return m_param;
155    }
156
157    /**
158     * Returns the (optional) resource for the navigation.<p>
159     *
160     * @return the (optional) resource for the navigation
161     */
162    public String getResource() {
163
164        return m_resource;
165    }
166
167    /**
168     * Returns the (optional) start level for the navigation.<p>
169     *
170     * @return the (optional) start level for the navigation
171     */
172    public String getStartLevel() {
173
174        return m_startLevel;
175    }
176
177    /**
178     * Returns the selected navigation type.<p>
179     *
180     * This must match one of the elements in {@link Type}.<p>
181     *
182     * @return the selected navigation type
183     */
184    public String getType() {
185
186        return m_type == null ? null : m_type.toString();
187    }
188
189    /**
190     * @see javax.servlet.jsp.tagext.Tag#release()
191     */
192    @Override
193    public void release() {
194
195        m_cms = null;
196        m_startLevel = null;
197        m_endLevel = null;
198        m_resource = null;
199        m_type = null;
200        super.release();
201    }
202
203    /**
204     * Sets the (optional) end level for the navigation.<p>
205     *
206     * @param endLevel the (optional) end level for the navigation
207     */
208    public void setEndLevel(String endLevel) {
209
210        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(endLevel)) {
211            m_endLevel = endLevel.trim();
212        }
213    }
214
215    /**
216     * Sets the locale for which the property should be read.
217     *
218     * @param locale the locale for which the property should be read.
219     */
220    public void setLocale(String locale) {
221
222        try {
223            m_locale = LocaleUtils.toLocale(locale);
224        } catch (IllegalArgumentException e) {
225            LOG.error(Messages.get().getBundle().key(Messages.ERR_TAG_INVALID_LOCALE_1, "cms:navigation"), e);
226            m_locale = null;
227        }
228    }
229
230    /**
231     * Sets the optional parameter for the navigation.<p>
232     *
233     * @param param the optional parameter for the navigation to set
234     */
235    public void setParam(String param) {
236
237        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(param)) {
238            m_param = param.trim();
239        }
240    }
241
242    /**
243     * Sets the (optional) resource for the navigation.<p>
244     *
245     * @param resource the (optional) resource for the navigation
246     */
247    public void setResource(String resource) {
248
249        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(resource)) {
250            m_resource = resource.trim();
251        }
252    }
253
254    /**
255     * Sets the (optional) start level for the navigation.<p>
256     *
257     * @param startLevel the (optional) start level for the navigation
258     */
259    public void setStartLevel(String startLevel) {
260
261        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(startLevel)) {
262            m_startLevel = startLevel.trim();
263        }
264    }
265
266    /**
267     * Sets the selected navigation type.<p>
268     *
269     * This must match one of the elements in {@link Type}.<p>
270     *
271     * @param type the navigation type to set
272     */
273    public void setType(String type) {
274
275        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(type)) {
276            m_type = Type.parse(type.trim());
277        }
278    }
279
280    /**
281     * Initializes this formatter tag.<p>
282     */
283    protected void init() {
284
285        // initialize OpenCms access objects
286        CmsFlexController controller = CmsFlexController.getController(pageContext.getRequest());
287        m_cms = controller.getCmsObject();
288
289        int startLevel = m_startLevel == null ? Integer.MIN_VALUE : Integer.parseInt(m_startLevel);
290        int endLevel = m_endLevel == null ? Integer.MIN_VALUE : Integer.parseInt(m_endLevel);
291
292        // load navigation bean in the JSP context
293        CmsJspNavigationBean bean = new CmsJspNavigationBean(
294            m_cms,
295            m_type,
296            startLevel,
297            endLevel,
298            m_resource,
299            m_param,
300            m_locale);
301        storeAttribute(getVar(), bean);
302    }
303}