01: /**********************************************************************************
02: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/tool-lib/src/java/org/theospi/portfolio/style/tool/StyleValidator.java $
03: * $Id:StyleValidator.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.theospi.portfolio.style.tool;
21:
22: import org.springframework.validation.Errors;
23: import org.theospi.portfolio.style.model.Style;
24: import org.theospi.utils.mvc.impl.ValidatorBase;
25:
26: /**
27: * Created by IntelliJ IDEA.
28: * User: John Ellis
29: * Date: Apr 23, 2004
30: * Time: 2:37:33 PM
31: * To change this template use File | Settings | File Templates.
32: */
33: public class StyleValidator extends ValidatorBase {
34:
35: /**
36: * Return whether or not this object can validate objects
37: * of the given class.
38: */
39: public boolean supports(Class clazz) {
40: if (Style.class.isAssignableFrom(clazz))
41: return true;
42: return false;
43: }
44:
45: /**
46: * Validate a presentation object, which must be of a class for which
47: * the supports() method returned true.
48: *
49: * @param obj Populated object to validate
50: * @param errors Errors object we're building. May contain
51: * errors for this field relating to types.
52: */
53: public void validate(Object obj, Errors errors) {
54: if (obj instanceof Style)
55: validateStyle((Style) obj, errors);
56: }
57:
58: protected void validateStyle(Style style, Errors errors) {
59: if (style.isValidate()) {
60: if (style.getName() == null
61: || style.getName().length() == 0) {
62: errors.rejectValue("name", "error.required",
63: "name is required");
64: }
65: if (style.getStyleFile() == null
66: || style.getStyleFile().getValue() == null
67: || style.getStyleFile().getValue().length() == 0) {
68: errors.rejectValue("styleFile", "error.required",
69: "Style file is required");
70: }
71: if (style.getDescription() != null
72: && style.getDescription().length() > 255) {
73: errors.rejectValue("description",
74: "error.lengthExceded", new Object[] { "255" },
75: "Value must be less than {0} characters");
76: }
77: }
78: }
79: }
|