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.ade.configuration;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.xml.content.CmsXmlContent;
034import org.opencms.xml.content.CmsXmlContentFactory;
035import org.opencms.xml.content.CmsXmlContentProperty;
036import org.opencms.xml.content.CmsXmlContentRootLocation;
037import org.opencms.xml.content.I_CmsXmlContentValueLocation;
038
039import java.util.Collections;
040import java.util.HashMap;
041import java.util.LinkedHashMap;
042import java.util.Locale;
043import java.util.Map;
044
045/**
046 * Contains a set of attribute definitions for the sitemap attribute editor.
047 */
048public class CmsSitemapAttributeEditorConfiguration {
049
050    /** A configuration with no entries. */
051    public static final CmsSitemapAttributeEditorConfiguration EMPTY = new CmsSitemapAttributeEditorConfiguration(
052        new HashMap<>());
053
054    /** The actual attribute definitions. */
055    private Map<String, CmsXmlContentProperty> m_attributeDefinitions;
056
057    /**
058     * Creates a new instance.
059     *
060     * @param attributeDefinitions the sitemap attribute definitions
061     */
062    public CmsSitemapAttributeEditorConfiguration(Map<String, CmsXmlContentProperty> attributeDefinitions) {
063
064        super();
065        m_attributeDefinitions = Collections.unmodifiableMap(new LinkedHashMap<>(attributeDefinitions));
066    }
067
068    /**
069     * Reads the attribute definitions from an XML content.
070     *
071     * @param cms the CmsObject  to use
072     * @param res the resource from which to read the attribute definitions
073     * @return the sitemap attribute editor configuration which was read from the file
074     * @throws CmsException if something goes wrong
075     */
076    public static CmsSitemapAttributeEditorConfiguration read(CmsObject cms, CmsResource res) throws CmsException {
077
078        Map<String, CmsXmlContentProperty> resultMap = new LinkedHashMap<>();
079        CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, cms.readFile(res));
080        CmsXmlContentRootLocation root = new CmsXmlContentRootLocation(content, Locale.ENGLISH);
081        for (I_CmsXmlContentValueLocation loc : root.getSubValues("Setting")) {
082            CmsXmlContentProperty propDef = CmsConfigurationReader.parseProperty(cms, loc).getPropertyData();
083            resultMap.put(propDef.getName(), propDef);
084        }
085        return new CmsSitemapAttributeEditorConfiguration(resultMap);
086    }
087
088    /**
089     * Gets the attribute definitions.
090     *
091     * @return the attribute definitions
092     */
093    public Map<String, CmsXmlContentProperty> getAttributeDefinitions() {
094
095        return m_attributeDefinitions;
096    }
097
098}