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.i18n.CmsLocaleManager;
031import org.opencms.main.CmsIllegalArgumentException;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsStringUtil;
034
035import java.util.ArrayList;
036import java.util.Collections;
037import java.util.List;
038import java.util.regex.Pattern;
039
040/**
041 * Help class for storing of rfs-rules.<p>
042 *
043 * @since 6.0.0
044 */
045public class CmsStaticExportRfsRule {
046
047    /** Name for the default work path. */
048    public static final Integer EXPORT_DEFAULT_BACKUPS = new Integer(0);
049
050    /** Description of the rule. */
051    private String m_description;
052
053    /** Number of export backup folders. */
054    private Integer m_exportBackups;
055
056    /** Rfs export path. */
057    private String m_exportPath;
058
059    /** configured Rfs export path. */
060    private final String m_exportPathConfigured;
061
062    /** Rfs export work path. */
063    private String m_exportWorkPath;
064
065    /** configured Rfs export work path. */
066    private final String m_exportWorkPathConfigured;
067
068    /** Name of rule. */
069    private String m_name;
070
071    /** List of regular expressions to determine related system resources. */
072    private List<Pattern> m_relatedSystemResources;
073
074    /** configured Url prefix. */
075    private final String m_rfsPreConfigured;
076
077    /** Url prefix pattern. */
078    private String m_rfsPrefix;
079
080    /** Source regular expression. */
081    private final Pattern m_source;
082
083    /** Relative links value. */
084    private Boolean m_useRelativeLinks;
085
086    /**
087     * Default constructor.<p>
088     *
089     * @param name the name of the rule
090     * @param description the description for the rule
091     * @param source the source regular expression
092     * @param rfsPrefix the url prefix
093     * @param exportPath the rfs export path
094     * @param exportWorkPath the rfs export work path
095     * @param exportBackups the number of backups
096     * @param useRelativeLinks Relative links value
097     */
098    public CmsStaticExportRfsRule(
099        String name,
100        String description,
101        String source,
102        String rfsPrefix,
103        String exportPath,
104        String exportWorkPath,
105        Integer exportBackups,
106        Boolean useRelativeLinks) {
107
108        m_name = name;
109        m_description = description;
110        m_source = Pattern.compile(source);
111        m_rfsPreConfigured = rfsPrefix;
112        m_exportPathConfigured = exportPath;
113        m_exportWorkPathConfigured = exportWorkPath;
114        m_exportBackups = exportBackups;
115        m_useRelativeLinks = useRelativeLinks;
116        m_relatedSystemResources = new ArrayList<Pattern>();
117    }
118
119    /**
120     * Full constructor.<p>
121     *
122     * @param name the name of the rule
123     * @param description the description for the rule
124     * @param source the source regular expression
125     * @param rfsPrefix the url prefix
126     * @param exportPath the rfs export path
127     * @param exportWorkPath the rfs export work path
128     * @param exportBackups the number of backups
129     * @param useRelativeLinks Relative links value
130     * @param relatedSystemRes list of <code>{@link Pattern}</code>s
131     */
132    public CmsStaticExportRfsRule(
133        String name,
134        String description,
135        String source,
136        String rfsPrefix,
137        String exportPath,
138        String exportWorkPath,
139        Integer exportBackups,
140        Boolean useRelativeLinks,
141        List<Pattern> relatedSystemRes) {
142
143        this(name, description, source, rfsPrefix, exportPath, exportWorkPath, exportBackups, useRelativeLinks);
144        m_relatedSystemResources.addAll(relatedSystemRes);
145    }
146
147    /**
148     * Adds a regular expression of related system resources.<p>
149     *
150     * @param regex the regular expression to add
151     */
152    public void addRelatedSystemRes(String regex) {
153
154        m_relatedSystemResources.add(Pattern.compile(regex));
155    }
156
157    /**
158     * Returns the description.<p>
159     *
160     * @return the description
161     */
162    public String getDescription() {
163
164        return m_description;
165    }
166
167    /**
168     * Returns the number of backups.<p>
169     *
170     * @return the number of backups
171     */
172    public Integer getExportBackups() {
173
174        if (m_exportBackups != null) {
175            return m_exportBackups;
176        }
177        // if backups not configured set to default value
178        return EXPORT_DEFAULT_BACKUPS;
179    }
180
181    /**
182     * Returns the rfs export Path.<p>
183     *
184     * @return the rfs export Path
185     */
186    public String getExportPath() {
187
188        if (OpenCms.getStaticExportManager().isUseTempDir() && OpenCms.getStaticExportManager().isFullStaticExport()) {
189            return getExportWorkPath();
190        }
191        return m_exportPath;
192    }
193
194    /**
195     * Returns the configured rfs export Path with unsubstituted context values.<p>
196     *
197     * @return the configured rfs export Path
198     */
199    public String getExportPathConfigured() {
200
201        return m_exportPathConfigured;
202    }
203
204    /**
205     * Returns the rfs export Work Path.<p>
206     *
207     * @return the rfs export Work Path
208     */
209    public String getExportWorkPath() {
210
211        return m_exportWorkPath;
212    }
213
214    /**
215     * Returns the configured rfs export Work Path with unsubstituted context values.<p>
216     *
217     * @return the configured rfs export Work Path
218     */
219    public String getExportWorkPathConfigured() {
220
221        if (m_exportWorkPathConfigured != null) {
222            return m_exportWorkPathConfigured;
223        }
224
225        // if work path not configured set to default value
226        return CmsStaticExportManager.EXPORT_DEFAULT_WORKPATH
227            + OpenCms.getResourceManager().getFileTranslator().translateResource(m_name);
228    }
229
230    /**
231     * Returns the rfs name for the given locale, only used for multi-language export.<p>
232     *
233     * @param rfsName the original rfs name
234     * @param fileSeparator the file separator to use
235     *
236     * @return the rfs name for the given locale
237     */
238    public String getLocalizedRfsName(String rfsName, String fileSeparator) {
239
240        String locRfsName = null;
241
242        // this might be too simple
243        locRfsName = CmsStringUtil.substitute(
244            rfsName,
245            fileSeparator + CmsLocaleManager.getDefaultLocale().toString() + fileSeparator,
246            fileSeparator + getName() + fileSeparator);
247        return locRfsName;
248    }
249
250    /**
251     * Returns the name.<p>
252     *
253     * @return the name
254     */
255    public String getName() {
256
257        return m_name;
258    }
259
260    /**
261     * Returns the related system resources list as list of <code>{@link Pattern}</code>.<p>
262     *
263     * @return the related resources list as list of <code>{@link Pattern}</code>
264     */
265    public List<Pattern> getRelatedSystemResources() {
266
267        return Collections.unmodifiableList(m_relatedSystemResources);
268    }
269
270    /**
271     * Returns the url Prefix with macro replacement.<p>
272     *
273     * @return the url Prefix
274     */
275    public String getRfsPrefix() {
276
277        return m_rfsPrefix;
278    }
279
280    /**
281     * Returns the configured url Prefix with unsubstituted context values.<p>
282     *
283     * @return the configured url Prefix
284     */
285    public String getRfsPrefixConfigured() {
286
287        return m_rfsPreConfigured;
288    }
289
290    /**
291     * Returns the source regular expression pattern.<p>
292     *
293     * @return the source regular expression pattern
294     */
295    public Pattern getSource() {
296
297        return m_source;
298    }
299
300    /**
301     * Returns true if the links in the static export should be relative.<p>
302     *
303     * @return true if the links in the static export should be relative
304     */
305    public Boolean getUseRelativeLinks() {
306
307        return m_useRelativeLinks;
308    }
309
310    /**
311     * Checks if a vfsName matches the given related system resource patterns.<p>
312     *
313     * @param vfsName the vfs name of a resource to check
314     *
315     * @return true if the name matches one of the given related system resource patterns
316     */
317    public boolean match(String vfsName) {
318
319        for (int j = 0; j < m_relatedSystemResources.size(); j++) {
320            Pattern pattern = m_relatedSystemResources.get(j);
321            if (pattern.matcher(vfsName).matches()) {
322                return true;
323            }
324        }
325        return false;
326    }
327
328    /**
329     * Sets the rfs export Path after normalizing.<p>
330     *
331     * @param exportPath the rfs export Path to set
332     */
333    public void setExportPath(String exportPath) {
334
335        if (exportPath.equals(OpenCms.getSystemInfo().getWebApplicationRfsPath())) {
336            // not allowed because a full static export would delete the opencms directory
337            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INVALID_EXPORT_PATH_1, m_name));
338        }
339        m_exportPath = exportPath;
340    }
341
342    /**
343     * Sets the rfs export work Path after normalizing.<p>
344     *
345     * @param exportWorkPath the rfs export Work Path to set
346     */
347    public void setExportWorkPath(String exportWorkPath) {
348
349        if (exportWorkPath.equals(OpenCms.getSystemInfo().getWebApplicationRfsPath())) {
350            // not allowed because a full static export would delete the opencms directory
351            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INVALID_EXPORT_PATH_1, m_name));
352        }
353        m_exportWorkPath = exportWorkPath;
354    }
355
356    /**
357     * Sets the url Prefix after normalizing.<p>
358     *
359     * @param rfsPrefix the url Prefix to set
360     */
361    public void setRfsPrefix(String rfsPrefix) {
362
363        m_rfsPrefix = rfsPrefix;
364    }
365
366    /**
367     * @see java.lang.Object#toString()
368     */
369    @Override
370    public String toString() {
371
372        StringBuffer ret = new StringBuffer(getClass().getName());
373        ret.append(":[");
374        ret.append("name: ").append(m_name).append("; ");
375        ret.append("description: ").append(m_description).append("; ");
376        ret.append("source: ").append(m_source).append("; ");
377        ret.append("exportPath: ").append(m_exportPath).append("; ");
378        ret.append("rfsPrefix: ").append(m_rfsPrefix).append("; ");
379        ret.append("useRelativeLinks: ").append(m_useRelativeLinks).append("; ");
380        ret.append("relatedSystemRes: ").append(m_relatedSystemResources).append("; ");
381        return ret.append("]").toString();
382    }
383
384}