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.commons.util.MessageUtils;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public abstract class AbstractCSVValidator extends ValidatorBase {
45
46
47
48 public static final String VALIDATOR_ID = "org.apache.myfaces.commons.validator.csv";
49
50
51
52
53 public static final String CSV_NOT_STRING_MESSAGE_ID = "org.apache.myfaces.commons.validator.csv.NOT_STRING";
54 public static final String CSV_INVALID_SEPARATOR_MESSAGE_ID = "org.apache.myfaces.commons.validator.csv.INVALID_SEPARATOR";
55 public static final String CSV_SUFFIX_MESSAGE_ID = "org.apache.myfaces.commons.validator.csv.SUFFIX";
56 private static final String DEFAULT_SEPARATOR = ",";
57
58
59
60
61
62 public abstract String getSubvalidatorId();
63
64
65
66
67 public abstract void setSubvalidatorId(String subvalidatorId);
68
69
70
71
72
73
74 public abstract String getSeparator();
75
76
77
78
79 public abstract void setSeparator(String separator);
80
81 private FacesMessage addMessage(FacesMessage oldMsg, FacesMessage newMsg, int index, String suffixMessageKey) {
82 if (oldMsg != null && newMsg.getSeverity().getOrdinal() < oldMsg.getSeverity().getOrdinal())
83 return oldMsg;
84 String summaryMessageText = null;
85 String detailMessageText = null;
86 if (oldMsg == null || newMsg.getSeverity().getOrdinal() > oldMsg.getSeverity().getOrdinal()) {
87 summaryMessageText = null;
88 detailMessageText = null;
89 }
90 else {
91 summaryMessageText = oldMsg.getSummary();
92 detailMessageText = oldMsg.getDetail();
93 }
94 Object[] args = { Integer.valueOf(index + 1) };
95 FacesMessage suffixMessage = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, suffixMessageKey, args);
96 String summarySuffix = suffixMessage.getSummary();
97 String detailSuffix = suffixMessage.getDetail();
98 if (summarySuffix == null)
99 summarySuffix = detailSuffix;
100 else if (detailSuffix == null)
101 detailSuffix = summarySuffix;
102 String summary = newMsg.getSummary();
103 if (summaryMessageText == null)
104 summaryMessageText = summary + summarySuffix;
105 else
106 summaryMessageText += ", " + summary + summarySuffix;
107 String detail = newMsg.getDetail();
108 if (detailMessageText == null)
109 detailMessageText = detail + detailSuffix;
110 else
111 detailMessageText += ", " + detail + detailSuffix;
112 return new FacesMessage(newMsg.getSeverity(), summaryMessageText, detailMessageText);
113 }
114
115 public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
116
117 if (facesContext == null) throw new NullPointerException("facesContext");
118 if (uiComponent == null) throw new NullPointerException("uiComponent");
119
120 if (value == null)
121 {
122 return;
123 }
124
125 String suffixMessageKey = getMessage();
126 if (suffixMessageKey == null)
127 suffixMessageKey = CSV_SUFFIX_MESSAGE_ID;
128 FacesMessage facesMsg = null;
129
130 if (!(value instanceof String)) {
131 Object[] args = { value };
132 throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, CSV_NOT_STRING_MESSAGE_ID, args));
133 }
134 Validator validator = facesContext.getApplication().createValidator(getSubvalidatorId());
135 if (getSeparator() == null)
136 setSeparator(DEFAULT_SEPARATOR);
137 String[] values = null;
138 try {
139 values = ((String)value).split(getSeparator());
140 }
141 catch (PatternSyntaxException e) {
142 Object[] args = { getSeparator() };
143 throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, CSV_INVALID_SEPARATOR_MESSAGE_ID, args));
144 }
145
146
147 for (int i = 0; i < values.length; i++) {
148 if (values[i].trim().length() == 0) {
149 continue;
150 }
151 else try {
152 validator.validate(facesContext, uiComponent, values[i]);
153 }
154 catch (ValidatorException e) {
155 facesMsg = addMessage(facesMsg, e.getFacesMessage(), i, suffixMessageKey);
156 }
157 }
158 if (facesMsg != null)
159 throw new ValidatorException(facesMsg);
160 }
161 }