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: SupportedImage.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.ImageContentLoader;
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.awt.Image;
15: import java.util.HashSet;
16: import java.util.Set;
17:
18: /**
19: * A validation rule that checks if the data in a property is able to be
20: * loaded as a supported image format without errors.
21: *
22: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
23: * @version $Revision: 3634 $
24: * @since 1.0
25: */
26: public class SupportedImage extends CmfPropertyValidationRule {
27: /**
28: * Creates a new <code>SupportedImage</code> instance.
29: *
30: * @param propertyName the name of the property that has to be validated
31: * @since 1.0
32: */
33: public SupportedImage(String propertyName) {
34: super (propertyName, false);
35: }
36:
37: public boolean validate() {
38: Object value = null;
39: try {
40: value = BeanUtils.getPropertyValue(getBean(),
41: getPropertyName());
42: } catch (BeanUtilsException e) {
43: // an error occurred when obtaining the value of the property
44: // just consider it valid to skip over it
45: return true;
46: }
47:
48: if (null == value) {
49: return true;
50: }
51:
52: // try to load the data in the property and if that's not possible, the data
53: // is considered invalid
54: Set<String> errors = new HashSet<String>();
55: Image data = new ImageContentLoader().load(value,
56: getFragment(), errors);
57: if (errors.size() > 0) {
58: setLoadingErrors(errors);
59: }
60: if (null == data) {
61: return false;
62: }
63:
64: setCachedLoadedData(data);
65:
66: return true;
67: }
68:
69: public ValidationError getError() {
70: return new ValidationError.INVALID(getSubject());
71: }
72: }
|