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.configuration;
029
030import org.apache.commons.digester3.Digester;
031import org.apache.commons.digester3.Rule;
032
033import org.dom4j.Element;
034import org.xml.sax.Attributes;
035
036/**
037* Helper class for parsing an element with no content but several attributes into a subclass of I_CmsConfigurationParameterHandler.<p>
038*/
039public class CmsElementWithAttrsParamConfigHelper {
040
041    /** The attributes to read / write. */
042    private String[] m_attrs;
043
044    /** The xpath of the element. */
045    private String m_basePath;
046
047    /** The class of the configuration object to create (must be subclass of I_CmsConfigurationParameterHandler). */
048    private Class<?> m_class;
049
050    /** The name of the XML element. */
051    private String m_name;
052
053    /**
054     * Creates a new instance.<p>
055     *
056     * @param parentPath the parent XPath
057     * @param name the XML element name
058     * @param cls the class to use for the configuration (must be subclass of I_CmsConfigurationParameterHandler)
059     * @param attrs the attributes to read / write
060     */
061    public CmsElementWithAttrsParamConfigHelper(String parentPath, String name, Class<?> cls, String... attrs) {
062
063        m_basePath = parentPath + "/" + name;
064        m_name = name;
065        m_class = cls;
066        m_attrs = attrs;
067    }
068
069    /**
070     * Adds the configuration parsing rules to the digester.<p>
071     *
072     * @param digester the digester to which the rules should be added
073     */
074    public void addRules(Digester digester) {
075
076        digester.addRule(m_basePath, new Rule() {
077
078            @SuppressWarnings("synthetic-access")
079            @Override
080            public void begin(String namespace, String name, Attributes attributes) throws Exception {
081
082                I_CmsConfigurationParameterHandler config = (I_CmsConfigurationParameterHandler)(m_class.newInstance());
083                for (String attr : m_attrs) {
084                    String attrValue = attributes.getValue(attr);
085                    if (attrValue != null) {
086                        config.addConfigurationParameter(attr, attrValue);
087                    }
088                }
089                config.initConfiguration();
090                getDigester().push(config);
091            }
092
093            @Override
094            public void end(String namespace, String name) throws Exception {
095
096                getDigester().pop();
097            }
098
099        });
100    }
101
102    /**
103     * Generates the XML configuration from the given configuration object.<p>
104     *
105     * @param parent the parent element
106     * @param config the configuration
107     */
108    public void generateXml(Element parent, I_CmsConfigurationParameterHandler config) {
109
110        if (config != null) {
111            Element elem = parent.addElement(m_name);
112            for (String attrName : m_attrs) {
113                String value = config.getConfiguration().get(attrName);
114                if (value != null) {
115                    elem.addAttribute(attrName, value);
116                }
117            }
118        }
119    }
120
121    /**
122     * Gets the xPath of the configuration element.<p>
123     *
124     * @return the xPath of the configuration element
125     */
126    public String getBasePath() {
127
128        return m_basePath;
129    }
130}