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.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   * Validation by validating comma separated values individually.
32   * 
33   * @JSFValidator
34   *   name = "s:validateCSV"
35   *   class = "org.apache.myfaces.custom.csvvalidator.CSVValidator"
36   *   tagClass = "org.apache.myfaces.custom.csvvalidator.ValidateCSVTag"
37   *   serialuidtag = "-8874279182242196266L"
38   *
39   * @author Lance Frohman
40   *
41   * @version $Revision: 940142 $ $Date: 2010-05-01 20:36:31 -0500 (Sat, 01 May 2010) $
42   */
43  public abstract class AbstractCSVValidator extends ValidatorBase {
44      /**
45       * <p>The standard converter id for this converter.</p>
46       */
47      public static final String VALIDATOR_ID = "org.apache.myfaces.validator.csv";
48      /**
49       * <p>The message identifiers of the {@link FacesMessage} to be created if
50       * the check fails.</p>
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       * @JSFProperty
59       * @return the VALIDATOR_ID of the actual validator to be used
60       */
61      public abstract String getSubvalidatorId();
62  
63      /**
64       * @param the VALIDATOR_ID of the actual validator to be used
65       */
66      public abstract void setSubvalidatorId(String subvalidatorId);
67  
68      /**
69       * @JSFProperty
70       *   literalOnly = "true"
71       * @return the separator character to separate values
72       */
73      public abstract String getSeparator();
74  
75      /**
76       * @param the separator character to separate values
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         // value must be a String
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         // loop through the separated values and validate each one
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 }