KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > content > check > CmsContentCheckProperty


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/check/CmsContentCheckProperty.java,v $
3  * Date : $Date: 2006/03/29 16:04:15 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2005 Alkacon Software GmbH (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 GmbH, 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.workplace.tools.content.check;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.workplace.tools.I_CmsToolHandler;
41 import org.opencms.xml.content.CmsXmlContent;
42 import org.opencms.xml.content.CmsXmlContentFactory;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Locale JavaDoc;
47 import java.util.regex.Pattern JavaDoc;
48
49 import org.apache.commons.logging.Log;
50
51 /**
52  * This implementation of the I_CmsContentCheck interface implments a check for
53  * resource properties.<p>
54  *
55  * The following items can be configured and checked:
56  * <ul>
57  * <li>Property not set</li>
58  * <li>Property value contains filename</li>
59  * <li>Property value is shorter than a minimum size</li>
60  * <li>Property value contains a given value (with RegEx)</li>
61  * <li>Property value does not contain a given value (with RegEx)</li>
62  * </ul>
63  *
64  * @author Michael Emmerich
65  *
66  * @version $Revision: 1.3 $
67  *
68  * @since 6.1.2
69  */

70 public class CmsContentCheckProperty extends A_CmsContentCheck implements I_CmsContentCheck, I_CmsToolHandler {
71
72     /** Path to the configuration file. */
73     private static final String JavaDoc CONFIGURATION = CmsContentCheck.VFS_PATH_PLUGIN_FOLDER
74         + "propertycheck/configuration.xml";
75
76     /** Name of the dialog parameter. */
77     private static final String JavaDoc DIALOG_PARAMETER = "property";
78
79     /** Path to the configuration icon. */
80     private static final String JavaDoc ICONPATH = "tools/contenttools/icons/big/contentcheck_property_configuration.png";
81
82     /** The log object for this class. */
83     private static final Log LOG = CmsLog.getLog(CmsContentCheckProperty.class);
84
85     /** Name of this content check. */
86     private static final String JavaDoc NAME = "Property Check";
87
88     /** The xpath for the empty configuration. */
89     private static final String JavaDoc XPATH_EMPTY = "empty";
90
91     /** The xpath for the error configuration. */
92     private static final String JavaDoc XPATH_ERROR = "error";
93
94     /** The xpath for the filename configuration. */
95     private static final String JavaDoc XPATH_FILENAME = "filename";
96
97     /** The xpath for the length configuration. */
98     private static final String JavaDoc XPATH_LENGTH = "length";
99
100     /** The xpath for the propertyname configuration. */
101     private static final String JavaDoc XPATH_PROPERTYNAME = "propertyname";
102
103     /** The xpath for the type configuration. */
104     private static final String JavaDoc XPATH_TYPE = "type";
105
106     /** The xpath for the value configuration. */
107     private static final String JavaDoc XPATH_VALUE = "value";
108
109     /** The xpath for the warning configuration. */
110     private static final String JavaDoc XPATH_WARNUING = "warning";
111
112     /** The active flag, signaling if this content check is active. */
113     private boolean m_active = true;
114
115     /** The CmsObject. */
116     private CmsObject m_cms;
117
118     /** List of all configured error checks. */
119     private List JavaDoc m_configuredErrorChecks = null;
120
121     /** List of all configured warning checks. */
122     private List JavaDoc m_configuredWarningChecks = null;
123
124     /** Locale to be used to extract xml content. */
125     private Locale JavaDoc m_locale;
126
127     /**
128      *
129      * @see org.opencms.workplace.tools.content.check.I_CmsContentCheck#executeContentCheck(org.opencms.file.CmsObject, org.opencms.workplace.tools.content.check.CmsContentCheckResource)
130      */

131     public CmsContentCheckResource executeContentCheck(CmsObject cms, CmsContentCheckResource testResource)
132     throws CmsException {
133
134         getConfiguration();
135         if (LOG.isDebugEnabled()) {
136             LOG.debug(Messages.get().getBundle().key(
137                 Messages.LOG_DEBUG_PROPERTY_CONFIGURED_ERRORS_2,
138                 new Object JavaDoc[] {testResource.getResourceName(), m_configuredErrorChecks}));
139         }
140         // check for errors
141
List JavaDoc errors = processProperties(m_configuredErrorChecks, testResource);
142         if (errors.size() > 0) {
143             testResource.addErrors(errors);
144         }
145         if (LOG.isDebugEnabled()) {
146             LOG.debug(Messages.get().getBundle().key(
147                 Messages.LOG_DEBUG_PROPERTY_CONFIGURED_WARNINGS_2,
148                 testResource.getResourceName(),
149                 m_configuredErrorChecks));
150         }
151         // check for warnings
152
List JavaDoc warnings = processProperties(m_configuredWarningChecks, testResource);
153         if (warnings.size() > 0) {
154             testResource.addWarnings(warnings);
155         }
156         return testResource;
157     }
158
159     /**
160      *
161      * @see org.opencms.workplace.tools.content.check.I_CmsContentCheck#getDialogParameterName()
162      */

163     public String JavaDoc getDialogParameterName() {
164
165         return DIALOG_PARAMETER;
166     }
167
168     /**
169      * @see org.opencms.workplace.tools.I_CmsToolHandler#getHelpText()
170      */

171     public String JavaDoc getHelpText() {
172
173         return Messages.get().getBundle().key(Messages.GUI_CHECKCONTENT_CONFIGURATION_PROPERTY_HELP_0);
174     }
175
176     /**
177      * @see org.opencms.workplace.tools.I_CmsToolHandler#getIconPath()
178      */

179     public String JavaDoc getIconPath() {
180
181         return ICONPATH;
182     }
183
184     /**
185      * @see org.opencms.workplace.tools.I_CmsToolHandler#getLink()
186      */

187     public String JavaDoc getLink() {
188
189         return "/system/workplace/views/admin/admin-editor.jsp?resource=/system/workplace/admin/contenttools/check/plugin/propertycheck/configuration.xml";
190     }
191
192     /**
193      * @see org.opencms.workplace.tools.content.check.I_CmsContentCheck#getMessageBundles()
194      */

195     public List JavaDoc getMessageBundles() {
196
197         List JavaDoc messages = new ArrayList JavaDoc();
198         messages.add(org.opencms.workplace.tools.content.check.Messages.get().getBundleName());
199         return messages;
200     }
201
202     /**
203      * @see org.opencms.workplace.tools.I_CmsToolHandler#getName()
204      */

205     public String JavaDoc getName() {
206
207         return NAME;
208     }
209
210     /**
211      * @see org.opencms.workplace.tools.I_CmsToolHandler#getPath()
212      */

213     public String JavaDoc getPath() {
214
215         return "/contenttools/checkconfig/checkproperty";
216     }
217
218     /**
219      * @see org.opencms.workplace.tools.I_CmsToolHandler#getPosition()
220      */

221     public float getPosition() {
222
223         return 1;
224     }
225
226     /**
227      * @see org.opencms.workplace.tools.I_CmsToolHandler#getShortName()
228      */

229     public String JavaDoc getShortName() {
230
231         return NAME;
232     }
233
234     /**
235      * @see org.opencms.workplace.tools.content.check.I_CmsContentCheck#init(org.opencms.file.CmsObject)
236      */

237     public void init(CmsObject cms) {
238
239         m_cms = cms;
240         m_locale = new Locale JavaDoc("en");
241     }
242
243     /**
244      * Gets the active flag.<p>
245      *
246      * @return true if this content check is active, false otherwise.
247      */

248     public boolean isActive() {
249
250         return m_active;
251     }
252
253     /**
254      * Sets the active flag.<p>
255      *
256      * This method is required to build the widget dialog frontend.
257      *
258      * @param value true if this content check is set to be active, false otherwise.
259      */

260     public void setActive(boolean value) {
261
262         m_active = value;
263     }
264
265     /**
266      * Gets the configuration of the property check.<p>
267      *
268      *@throws CmsException if an error occurs reading the configuration
269      */

270     private void getConfiguration() throws CmsException {
271
272         if (m_configuredErrorChecks == null || m_configuredWarningChecks == null) {
273             // get the configuration file
274
CmsResource res = m_cms.readResource(CONFIGURATION);
275             if (LOG.isDebugEnabled()) {
276                 LOG.debug(Messages.get().getBundle().key(
277                     Messages.LOG_DEBUG_PROPERTY_CONFIG_FILENAME_1,
278                     res.getRootPath()));
279             }
280             CmsFile file = CmsFile.upgrade(res, m_cms);
281             if (LOG.isDebugEnabled()) {
282                 LOG.debug(Messages.get().getBundle().key(
283                     Messages.LOG_DEBUG_PROPERTY_CONFIG_FILE_1,
284                     new String JavaDoc(file.getContents())));
285             }
286             CmsXmlContent configuration = CmsXmlContentFactory.unmarshal(m_cms, file);
287
288             // now extract the configured error checks from it
289
m_configuredErrorChecks = getConfiguredChecks(configuration, XPATH_ERROR);
290             m_configuredWarningChecks = getConfiguredChecks(configuration, XPATH_WARNUING);
291         }
292     }
293
294     /**
295      * Reads the configuration for a given xpath and stored all results in a list.<p>
296      *
297      * @param configuration the configuration to read from
298      * @param xpath the xpath prefix
299      * @return list of CmsContentCheckProperetyObject objects
300      */

301     private List JavaDoc getConfiguredChecks(CmsXmlContent configuration, String JavaDoc xpath) {
302
303         List JavaDoc checks = new ArrayList JavaDoc();
304
305         if (LOG.isDebugEnabled()) {
306             LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_PROPERTY_CONFIG_XPATH_2, xpath, m_locale));
307         }
308
309         int size = configuration.getIndexCount(xpath, m_locale);
310         for (int i = 1; i <= size; i++) {
311             // extract the values from the configuration
312
String JavaDoc propertyname = configuration.getValue(xpath + "[" + i + "]/" + XPATH_PROPERTYNAME, m_locale).getStringValue(
313                 m_cms);
314             String JavaDoc type = configuration.getValue(xpath + "[" + i + "]/" + XPATH_TYPE, m_locale).getStringValue(m_cms);
315             String JavaDoc empty = configuration.getValue(xpath + "[" + i + "]/" + XPATH_EMPTY, m_locale).getStringValue(m_cms);
316             String JavaDoc filename = configuration.getValue(xpath + "[" + i + "]/" + XPATH_FILENAME, m_locale).getStringValue(
317                 m_cms);
318             String JavaDoc length = configuration.getValue(xpath + "[" + i + "]/" + XPATH_LENGTH, m_locale).getStringValue(
319                 m_cms);
320             int values = configuration.getIndexCount(xpath + "[" + i + "]/" + XPATH_VALUE, m_locale);
321
322             //String value = configuration.getValue(xpath + "[" + i + "]/" + XPATH_VALUE, m_locale).getStringValue(m_cms);
323

324             // store them in the CmsContentCheckProperetyObject obejct for fürther processing
325
CmsContentCheckProperetyObject propObject = new CmsContentCheckProperetyObject();
326
327             if (CmsStringUtil.isNotEmpty(propertyname)) {
328                 propObject.setPropertyname(propertyname);
329             }
330             if (CmsStringUtil.isNotEmpty(type)) {
331                 propObject.setType(type);
332             }
333             if (CmsStringUtil.isNotEmpty(empty)) {
334                 propObject.setEmpty(empty.equals("true"));
335             }
336             if (CmsStringUtil.isNotEmpty(filename)) {
337                 propObject.setFilename(filename.equals("true"));
338             }
339             if (CmsStringUtil.isNotEmpty(length)) {
340                 propObject.setLength(new Integer JavaDoc(length).intValue());
341             }
342             if (values > 0) {
343                 List JavaDoc valueList = new ArrayList JavaDoc();
344                 for (int j = 1; j <= values; j++) {
345                     String JavaDoc value = configuration.getValue(
346                         xpath + "[" + i + "]/" + XPATH_VALUE + "[" + j + "]",
347                         m_locale).getStringValue(m_cms);
348                     if (CmsStringUtil.isNotEmpty(value)) {
349                         valueList.add(value);
350                     }
351                 }
352                 propObject.setValue(valueList);
353             }
354
355             if (LOG.isDebugEnabled()) {
356                 LOG.debug(Messages.get().getBundle().key(
357                     Messages.LOG_DEBUG_PROPERTY_CONFIG_PROPERTY_3,
358                     new Integer JavaDoc(i),
359                     new Integer JavaDoc(size),
360                     propObject));
361             }
362
363             checks.add(propObject);
364         }
365         return checks;
366     }
367
368     /**
369      * Processes a list of CmsContentCheckProperetyObject and runs all available tests on them.<p>
370      *
371      * All errors or warnings found are collected in a list returned to the calling method.
372      *
373      * @param properties list of CmsContentCheckProperetyObject to process
374      * @param testResource the CmsContentCheckResource to run all tests on
375      * @return list of Strings containing either errors or warinings
376      */

