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.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   * Validation by validating comma separated values individually.
35   * 
36   *
37   * @author Lance Frohman
38   *
39   * @version $Revision: 1021618 $ $Date: 2010-10-11 23:08:53 -0500 (Mon, 11 Oct 2010) $
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       * <p>The standard converter id for this converter.</p>
49       */
50      public static final String VALIDATOR_ID = "org.apache.myfaces.commons.validator.csv";
51      /**
52       * <p>The message identifiers of the {@link FacesMessage} to be created if
53       * the check fails.</p>
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       * @return the VALIDATOR_ID of the actual validator to be used
62       */
63      @JSFProperty
64      public abstract String getSubvalidatorId();
65  
66      /**
67       * @param the VALIDATOR_ID of the actual validator to be used
68       */
69      public abstract void setSubvalidatorId(String subvalidatorId);
70  
71      /**
72       * 
73       * @return the separator character to separate values
74       */
75      @JSFProperty(
76         literalOnly = true)
77      public abstract String getSeparator();
78  
79      /**
80       * @param the separator character to separate values
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         // value must be a String
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         // loop through the separated values and validate each one
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 }