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.loader;
029
030import org.opencms.file.CmsResource;
031
032/**
033 * Facade object that provides access to the template loader for a resource.<p>
034 *
035 * Some resource types are actually not loadable themself but require a template
036 * to be processed. This template is attached using the <code>template</code> property.
037 * Depending on the resource type of the template itself, a loader is then selected that
038 * processed the requested resource. The processing itself might start on the template,
039 * or on the requested resource (this will depend on the loader and the resource type implementation).<p>
040 *
041 * @since 6.0.0
042 */
043public class CmsTemplateLoaderFacade {
044
045    /** The selected template context. */
046    private CmsTemplateContext m_context;
047
048    /** The resource loader. */
049    private I_CmsResourceLoader m_loader;
050
051    /** The resource requested by the user. */
052    private CmsResource m_resource;
053
054    /** The template file attached to the resource. */
055    private CmsResource m_template;
056
057    /** The template name. */
058    private String m_templateName = "";
059
060    /**
061     * Creates a new template loader facade.<p>
062     *
063     * Some resource types are actually not loadable themself but require a template
064     * to be processed. This template is attached using the <code>template</code> property.
065     * Depending on the resource type of the template itself, a loader is then selected that
066     * processed the requested resource. The processing itself might start on the template,
067     * or on the requested resource (this will depend on the loader and the resource type implementation).<p>
068     *
069     * @param loader the loader to use
070     * @param resource the file to use
071     * @param template the template to use (ignored if null)
072     * @throws CmsLoaderException in case the template file does not use a loader that actually supports templates
073     */
074    public CmsTemplateLoaderFacade(I_CmsResourceLoader loader, CmsResource resource, CmsResource template)
075    throws CmsLoaderException {
076
077        if (!loader.isUsableForTemplates()) {
078            throw new CmsLoaderException(Messages.get().container(Messages.ERR_LOADER_NOT_TEMPLATE_ENABLED_0));
079        }
080        m_loader = loader;
081        m_resource = resource;
082        m_template = template;
083    }
084
085    /**
086     * Returns the loader.<p>
087     *
088     * @return the loader
089     */
090    public I_CmsResourceLoader getLoader() {
091
092        return m_loader;
093    }
094
095    /**
096     * Returns the loaders start resource.<p>
097     *
098     * @return the loaders start resource
099     */
100    public CmsResource getLoaderStartResource() {
101
102        if (m_loader.isUsingUriWhenLoadingTemplate()) {
103            return m_resource;
104        } else {
105            return m_template;
106        }
107    }
108
109    /**
110     * Returns the resource.<p>
111     *
112     * @return the resource
113     */
114    public CmsResource getResource() {
115
116        return m_resource;
117    }
118
119    /**
120     * Returns the template.<p>
121     *
122     * @return the template
123     */
124    public CmsResource getTemplate() {
125
126        return m_template;
127    }
128
129    /**
130     * Gets the template context.<p>
131     *
132     * @return the template context
133     */
134    public CmsTemplateContext getTemplateContext() {
135
136        return m_context;
137    }
138
139    /**
140     * Gets the template name.<p>
141     *
142     * @return the template name
143     */
144    public String getTemplateName() {
145
146        return m_templateName;
147    }
148
149    /**
150     * Sets the template context.<p>
151     *
152     * @param context the template context
153     */
154    public void setTemplateContext(CmsTemplateContext context) {
155
156        m_context = context;
157    }
158
159    /**
160     * Sets the template name.<p>
161     *
162     * @param templateName the new template name
163     */
164    public void setTemplateName(String templateName) {
165
166        m_templateName = templateName;
167    }
168
169}