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, 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.ade.configuration.plugins.CmsTemplatePlugin;
031import org.opencms.file.CmsObject;
032import org.opencms.main.CmsLog;
033import org.opencms.relations.CmsLink;
034import org.opencms.relations.CmsLinkInfo;
035
036import java.util.Map;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * Wrapper around template plugin objects for use in JSP EL expressions.
042 */
043public class CmsTemplatePluginWrapper {
044
045    /** Logger instance for this class. */
046    private static final Log LOG = CmsLog.getLog(CmsTemplatePluginWrapper.class);
047
048    /** The current CmsObject. */
049    private CmsObject m_cms;
050
051    /** The wrapped plugin. */
052    private CmsTemplatePlugin m_plugin;
053
054    /**
055     * Creates a new wrapper.
056     *
057     * @param cms the current CmsObject
058     * @param plugin the plugin to wrap
059     */
060    public CmsTemplatePluginWrapper(CmsObject cms, CmsTemplatePlugin plugin) {
061
062        m_cms = cms;
063        m_plugin = plugin;
064    }
065
066    /**
067     * Gets the plugin attributes.
068     *
069     * @return the plugin attributes
070     */
071    public Map<String, String> getAttributes() {
072
073        return m_plugin.getAttributes();
074    }
075
076    /**
077     * Gets the group of the plugin.
078     *
079     * @return the group
080     */
081    public String getGroup() {
082
083        return m_plugin.getGroup();
084    }
085
086    /**
087     * Gets a link to the plugin target.
088     *
089     * @return a link to the plugin target
090     */
091    public String getLink() {
092
093        CmsLinkInfo target = m_plugin.getTarget();
094        CmsLink targetLink = target.toLink();
095        if (targetLink == null) {
096            LOG.warn("getLink called on template plugin with no link target: " + toString());
097            return "";
098        }
099        String link = targetLink.getLink(m_cms);
100        return link;
101    }
102
103    /**
104     * Gets the order of the plugin.
105     *
106     * @return the order of the plugin
107     */
108    public int getOrder() {
109
110        return m_plugin.getOrder();
111    }
112
113    /**
114     * Returns the path of the resource, if this is an internal link, and null otherwise.
115     *
116     * @return the path of the link target
117     */
118    public String getPath() {
119
120        CmsLinkInfo target = m_plugin.getTarget();
121        if (!target.isInternal()) {
122            return null;
123        }
124        CmsLink targetLink = target.toLink();
125        if (targetLink == null) {
126            return null;
127        }
128        targetLink.checkConsistency(m_cms);
129        return m_cms.getRequestContext().removeSiteRoot(targetLink.getTarget());
130    }
131
132    /**
133     * Gets the plugin bean wrapped by this wrapper.
134     *
135     * @return the wrapped template plugin
136     */
137    public CmsTemplatePlugin getPlugin() {
138
139        return m_plugin;
140    }
141
142    /**
143     * @see java.lang.Object#toString()
144     */
145    @Override
146    public String toString() {
147
148        return m_plugin.toString();
149    }
150
151}