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.xml.content;
029
030import org.opencms.main.CmsLog;
031
032import org.apache.commons.logging.Log;
033
034/**
035 * A configuration bean representing a <ChangeHandler> element configured in an XSD's field settings.
036 */
037public class CmsChangeHandlerConfig {
038
039    /** Logger instance for this class. */
040    private static final Log LOG = CmsLog.getLog(CmsChangeHandlerConfig.class);
041
042    /** Name of the field for which this is configured. */
043    private String m_field;
044
045    /** The class name for the change handler. */
046    private String m_className;
047
048    /** The configuration string for the change handler. */
049    private String m_config;
050
051    /**
052     * Creates a new instance.
053     *
054     * @param field the field name
055     * @param className the handler class name
056     * @param config the configuration string
057     */
058    public CmsChangeHandlerConfig(String field, String className, String config) {
059
060        m_field = field;
061        m_className = className.trim();
062        m_config = config;
063    }
064
065    /**
066     * Gets the class name for the handler.
067     *
068     * @return the class name
069     */
070    public String getClassName() {
071
072        return m_className;
073    }
074
075    /**
076     * Gets the configuration string for the handler.
077     *
078     * @return the configuration string
079     */
080    public String getConfig() {
081
082        return m_config;
083    }
084
085    /**
086     * Gets the field name for which the handler is configured.
087     *
088     * @return the field name for which the handler is configured
089     */
090    public String getField() {
091
092        return m_field;
093    }
094
095    /**
096     * Creates a new handler instance using this configuration and the given scope.
097     *
098     * @param scope the scope
099     * @return the new handler instance
100     */
101    public java.util.Optional<I_CmsXmlContentEditorChangeHandler> newHandler(String scope) {
102
103        try {
104            Class<?> cls = Class.forName(m_className, false, getClass().getClassLoader());
105            if (I_CmsXmlContentEditorChangeHandler.class.isAssignableFrom(cls)) {
106                I_CmsXmlContentEditorChangeHandler handler = (I_CmsXmlContentEditorChangeHandler)(cls.newInstance());
107                handler.setConfiguration(m_config);
108                handler.setScope(scope);
109                return java.util.Optional.of(handler);
110            } else {
111                throw new Exception("Incompatible class for editor change handler: " + m_className);
112            }
113        } catch (Exception e) {
114            LOG.error("Could not create editor change handler: " + e.getLocalizedMessage(), e);
115            return java.util.Optional.empty();
116        }
117    }
118}