KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > staticexport > CmsStaticExportExportRule


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/staticexport/CmsStaticExportExportRule.java,v $
3  * Date : $Date: 2006/09/21 09:34:48 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2005 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.staticexport;
33
34 import org.opencms.db.CmsPublishedResource;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.main.CmsException;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.HashSet JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Set JavaDoc;
46 import java.util.regex.Pattern JavaDoc;
47
48 /**
49  * Help class for storing of export-rules.<p>
50  *
51  * @author Michael Moossen
52  * @version $Revision: 1.2 $
53  * @since 6.0.0
54  */

55 public class CmsStaticExportExportRule {
56
57     /** Description of the rule. */
58     private String JavaDoc m_description;
59
60     /** configured Rfs export path. */
61     private List JavaDoc m_exportResources;
62
63     /** List of regular expresions to determine if a relevant resource has been modified. */
64     private List JavaDoc m_modifiedResources;
65
66     /** Name of rule. */
67     private String JavaDoc m_name;
68
69     /**
70      * Default constructor.<p>
71      *
72      * @param name the name of the rule
73      * @param description the description for the rule
74      */

75     public CmsStaticExportExportRule(String JavaDoc name, String JavaDoc description) {
76
77         m_name = name;
78         m_description = description;
79         m_exportResources = new ArrayList JavaDoc();
80         m_modifiedResources = new ArrayList JavaDoc();
81     }
82
83     /**
84      * Full Constructor.<p>
85      *
86      * @param name the name of the rule
87      * @param description the description of the rule
88      * @param modifiedResources a list of patterns to identify modified resources
89      * @param exportResourcePatterns a list of strings to export resources
90      */

91     public CmsStaticExportExportRule(
92         String JavaDoc name,
93         String JavaDoc description,
94         List JavaDoc modifiedResources,
95         List JavaDoc exportResourcePatterns) {
96
97         this(name, description);
98         m_modifiedResources.addAll(modifiedResources);
99         m_exportResources.addAll(exportResourcePatterns);
100     }
101
102     /**
103      * Adds a export Resource expression.<p>
104      *
105      * @param exportResource the export Resource expression to add
106      */

107     public void addExportResourcePattern(String JavaDoc exportResource) {
108
109         m_exportResources.add(exportResource);
110     }
111
112     /**
113      * Adds a modified Resource regular expression.<p>
114      *
115      * @param modifiedRegex the modified Resource regular expression to add
116      */

117     public void addModifiedResource(String JavaDoc modifiedRegex) {
118
119         m_modifiedResources.add(Pattern.compile(modifiedRegex));
120     }
121
122     /**
123      * Returns the description.<p>
124      *
125      * @return the description
126      */

127     public String JavaDoc getDescription() {
128
129         return m_description;
130     }
131
132     /**
133      * Returns the export Resources list.<p>
134      *
135      * @return the export Resources list
136      */

137     public List JavaDoc getExportResourcePatterns() {
138
139         return Collections.unmodifiableList(m_exportResources);
140     }
141
142     /**
143      * Returns a set of <code>{@link CmsPublishedResource}</code> containing all resources specified by the
144      * <code>&lt;export-resources&gt;</code> node of this rule.<p>
145      *
146      * @param cms the cms context
147      *
148      * @return a set of matching resources
149      *
150      * @throws CmsException if something goes wrong
151      */

152     public Set JavaDoc getExportResources(CmsObject cms) throws CmsException {
153
154         Set JavaDoc resources = new HashSet JavaDoc(128);
155         Iterator JavaDoc itExpRes = m_exportResources.iterator();
156         while (itExpRes.hasNext()) {
157             String JavaDoc exportRes = (String JavaDoc)itExpRes.next();
158             // read all from the configured node path, exclude resources flagged as internal
159
List JavaDoc 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 JavaDoc itRes = vfsResources.iterator();
164             while (itRes.hasNext()) {
165                 CmsResource vfsResource = (CmsResource)itRes.next();
166                 CmsPublishedResource resource = new CmsPublishedResource(vfsResource);
167                 resources.add(resource);
168             }
169         }
170         return resources;
171     }
172
173     /**
174      * Returns the modified Resources list as list of <code>{@link Pattern}</code>.<p>
175      *
176      * @return the modified Resources list as list of <code>{@link Pattern}</code>
177      */

178     public List JavaDoc getModifiedResources() {
179
180         return Collections.unmodifiableList(m_modifiedResources);
181     }
182
183     /**
184      * Returns the name.<p>
185      *
186      * @return the name
187      */

188     public String JavaDoc getName() {
189
190         return m_name;
191     }
192
193     /**
194      * Returns a set of <code>{@link CmsPublishedResource}</code> objects specified by the
195      * <code>&lt;export-resources&gt;</code> node of this rule, if the publishedResource
196      * matches a modified Resource regular expression.<p>
197      *
198      * @param cms the cms context
199      * @param publishedResource a published resource to test
200      *
201      * @return a set of matching resources, or <code>null</code> if resource does not match
202      *
203      * @throws CmsException if something goes wrong
204      */

205     public Set JavaDoc getRelatedResources(CmsObject cms, CmsPublishedResource publishedResource) throws CmsException {
206
207         if (match(publishedResource.getRootPath())) {
208             return getExportResources(cms);
209         }
210         return null;
211     }
212
213     /**
214      * Checks if a vfsName matches the given modified resource patterns.<p>
215      *
216      * @param vfsName the vfs name of a resource to check
217      * @return true if the name matches one of the given modified resource patterns
218      */

219     public boolean match(String JavaDoc vfsName) {
220
221         for (int j = 0; j < m_modifiedResources.size(); j++) {
222             Pattern JavaDoc pattern = (Pattern JavaDoc)m_modifiedResources.get(j);
223             if (pattern.matcher(vfsName).matches()) {
224                 return true;
225             }
226         }
227         return false;
228     }
229
230     /**
231      * @see java.lang.Object#toString()
232      */

233     public String JavaDoc toString() {
234
235         StringBuffer JavaDoc ret = new StringBuffer JavaDoc(getClass().getName());
236         ret.append(":[");
237         ret.append("name: ").append(m_name).append("; ");
238         ret.append("description: ").append(m_description).append("; ");
239         ret.append("modified patterns: ").append(m_modifiedResources).append("; ");
240         ret.append("export resources: ").append(m_exportResources).append("; ");
241         return ret.append("]").toString();
242     }
243 }
244
Popular Tags