001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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 098 // do nothing 099 } 100 101 /** 102 * Parses a template mapper configuration from an XML document.<p> 103 * 104 * @param cms the current CMS context 105 * @param doc the XML document containing the configuration 106 * 107 * @throws CmsException if something goes wrong 108 */ 109 public CmsTemplateMapperConfiguration(CmsObject cms, Document doc) 110 throws CmsException { 111 112 cms = OpenCms.initCmsObject(cms); 113 cms.getRequestContext().setSiteRoot(""); 114 Element root = doc.getRootElement(); 115 String enabledStr = root.attributeValue(A_ENABLED); 116 boolean enabled = Boolean.parseBoolean(enabledStr); 117 118 if (enabled) { 119 for (Node node : root.selectNodes("//" + N_FORMATTER_CONFIG)) { 120 Element formatterElem = (Element)node; 121 String oldPath = formatterElem.attributeValue(A_OLD); 122 String newPath = formatterElem.attributeValue(A_NEW); 123 try { 124 CmsResource oldFormatter = cms.readResource(oldPath, CmsResourceFilter.IGNORE_EXPIRATION); 125 CmsResource newFormatter = cms.readResource(newPath, CmsResourceFilter.IGNORE_EXPIRATION); 126 m_formatterConfigMap.put(oldFormatter.getStructureId(), newFormatter.getStructureId()); 127 } catch (CmsException e) { 128 LOG.error(e.getLocalizedMessage(), e); 129 } 130 } 131 for (Node node : root.selectNodes("//" + N_FORMATTER_JSP)) { 132 Element formatterElem = (Element)node; 133 String oldPath = formatterElem.attributeValue(A_OLD); 134 String newPath = formatterElem.attributeValue(A_NEW); 135 try { 136 CmsResource oldFormatter = cms.readResource(oldPath, CmsResourceFilter.IGNORE_EXPIRATION); 137 CmsResource newFormatter = cms.readResource(newPath, CmsResourceFilter.IGNORE_EXPIRATION); 138 m_formatterJspMap.put(oldFormatter.getStructureId(), newFormatter.getStructureId()); 139 } catch (CmsException e) { 140 LOG.error(e.getLocalizedMessage(), e); 141 } 142 } 143 144 for (Node node : root.selectNodes("//" + N_ELEMENT_GROUP_TYPE)) { 145 Element groupElem = (Element)node; 146 String oldType = groupElem.attributeValue(A_OLD); 147 String newType = groupElem.attributeValue(A_NEW); 148 m_groupTypeMap.put(oldType, newType); 149 } 150 151 for (Node node : root.selectNodes("//" + N_PATH)) { 152 Element pathElem = (Element)node; 153 m_paths.add(pathElem.getText()); 154 } 155 } 156 } 157 158 /** 159 * Gets the mapped type for a given element group type, or null if there is no mapped type.<p> 160 * 161 * @param type the original element group type 162 * 163 * @return the mapped element group type 164 */ 165 public String getMappedElementGroupType(String type) { 166 167 return m_groupTypeMap.get(type); 168 169 } 170 171 /** 172 * Gets the mapped formatter configuration structure id string for another formatter configuration structure id string.<p> 173 * 174 * @param id the structure id of a formatter configuration as a string 175 * @return the mapped formatter configuration structure id of a string 176 */ 177 public String getMappedFormatterConfiguration(String id) { 178 179 CmsUUID resultId = m_formatterConfigMap.get(new CmsUUID(id)); 180 if (resultId == null) { 181 return null; 182 } 183 return resultId.toString(); 184 } 185 186 /** 187 * Gets the mapped formatter JSP structure id for another formatter JSP structure id.<p> 188 * 189 * @param formatterId the input formatter JSP structure id 190 * @return the mapped formatter JSP structure id 191 */ 192 public CmsUUID getMappedFormatterJspId(CmsUUID formatterId) { 193 194 return m_formatterJspMap.get(formatterId); 195 } 196 197 /** 198 * Checks if the mapping is enabled for the given root path.<p> 199 * 200 * @param rootPath a VFS root path 201 * @return true if the configuration is enabled for the given root path 202 */ 203 public boolean isEnabledForPath(String rootPath) { 204 205 for (String path : m_paths) { 206 if (CmsStringUtil.isPrefixPath(path, rootPath)) { 207 return true; 208 } 209 } 210 return false; 211 } 212 213}