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