1 31 32 package org.opencms.frontend.templateone.modules; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProperty; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsResourceFilter; 39 import org.opencms.jsp.util.CmsTemplateContentListItem; 40 import org.opencms.main.CmsException; 41 import org.opencms.main.OpenCms; 42 import org.opencms.util.CmsMacroResolver; 43 import org.opencms.util.CmsStringUtil; 44 import org.opencms.xml.CmsXmlException; 45 import org.opencms.xml.content.CmsDefaultXmlContentHandler; 46 import org.opencms.xml.content.CmsXmlContent; 47 import org.opencms.xml.content.Messages; 48 import org.opencms.xml.types.I_CmsXmlContentValue; 49 50 import java.util.ArrayList ; 51 import java.util.List ; 52 import java.util.Locale ; 53 54 66 public class CmsLayoutXmlContentHandler extends CmsDefaultXmlContentHandler { 67 68 69 public static final String CONFIG_RESTYPE_NAME = "layout"; 70 71 72 public static final String ELEMENT_COLLECTOR = "Collector"; 73 74 75 public static final String ELEMENT_COUNT = "Count"; 76 77 78 public static final String ELEMENT_LAYOUT = "Layout"; 79 80 81 public static final String ELEMENT_RESOURCETYPE = "Type"; 82 83 84 public static final String ELEMENT_VFSFOLDER = "Folder"; 85 86 87 public static final String PROPERTY_LAYOUT_COLUMN = "layout.column"; 88 89 92 public CmsLayoutXmlContentHandler() { 93 94 super(); 95 } 96 97 102 public CmsFile prepareForWrite(CmsObject cms, CmsXmlContent content, CmsFile file) throws CmsException { 103 104 super.prepareForWrite(cms, content, file); 105 resolveLayoutMappings(cms, content); 106 removeEmptyLayoutMappings(cms, content); 107 return file; 108 } 109 110 118 protected void removeEmptyLayoutMappings(CmsObject cms, CmsXmlContent content) throws CmsException { 119 120 super.removeEmptyMappings(cms, content); 121 122 String rootPath = null; 123 List siblings = null; 124 125 if (rootPath == null) { 127 rootPath = content.getFile().getRootPath(); 128 } 129 130 try { 131 cms.getRequestContext().saveSiteRoot(); 133 cms.getRequestContext().setSiteRoot("/"); 134 135 if (siblings == null) { 137 siblings = cms.readSiblings(rootPath, CmsResourceFilter.IGNORE_EXPIRATION); 138 } 139 140 for (int i = 0; i < siblings.size(); i++) { 141 142 String filename = ((CmsResource)siblings.get(i)).getRootPath(); 144 Locale locale = OpenCms.getLocaleManager().getDefaultLocale(cms, filename); 145 146 if (!content.hasLocale(locale)) { 147 continue; 149 } 150 151 List nestedItems = content.getValues(ELEMENT_LAYOUT, locale); 152 153 int deleteIndex = nestedItems.size() + 1; 155 156 CmsMacroResolver resolver = CmsMacroResolver.newInstance(); 158 resolver.addMacro(CmsTemplateContentListItem.MACRO_LISTINDEX, "" + deleteIndex); 159 160 List properties = new ArrayList (4); 161 162 CmsProperty p = new CmsProperty( 164 resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_TYPE), 165 CmsProperty.DELETE_VALUE, 166 null); 167 properties.add(p); 168 169 p = new CmsProperty( 171 resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_FOLDER), 172 CmsProperty.DELETE_VALUE, 173 null); 174 properties.add(p); 175 176 p = new CmsProperty( 178 resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_COUNT), 179 CmsProperty.DELETE_VALUE, 180 null); 181 properties.add(p); 182 183 p = new CmsProperty( 185 resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_COLLECTOR), 186 CmsProperty.DELETE_VALUE, 187 null); 188 properties.add(p); 189 190 cms.writePropertyObjects(filename, properties); 192 } 193 194 } finally { 195 cms.getRequestContext().restoreSiteRoot(); 197 } 198 } 199 200 209 protected void resolveLayoutMappings(CmsObject cms, CmsXmlContent content) throws CmsException { 210 211 CmsFile file = content.getFile(); 213 if (file == null) { 214 throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_RESOLVE_FILE_NOT_FOUND_0)); 215 } 216 217 String rootPath = content.getFile().getRootPath(); 219 String siteRoot = cms.getRequestContext().getSiteRoot(); 220 221 try { 222 cms.getRequestContext().saveSiteRoot(); 224 cms.getRequestContext().setSiteRoot("/"); 225 226 List siblings = cms.readSiblings(rootPath, CmsResourceFilter.IGNORE_EXPIRATION); 228 229 233 for (int i = (siblings.size() - 1); i >= 0; i--) { 234 String filename = ((CmsResource)siblings.get(i)).getRootPath(); 236 Locale locale = OpenCms.getLocaleManager().getDefaultLocale(cms, filename); 237 238 if (!content.hasLocale(locale)) { 239 continue; 241 } 242 243 List nestedItems = content.getValues(ELEMENT_LAYOUT, locale); 245 List properties = new ArrayList (nestedItems.size() * 4); 246 for (int k = 0; k < nestedItems.size(); k++) { 247 I_CmsXmlContentValue layoutConfig = (I_CmsXmlContentValue)nestedItems.get(k); 248 String layoutConfigPath = layoutConfig.getPath() + "/"; 249 250 CmsMacroResolver resolver = CmsMacroResolver.newInstance(); 252 resolver.addMacro(CmsTemplateContentListItem.MACRO_LISTINDEX, "" + (layoutConfig.getIndex() + 1)); 253 254 String resType = content.getStringValue(cms, layoutConfigPath + ELEMENT_RESOURCETYPE, locale); 256 CmsProperty p = new CmsProperty( 257 resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_TYPE), 258 resType, 259 null); 260 properties.add(p); 261 262 String folder = content.getStringValue(cms, layoutConfigPath + ELEMENT_VFSFOLDER, locale); 264 if (CmsStringUtil.isNotEmpty(folder)) { 265 if (folder.length() > 1 && folder.startsWith(siteRoot)) { 267 folder = folder.substring(siteRoot.length()); 268 } 269 } else { 270 folder = CmsProperty.DELETE_VALUE; 272 } 273 p = new CmsProperty( 274 resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_FOLDER), 275 folder, 276 null); 277 properties.add(p); 278 279 String count = content.getStringValue(cms, layoutConfigPath + ELEMENT_COUNT, locale); 281 p = new CmsProperty( 282 resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_COUNT), 283 count, 284 null); 285 properties.add(p); 286 287 String collector = content.getStringValue(cms, layoutConfigPath + ELEMENT_COLLECTOR, locale); 289 p = new CmsProperty( 290 resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_COLLECTOR), 291 collector, 292 null); 293 properties.add(p); 294 } 295 296 cms.writePropertyObjects(filename, properties); 298 } 299 300 } finally { 301 cms.getRequestContext().restoreSiteRoot(); 303 } 304 } 305 306 } | Popular Tags |