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