aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/data/Validator.java
blob: 2dcbb5bb4357b501cdd5efbe3925cc718ae17977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* *************************************************************************
 
 IT Mill Toolkit 

 Development of Browser User Interfaces Made Easy

 Copyright (C) 2000-2006 IT Mill Ltd
 
 *************************************************************************

 This product is distributed under commercial license that can be found
 from the product package on license.pdf. Use of this product might 
 require purchasing a commercial license from IT Mill Ltd. For guidelines 
 on usage, see licensing-guidelines.html

 *************************************************************************
 
 For more information, contact:
 
 IT Mill Ltd                           phone: +358 2 4802 7180
 Ruukinkatu 2-4                        fax:   +358 2 4802 7181
 20540, Turku                          email:  info@itmill.com
 Finland                               company www: www.itmill.com
 
 Primary source for information and releases: www.itmill.com

 ********************************************************************** */

package com.itmill.toolkit.data;

import com.itmill.toolkit.terminal.ErrorMessage;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;

/**
 * Object validator interface. Implementors of this class can be added to any
 * {@link com.itmill.toolkit.data.Validatable} object to verify its value. The
 * <code>Validatable#isValid(Object)</code> iterates all registered
 * <code>Validator</code>s, calling their {@link #validate(Object)} methods.
 * <code>validate(Object)</code> should throw the
 * {@link Validator.InvalidValueException} if the given value is not valid by
 * its standards.
 * 
 * @author IT Mill Ltd.
 * @version
 * @VERSION@
 * @since 3.0
 */
public interface Validator {

    /**
     * Checks the given value against this validator. If the value is valid this
     * method should do nothing, and if it's not valid, it should throw
     * <code>Validator.InvalidValueException</code>
     * 
     * @param value
     *                the value to check
     * @throws Validator.InvalidValueException
     *                 if the value is not valid
     */
    public void validate(Object value) throws Validator.InvalidValueException;

    /**
     * Tests if the given value is valid.
     * 
     * @param value
     *                the value to check
     * @return <code>true</code> for valid value, otherwise <code>false</code>.
     */
    public boolean isValid(Object value);

    /**
     * Adds the proposing functionality to a {@link Validator}. A
     * <code>Suggestive</code> validator can propose a valid value for the
     * object it is attached to validate. This way the {@link Validatable}
     * object may avoid situations where it contains a value that could lead to
     * a error.
     * 
     * @author IT Mill Ltd.
     * @version
     * @VERSION@
     * @since 3.0
     */
    public interface Suggestive extends Validator {

        /**
         * Suggests another value that can be used instead of the proposedValue
         * if it is invalid. If it is valid in the opinion of this validator,
         * however, it is returned as is.
         * 
         * @param proposedValue
         *                Originally proposed value that could be invalid.
         * @return Suggested value that's not invalid against this validator
         */
        public Object suggestValidValue(Object proposedValue);
    }

    /**
     * Invalid value exception can be thrown by {@link Validator} when a given
     * value is not valid.
     * 
     * @author IT Mill Ltd.
     * @version
     * @VERSION@
     * @since 3.0
     */
    public class InvalidValueException extends RuntimeException implements
            ErrorMessage {

        /**
         * Serial generated by eclipse.
         */
        private static final long serialVersionUID = 3689073941163422257L;

        /** Array of validation errors that are causing the problem. */
        private InvalidValueException[] causes = null;

        /**
         * Constructs a new <code>InvalidValueException</code> with the
         * specified detail message.
         * 
         * @param message
         *                The detail message of the problem.
         */
        public InvalidValueException(String message) {
            this(message, new InvalidValueException[] {});
        }

        /**
         * Constructs a new <code>InvalidValueException</code> with a set of
         * causing validation exceptions. The error message contains first the
         * given message and then a list of validation errors in the given
         * validatables.
         * 
         * @param message
         *                The detail message of the problem.
         * @param causes
         *                Array of validatables whos invalidities are possiblity
         *                causing the invalidity.
         */
        public InvalidValueException(String message,
                InvalidValueException[] causes) {
            super(message);
            if (causes == null) {
                throw new NullPointerException(
                        "Possible causes array must not be null");
            }
            this.causes = causes;
        }

        public final int getErrorLevel() {
            return ErrorMessage.ERROR;
        }

        public void paint(PaintTarget target) throws PaintException {
            target.startTag("error");
            target.addAttribute("level", "error");

            // Error message
            String message = getLocalizedMessage();
            if (message != null) {
                target.addText(message);
            }

            // Paint all the causes
            for (int i = 0; i < causes.length; i++) {
                causes[i].paint(target);
            }

            target.endTag("error");
        }

        /* Documented in super interface */
        public void addListener(RepaintRequestListener listener) {
        }

        /* Documented in super interface */
        public void removeListener(RepaintRequestListener listener) {
        }

        /* Documented in super interface */
        public void requestRepaint() {
        }

        /* Documented in super interface */
        public void requestRepaintRequests() {
        }

    }
}