1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.commons.validator;
20
21 import java.util.regex.PatternSyntaxException;
22
23 import javax.faces.application.FacesMessage;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.validator.Validator;
27 import javax.faces.validator.ValidatorException;
28
29 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
30 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
31 import org.apache.myfaces.commons.util.MessageUtils;
32
33
34
35
36
37
38
39
40
41 @JSFValidator(
42 name = "mcv:validateCSV",
43 clazz = "org.apache.myfaces.commons.validator.CSVValidator",
44 tagClass = "org.apache.myfaces.commons.validator.ValidateCSVTag",
45 serialuidtag = "-8874279182242196266L")
46 public abstract class AbstractCSVValidator extends ValidatorBase {
47
48
49
50 public static final String VALIDATOR_ID = "org.apache.myfaces.commons.validator.csv";
51
52
53
54
55 public static final String CSV_NOT_STRING_MESSAGE_ID = "org.apache.myfaces.commons.validator.csv.NOT_STRING";
56 public static final String CSV_INVALID_SEPARATOR_MESSAGE_ID = "org.apache.myfaces.commons.validator.csv.INVALID_SEPARATOR";
57 public static final String CSV_SUFFIX_MESSAGE_ID = "org.apache.myfaces.commons.validator.csv.SUFFIX";
58 private static final String DEFAULT_SEPARATOR = ",";
59
60
61
62
63 @JSFProperty
64 public abstract String getSubvalidatorId();
65
66
67
68
69 public abstract void setSubvalidatorId(String subvalidatorId);
70
71
72
73
74
75 @JSFProperty(
76 literalOnly = true)
77 public abstract String getSeparator();
78
79
80
81
82 public abstract void setSeparator(String separator);
83
84 private FacesMessage addMessage(FacesMessage oldMsg, FacesMessage newMsg, int index, String suffixMessageKey) {
85 if (oldMsg != null && newMsg.getSeverity().getOrdinal() < oldMsg.getSeverity().getOrdinal())
86 return oldMsg;
87 String summaryMessageText = null;
88 String detailMessageText = null;
89 if (oldMsg == null || newMsg.getSeverity().getOrdinal() > oldMsg.getSeverity().getOrdinal()) {
90 summaryMessageText = null;
91 detailMessageText = null;
92 }
93 else {
94 summaryMessageText = oldMsg.getSummary();
95 detailMessageText = oldMsg.getDetail();
96 }
97 Object[] args = { Integer.valueOf(index + 1) };
98 FacesMessage suffixMessage = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, suffixMessageKey, args);
99 String summarySuffix = suffixMessage.getSummary();
100 String detailSuffix = suffixMessage.getDetail();
101 if (summarySuffix == null)
102 summarySuffix = detailSuffix;
103 else if (detailSuffix == null)
104 detailSuffix = summarySuffix;
105 String summary = newMsg.getSummary();
106 if (summaryMessageText == null)
107 summaryMessageText = summary + summarySuffix;
108 else
109 summaryMessageText += ", " + summary + summarySuffix;
110 String detail = newMsg.getDetail();
111 if (detailMessageText == null)
112 detailMessageText = detail + detailSuffix;
113 else
114 detailMessageText += ", " + detail + detailSuffix;
115 return new FacesMessage(newMsg.getSeverity(), summaryMessageText, detailMessageText);
116 }
117
118 public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
119
120 if (facesContext == null) throw new NullPointerException("facesContext");
121 if (uiComponent == null) throw new NullPointerException("uiComponent");
122
123 if (value == null)
124 {
125 return;
126 }
127
128 String suffixMessageKey = getMessage();
129 if (suffixMessageKey == null)
130 suffixMessageKey = CSV_SUFFIX_MESSAGE_ID;
131 FacesMessage facesMsg = null;
132
133 if (!(value instanceof String)) {
134 Object[] args = { value };
135 throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, CSV_NOT_STRING_MESSAGE_ID, args));
136 }
137 Validator validator = facesContext.getApplication().createValidator(getSubvalidatorId());
138 if (getSeparator() == null)
139 setSeparator(DEFAULT_SEPARATOR);
140 String[] values = null;
141 try {
142 values = ((String)value).split(getSeparator());
143 }
144 catch (PatternSyntaxException e) {
145 Object[] args = { getSeparator() };
146 throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, CSV_INVALID_SEPARATOR_MESSAGE_ID, args));
147 }
148
149
150 for (int i = 0; i < values.length; i++) {
151 if (values[i].trim().length() == 0) {
152 continue;
153 }
154 else try {
155 validator.validate(facesContext, uiComponent, values[i]);
156 }
157 catch (ValidatorException e) {
158 facesMsg = addMessage(facesMsg, e.getFacesMessage(), i, suffixMessageKey);
159 }
160 }
161 if (facesMsg != null)
162 throw new ValidatorException(facesMsg);
163 }
164 }