377     private List JavaDoc processProperties(List JavaDoc properties, CmsContentCheckResource testResource) {
378
379         List JavaDoc results = new ArrayList JavaDoc();
380
381         if (LOG.isDebugEnabled()) {
382             LOG.debug(Messages.get().getBundle().key(
383                 Messages.LOG_DEBUG_PROPERTY_RESOURCE_1,
384                 testResource.getResourceName()));
385         }
386
387         //loop through all property tests
388
for (int i = 0; i < properties.size(); i++) {
389             try {
390                 CmsContentCheckProperetyObject propObject = (CmsContentCheckProperetyObject)properties.get(i);
391
392                 if (LOG.isDebugEnabled()) {
393                     LOG.debug(Messages.get().getBundle().key(
394                         Messages.LOG_DEBUG_PROPERTY_PROPERTY_1,
395                         propObject.toString()));
396                 }
397
398                 // check if this test must be done for thies kind of resource
399
if ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_BOTH))
400                     || ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_FILE) && (testResource.getResource().isFile())))
401                     || ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_FOLDER) && (testResource.getResource().isFolder())))
402
403                 ) {
404
405                     // read the property
406
String JavaDoc prop = m_cms.readPropertyObject(
407                         testResource.getResource(),
408                         propObject.getPropertyname(),
409                         false).getValue();
410
411                     if (LOG.isDebugEnabled()) {
412                         LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_PROPERTY_VALUE_1, prop));
413                     }
414
415                     if (LOG.isDebugEnabled()) {
416                         LOG.debug(Messages.get().getBundle().key(
417                             Messages.LOG_DEBUG_PROPERTY_ISEMPTYCHECK_1,
418                             new Boolean JavaDoc(propObject.isEmpty())));
419                     }
420
421                     // test if the property is empty
422
if (propObject.isEmpty() && CmsStringUtil.isEmpty(prop)) {
423                         results.add(Messages.get().getBundle().key(
424                             Messages.ERR_CHECK_NO_PROPERTYNAME_1,
425                             propObject.getPropertyname()));
426                         if (LOG.isDebugEnabled()) {
427                             LOG.debug(Messages.get().getBundle().key(
428                                 Messages.ERR_CHECK_NO_PROPERTYNAME_1,
429                                 propObject.getPropertyname()));
430                         }
431                     }
432
433                     if (LOG.isDebugEnabled()) {
434                         LOG.debug(Messages.get().getBundle().key(
435                             Messages.LOG_DEBUG_PROPERTY_ISFILENAME_1,
436                             new Boolean JavaDoc(propObject.isFilename())));
437                     }
438
439                     // test if the property does not start with the filename
440
if (!CmsStringUtil.isEmpty(prop)) {
441                         if (propObject.isFilename()
442                             && testResource.getResource().getName().toLowerCase().startsWith(prop.toLowerCase())) {
443                             results.add(Messages.get().getBundle().key(
444                                 Messages.ERR_CHECK_CONTAINS_FILENAME_2,
445                                 propObject.getPropertyname(),
446                                 prop));
447                             if (LOG.isDebugEnabled()) {
448                                 LOG.debug(Messages.get().getBundle().key(
449                                     Messages.ERR_CHECK_CONTAINS_FILENAME_2,
450                                     propObject.getPropertyname(),
451                                     prop));
452                             }
453                         }
454
455                         if (LOG.isDebugEnabled()) {
456                             LOG.debug(Messages.get().getBundle().key(
457                                 Messages.LOG_DEBUG_PROPERTY_CHECKLENGTH_2,
458                                 new Integer JavaDoc(propObject.getLength()),
459                                 new Integer JavaDoc(prop.length())));
460                         }
461                         // test if the minmal property length is valid
462
if (propObject.getLength() > -1) {
463                             if (prop.length() < propObject.getLength()) {
464                                 results.add(Messages.get().getBundle().key(
465                                     Messages.ERR_CHECK_TOO_SHORT_3,
466                                     propObject.getPropertyname(),
467                                     prop,
468                                     new Integer JavaDoc(prop.length())));
469                                 if (LOG.isDebugEnabled()) {
470                                     LOG.debug(Messages.get().getBundle().key(
471                                         Messages.ERR_CHECK_TOO_SHORT_3,
472                                         propObject.getPropertyname(),
473                                         prop,
474                                         new Integer JavaDoc(prop.length())));
475                                 }
476                             }
477                         }
478
479                         // test if the value matches a regex
480
if (propObject.getValue().size() > 0) {
481                             for (int j = 0; j < propObject.getValue().size(); j++) {
482
483                                 String JavaDoc regex = new String JavaDoc((String JavaDoc)propObject.getValue().get(j));
484
485                                 boolean matchResult = true;
486                                 if (regex.charAt(0) == '!') {
487                                     // negate the pattern
488
matchResult = false;
489                                     regex = regex.substring(1);
490                                 }
491                                 String JavaDoc matchValue = prop;
492                                 boolean match = Pattern.matches(regex, matchValue);
493
494                                 if (LOG.isDebugEnabled()) {
495                                     LOG.debug(Messages.get().getBundle().key(
496                                         Messages.LOG_DEBUG_PROPERTY_MATCHPATTERN_2,
497                                         regex,
498                                         matchValue));
499                                 }
500
501                                 if (matchResult != match) {
502                                     results.add(Messages.get().getBundle().key(
503                                         Messages.ERR_CHECK_MATCH_3,
504                                         propObject.getPropertyname(),
505                                         prop,
506                                         propObject.getValue().get(j)));
507                                     if (LOG.isDebugEnabled()) {
508                                         LOG.debug(Messages.get().getBundle().key(
509                                             Messages.ERR_CHECK_MATCH_3,
510                                             propObject.getPropertyname(),
511                                             prop,
512                                             propObject.getValue().get(j)));
513                                     }
514                                 }
515                             }
516                         }
517                     }
518                 }
519
520             } catch (CmsException e) {
521                 LOG.error(Messages.get().getBundle().key(
522                     Messages.LOG_ERROR_PROCESSING_PROPERTIES_2,
523                     testResource.getResourceName(),
524                     e));
525             }
526         }
527
528         return results;
529     }
530 }
Popular Tags