01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: SupportedXhtml.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.cmf.validation;
09:
10: import com.uwyn.rife.cmf.loader.XhtmlContentLoader;
11: import com.uwyn.rife.site.ValidationError;
12: import com.uwyn.rife.tools.BeanUtils;
13: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
14: import java.util.HashSet;
15: import java.util.Set;
16:
17: /**
18: * A validation rule that checks if the data in a property is able to be
19: * loaded as supported and valid xhtml without errors.
20: *
21: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
22: * @version $Revision: 3634 $
23: * @since 1.0
24: */
25: public class SupportedXhtml extends CmfPropertyValidationRule {
26: /**
27: * Instantiates a new <code>SupportedXhtml</code> instance.
28: *
29: * @param propertyName the name of the property
30: * @param fragment <code>true</code> if the property is a fragment; or
31: * <p><code>false</code> if it's a complete document
32: * @since 1.0
33: */
34: public SupportedXhtml(String propertyName, boolean fragment) {
35: super (propertyName, fragment);
36: }
37:
38: public boolean validate() {
39: Object value = null;
40: try {
41: value = BeanUtils.getPropertyValue(getBean(),
42: getPropertyName());
43: } catch (BeanUtilsException e) {
44: // an error occurred when obtaining the value of the property
45: // just consider it valid to skip over it
46: return true;
47: }
48:
49: if (null == value) {
50: return true;
51: }
52:
53: // try to load the data in the property and if that's not possible, the data
54: // is considered invalid
55: Set<String> errors = new HashSet<String>();
56: String data = new XhtmlContentLoader().load(value,
57: getFragment(), errors);
58: if (errors.size() > 0) {
59: setLoadingErrors(errors);
60: }
61: if (null == data) {
62: return false;
63: }
64:
65: setCachedLoadedData(data);
66:
67: return true;
68: }
69:
70: public ValidationError getError() {
71: return new ValidationError.INVALID(getSubject());
72: }
73: }
|