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.staticexport; 029 030import org.opencms.db.CmsPublishedResource; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsResource; 033import org.opencms.file.CmsResourceFilter; 034import org.opencms.main.CmsException; 035 036import java.util.ArrayList; 037import java.util.Collections; 038import java.util.HashSet; 039import java.util.Iterator; 040import java.util.List; 041import java.util.Set; 042import java.util.regex.Pattern; 043 044/** 045 * Help class for storing of export-rules.<p> 046 * 047 * @since 6.0.0 048 */ 049public class CmsStaticExportExportRule { 050 051 /** Description of the rule. */ 052 private String m_description; 053 054 /** configured Rfs export path. */ 055 private List<String> m_exportResources; 056 057 /** List of regular expresions to determine if a relevant resource has been modified. */ 058 private List<Pattern> m_modifiedResources; 059 060 /** Name of rule. */ 061 private String m_name; 062 063 /** 064 * Default constructor.<p> 065 * 066 * @param name the name of the rule 067 * @param description the description for the rule 068 */ 069 public CmsStaticExportExportRule(String name, String description) { 070 071 m_name = name; 072 m_description = description; 073 m_exportResources = new ArrayList<String>(); 074 m_modifiedResources = new ArrayList<Pattern>(); 075 } 076 077 /** 078 * Full Constructor.<p> 079 * 080 * @param name the name of the rule 081 * @param description the description of the rule 082 * @param modifiedResources a list of patterns to identify modified resources 083 * @param exportResourcePatterns a list of strings to export resources 084 */ 085 public CmsStaticExportExportRule( 086 String name, 087 String description, 088 List<Pattern> modifiedResources, 089 List<String> exportResourcePatterns) { 090 091 this(name, description); 092 m_modifiedResources.addAll(modifiedResources); 093 m_exportResources.addAll(exportResourcePatterns); 094 } 095 096 /** 097 * Adds a export Resource expression.<p> 098 * 099 * @param exportResource the export Resource expression to add 100 */ 101 public void addExportResourcePattern(String exportResource) { 102 103 m_exportResources.add(exportResource); 104 } 105 106 /** 107 * Adds a modified Resource regular expression.<p> 108 * 109 * @param modifiedRegex the modified Resource regular expression to add 110 */ 111 public void addModifiedResource(String modifiedRegex) { 112 113 m_modifiedResources.add(Pattern.compile(modifiedRegex)); 114 } 115 116 /** 117 * Returns the description.<p> 118 * 119 * @return the description 120 */ 121 public String getDescription() { 122 123 return m_description; 124 } 125 126 /** 127 * Returns the export Resources list.<p> 128 * 129 * @return the export Resources list 130 */ 131 public List<String> getExportResourcePatterns() { 132 133 return Collections.unmodifiableList(m_exportResources); 134 } 135 136 /** 137 * Returns a set of <code>{@link CmsPublishedResource}</code> objects containing all resources specified by the 138 * <code><export-resources></code> node of this rule.<p> 139 * 140 * @param cms the current OpenCms context 141 * 142 * @return a set of matching resources 143 * 144 * @throws CmsException if something goes wrong 145 */ 146 public Set<CmsPublishedResource> getExportResources(CmsObject cms) throws CmsException { 147 148 Set<CmsPublishedResource> resources = new HashSet<CmsPublishedResource>(128); 149 Iterator<String> itExpRes = m_exportResources.iterator(); 150 while (itExpRes.hasNext()) { 151 String exportRes = itExpRes.next(); 152 // read all from the configured node path, exclude resources flagged as internal 153 if (cms.existsResource(exportRes)) { 154 // first add the resource itself 155 CmsResource res = cms.readResource(exportRes); 156 resources.add(new CmsPublishedResource(res)); 157 if (res.isFolder()) { 158 // if the resource is a folder add also all sub-resources 159 List<CmsResource> vfsResources = cms.readResources( 160 exportRes, 161 CmsResourceFilter.ALL.addExcludeFlags(CmsResource.FLAG_INTERNAL)); 162 // loop through the list and create the list of CmsPublishedResources 163 Iterator<CmsResource> itRes = vfsResources.iterator(); 164 while (itRes.hasNext()) { 165 CmsResource vfsResource = itRes.next(); 166 CmsPublishedResource resource = new CmsPublishedResource(vfsResource); 167 resources.add(resource); 168 } 169 } 170 } 171 } 172 return resources; 173 } 174 175 /** 176 * Returns the modified Resources list as list of <code>{@link Pattern}</code>.<p> 177 * 178 * @return the modified Resources list as list of <code>{@link Pattern}</code> 179 */ 180 public List<Pattern> getModifiedResources() { 181 182 return Collections.unmodifiableList(m_modifiedResources); 183 } 184 185 /** 186 * Returns the name.<p> 187 * 188 * @return the name 189 */ 190 public String getName() { 191 192 return m_name; 193 } 194 195 /** 196 * Returns a set of <code>{@link CmsPublishedResource}</code> objects specified by the 197 * <code><export-resources></code> node of this rule, if the publishedResource 198 * matches a modified Resource regular expression.<p> 199 * 200 * @param cms the cms context 201 * @param publishedResource a published resource to test 202 * 203 * @return a set of matching resources, or <code>null</code> if resource does not match 204 * 205 * @throws CmsException if something goes wrong 206 */ 207 public Set<CmsPublishedResource> getRelatedResources(CmsObject cms, CmsPublishedResource publishedResource) 208 throws CmsException { 209 210 if (match(publishedResource.getRootPath())) { 211 return getExportResources(cms); 212 } 213 return null; 214 } 215 216 /** 217 * Checks if a vfsName matches the given modified resource patterns.<p> 218 * 219 * @param vfsName the vfs name of a resource to check 220 * @return true if the name matches one of the given modified resource patterns 221 */ 222 public boolean match(String vfsName) { 223 224 for (int j = 0; j < m_modifiedResources.size(); j++) { 225 Pattern pattern = m_modifiedResources.get(j); 226 if (pattern.matcher(vfsName).matches()) { 227 return true; 228 } 229 } 230 return false; 231 } 232 233 /** 234 * @see java.lang.Object#toString() 235 */ 236 @Override 237 public String toString() { 238 239 StringBuffer ret = new StringBuffer(getClass().getName()); 240 ret.append(":["); 241 ret.append("name: ").append(m_name).append("; "); 242 ret.append("description: ").append(m_description).append("; "); 243 ret.append("modified patterns: ").append(m_modifiedResources).append("; "); 244 ret.append("export resources: ").append(m_exportResources).append("; "); 245 return ret.append("]").toString(); 246 } 247}