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.workplace.editors;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.loader.CmsTemplateContextManager;
034import org.opencms.loader.I_CmsTemplateContextProvider;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.OpenCms;
038import org.opencms.util.CmsStringUtil;
039
040import org.apache.commons.logging.Log;
041
042/**
043 * A default editor CSS handler to obtain the CSS style sheet path from the template property value of the template itself.<p>
044 *
045 * @since 6.9.2
046 */
047public class CmsEditorCssHandlerDefault implements I_CmsEditorCssHandler {
048
049    /** The log object for this class. */
050    private static final Log LOG = CmsLog.getLog(CmsEditorCssHandlerDefault.class);
051
052    /** Sitemap attribute that can be used to configure the editor stylesheet. */
053    public static final String ATTR_TEMPLATE_EDITOR_CSS = "template.editor.css";
054
055    /** Request context attribute used to pass the editor stylesheet (normally defined as a meta tag) to the CSS handler. */
056    public static final String ATTRIBUTE_EDITOR_STYLESHEET = "cms-editor-stylesheet";
057
058    /**
059     * @see org.opencms.workplace.editors.I_CmsEditorCssHandler#getUriStyleSheet(org.opencms.file.CmsObject, java.lang.String)
060     */
061    public String getUriStyleSheet(CmsObject cms, String editedResourcePath) {
062
063        String editContext = (String)(cms.getRequestContext().getAttribute(CmsXmlContentEditor.ATTRIBUTE_EDITCONTEXT));
064        String result = "";
065        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(editContext)) {
066            // prefer the style sheet of the edit context (usually this will be a container page)
067            result = internalGetUriStyleSheet(cms, editContext);
068        }
069        if (CmsStringUtil.isEmptyOrWhitespaceOnly(result)) {
070            result = internalGetUriStyleSheet(cms, editedResourcePath);
071        }
072        return result;
073    }
074
075    /**
076     * @see org.opencms.workplace.editors.I_CmsEditorCssHandler#matches(org.opencms.file.CmsObject, java.lang.String)
077     */
078    public boolean matches(CmsObject cms, String editedResourcePath) {
079
080        // this returns always true, as it is the default CSS handler
081        return true;
082    }
083
084    /**
085     * Finds the style sheet by reading the template property of the template for a given path.<p>
086     *
087     * @param cms the current CMS context
088     * @param editedResourcePath the resource path
089     *
090     * @return the CSS uri from the template for the given path
091     */
092    private String internalGetUriStyleSheet(CmsObject cms, String editedResourcePath) {
093
094        if (editedResourcePath == null) {
095            return "";
096        }
097        String result = "";
098
099        Object attr = cms.getRequestContext().getAttribute(ATTRIBUTE_EDITOR_STYLESHEET);
100        if (attr instanceof String) {
101            String editorStylesheet = (String)attr;
102            return editorStylesheet;
103        }
104
105        try {
106            CmsADEConfigData config = OpenCms.getADEManager().lookupConfigurationWithCache(
107                cms,
108                cms.getRequestContext().addSiteRoot(editedResourcePath));
109            String cssPathFromSitemapConfig = config.getAttribute(ATTR_TEMPLATE_EDITOR_CSS, null);
110            if ((cssPathFromSitemapConfig != null) && cms.existsResource(cssPathFromSitemapConfig)) {
111                return cssPathFromSitemapConfig;
112            }
113        } catch (Exception e) {
114            LOG.error(e.getLocalizedMessage(), e);
115        }
116
117        try {
118            // determine the path of the template
119            String templatePath = "";
120            try {
121                templatePath = cms.readPropertyObject(
122                    editedResourcePath,
123                    CmsPropertyDefinition.PROPERTY_TEMPLATE,
124                    true).getValue("");
125                if (CmsTemplateContextManager.isProvider(templatePath)) {
126                    I_CmsTemplateContextProvider provider = OpenCms.getTemplateContextManager().getTemplateContextProvider(
127                        templatePath);
128                    if (provider != null) {
129                        String providerResult = provider.getEditorStyleSheet(cms, editedResourcePath);
130                        if (providerResult != null) {
131                            return providerResult;
132                        }
133                    }
134                }
135            } catch (CmsException e) {
136                if (LOG.isWarnEnabled()) {
137                    LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_PROP_FAILED_0), e);
138                }
139            }
140            if (CmsStringUtil.isNotEmpty(templatePath)) {
141                // read the template property value from the template file where the absolute CSS path is (or should be) stored
142                result = cms.readPropertyObject(templatePath, CmsPropertyDefinition.PROPERTY_TEMPLATE, false).getValue(
143                    "");
144            }
145        } catch (CmsException e) {
146            if (LOG.isWarnEnabled()) {
147                LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_PROP_STYLESHEET_FAILED_0), e);
148            }
149        }
150        return result;
151    }
152
153}