View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Validation by validating comma separated values individually.
33   * 
34   * @JSFValidator
35   *   name = "mcv:validateCSV"
36   *   class = "org.apache.myfaces.commons.validator.CSVValidator"
37   *   tagClass = "org.apache.myfaces.commons.validator.ValidateCSVTag"
38   *   serialuidtag = "-8874279182242196266L"
39   *
40   * @author Lance Frohman
41   *
42   * @version $Revision: 687546 $ $Date: 2008-08-20 22:55:03 -0500 (Wed, 20 Aug 2008) $
43   */
44  public abstract class AbstractCSVValidator extends ValidatorBase {
45      /**
46       * <p>The standard converter id for this converter.</p>
47       */
48      public static final String VALIDATOR_ID = "org.apache.myfaces.commons.validator.csv";
49      /**
50       * <p>The message identifiers of the {@link FacesMessage} to be created if
51       * the check fails.</p>
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       * @JSFProperty
60       * @return the VALIDATOR_ID of the actual validator to be used
61       */
62      public abstract String getSubvalidatorId();
63  
64      /**
65       * @param the VALIDATOR_ID of the actual validator to be used
66       */
67      public abstract void setSubvalidatorId(String subvalidatorId);
68  
69      /**
70       * @JSFProperty
71       *   literalOnly = "true"
72       * @return the separator character to separate values
73       */
74      public abstract String getSeparator();
75  
76      /**
77       * @param the separator character to separate values
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         // value must be a String
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         // loop through the separated values and validate each one
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 }