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.templatemapper;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.util.CmsUUID;
038
039import java.util.HashMap;
040import java.util.List;
041import java.util.Map;
042
043import org.apache.commons.compress.utils.Lists;
044import org.apache.commons.logging.Log;
045
046import org.dom4j.Document;
047import org.dom4j.Element;
048import org.dom4j.Node;
049
050/**
051 * Configuration for the template mapper.<p>
052 */
053public class CmsTemplateMapperConfiguration {
054
055    /** XML attribute name. */
056    public static final String A_ENABLED = "enabled";
057
058    /** XML attribute name. */
059    public static final String A_NEW = "new";
060
061    /** XML attribute name. */
062    public static final String A_OLD = "old";
063
064    /** Empty configuratin. */
065    public static final CmsTemplateMapperConfiguration EMPTY_CONFIG = new CmsTemplateMapperConfiguration();
066
067    /** XML element name. */
068    public static final String N_FORMATTER_CONFIG = "formatter-config";
069    /** XML element name. */
070    public static final String N_FORMATTER_JSP = "formatter-jsp";
071
072    /** XML element name. */
073    public static final String N_ELEMENT_GROUP_TYPE = "element-group-type";
074
075    /** XML element name. */
076    public static final String N_PATH = "path";
077
078    /** The logger instance for this class. */
079    private static final Log LOG = CmsLog.getLog(CmsTemplateMapperConfiguration.class);
080
081    /** The formatter configuration mapping (structure id to structure id). */
082    private Map<CmsUUID, CmsUUID> m_formatterConfigMap = new HashMap<>();
083
084    /** The formatter JSP mapping (structure id to structure id). */
085    private Map<CmsUUID, CmsUUID> m_formatterJspMap = new HashMap<>();
086
087    /** Mapping for element group types. */
088    private Map<String, String> m_groupTypeMap = new HashMap<>();
089
090    /** List of root paths for which the configuration is valid. */
091    private List<String> m_paths = Lists.newArrayList();
092
093    /**
094     * Creates an empty mapper configuration which is not active for any path.<p>
095     */
096    public CmsTemplateMapperConfiguration() {
097        // do nothing
098    }
099
100    /**
101     * Parses a template mapper configuration from an XML document.<p>
102     *
103     * @param cms the current CMS context
104     * @param doc the XML document containing the configuration
105     *
106     * @throws CmsException if something goes wrong
107     */
108    public CmsTemplateMapperConfiguration(CmsObject cms, Document doc)
109    throws CmsException {
110
111        cms = OpenCms.initCmsObject(cms);
112        cms.getRequestContext().setSiteRoot("");
113        Element root = doc.getRootElement();
114        String enabledStr = root.attributeValue(A_ENABLED);
115        boolean enabled = Boolean.parseBoolean(enabledStr);
116
117        if (enabled) {
118            for (Node node : root.selectNodes("//" + N_FORMATTER_CONFIG)) {
119                Element formatterElem = (Element)node;
120                String oldPath = formatterElem.attributeValue(A_OLD);
121                String newPath = formatterElem.attributeValue(A_NEW);
122                try {
123                    CmsResource oldFormatter = cms.readResource(oldPath, CmsResourceFilter.IGNORE_EXPIRATION);
124                    CmsResource newFormatter = cms.readResource(newPath, CmsResourceFilter.IGNORE_EXPIRATION);
125                    m_formatterConfigMap.put(oldFormatter.getStructureId(), newFormatter.getStructureId());
126                } catch (CmsException e) {
127                    LOG.error(e.getLocalizedMessage(), e);
128                }
129            }
130            for (Node node : root.selectNodes("//" + N_FORMATTER_JSP)) {
131                Element formatterElem = (Element)node;
132                String oldPath = formatterElem.attributeValue(A_OLD);
133                String newPath = formatterElem.attributeValue(A_NEW);
134                try {
135                    CmsResource oldFormatter = cms.readResource(oldPath, CmsResourceFilter.IGNORE_EXPIRATION);
136                    CmsResource newFormatter = cms.readResource(newPath, CmsResourceFilter.IGNORE_EXPIRATION);
137                    m_formatterJspMap.put(oldFormatter.getStructureId(), newFormatter.getStructureId());
138                } catch (CmsException e) {
139                    LOG.error(e.getLocalizedMessage(), e);
140                }
141            }
142
143            for (Node node : root.selectNodes("//" + N_ELEMENT_GROUP_TYPE)) {
144                Element groupElem = (Element)node;
145                String oldType = groupElem.attributeValue(A_OLD);
146                String newType = groupElem.attributeValue(A_NEW);
147                m_groupTypeMap.put(oldType, newType);
148            }
149
150            for (Node node : root.selectNodes("//" + N_PATH)) {
151                Element pathElem = (Element)node;
152                m_paths.add(pathElem.getText());
153            }
154        }
155    }
156
157    /**
158     * Gets the mapped type for a given element group type, or null if there is no mapped type.<p>
159     *
160     * @param type the original element group type
161     *
162     * @return the mapped element group type
163     */
164    public String getMappedElementGroupType(String type) {
165
166        return m_groupTypeMap.get(type);
167
168    }
169
170    /**
171     * Gets the mapped formatter configuration structure id string for another formatter configuration structure id string.<p>
172     *
173     * @param id the structure id of a formatter configuration as a string
174     * @return the mapped formatter configuration structure id of a string
175     */
176    public String getMappedFormatterConfiguration(String id) {
177
178        CmsUUID resultId = m_formatterConfigMap.get(new CmsUUID(id));
179        if (resultId == null) {
180            return null;
181        }
182        return resultId.toString();
183    }
184
185    /**
186     * Gets the mapped formatter JSP structure id for another formatter JSP structure id.<p>
187     *
188     * @param formatterId the input formatter JSP structure id
189     * @return the mapped formatter JSP structure id
190     */
191    public CmsUUID getMappedFormatterJspId(CmsUUID formatterId) {
192
193        return m_formatterJspMap.get(formatterId);
194    }
195
196    /**
197     * Checks if the mapping is enabled for the given root path.<p>
198     *
199     * @param rootPath a VFS root path
200     * @return true if the configuration is enabled for the given root path
201     */
202    public boolean isEnabledForPath(String rootPath) {
203
204        for (String path : m_paths) {
205            if (CmsStringUtil.isPrefixPath(path, rootPath)) {
206                return true;
207            }
208        }
209        return false;
210    }
211
212}