View Javadoc

1   package org.cateproject.controller.common;
2   
3   import org.cateproject.controller.sanitize.AntiSamyService;
4   import org.springframework.beans.factory.annotation.Autowired;
5   import org.springframework.stereotype.Component;
6   import org.springframework.validation.Errors;
7   import org.springframework.validation.ValidationUtils;
8   import org.springframework.validation.Validator;
9   
10  import eu.etaxonomy.cdm.model.common.Annotation;
11  
12  @Component
13  public class AnnotationValidator implements Validator {
14  
15  	private AntiSamyService antiSamyService;
16  	
17  	@Autowired
18  	public void setAntiSamyService(AntiSamyService antiSamyService) {
19  		this.antiSamyService = antiSamyService;
20  	}
21  
22  	public boolean supports(Class clazz) {
23  		return clazz.equals(Annotation.class);
24  	}
25  
26  	public void validate(Object target, Errors errors) {
27  		Annotation annotation = (Annotation) target;
28  		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "text", "text.required");
29  		
30  		if(annotation.getText() != null) {
31  			String text = antiSamyService.sanitize(annotation.getText(),"text",errors);
32  			annotation.setText(text);
33  		}
34  	}	
35  }