View Javadoc

1   /*
2    * $Id: HourChecker.java,v 1.1 2008/06/06 14:39:21 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.validator.impl;
5   
6   import java.util.regex.Matcher;
7   import java.util.regex.Pattern;
8   
9   import javax.faces.application.FacesMessage;
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  import org.rcfaces.core.validator.ICheckerTask;
14  import org.rcfaces.core.validator.IClientValidatorContext;
15  
16  /**
17   * 
18   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
19   * @version $Revision: 1.1 $ $Date: 2008/06/06 14:39:21 $
20   */
21  public class HourChecker extends AbstractClientValidatorTask implements
22          ICheckerTask {
23      private static final String REVISION = "$Revision: 1.1 $";
24  
25      private static final Log LOG = LogFactory.getLog(HourChecker.class);
26  
27      private static final Pattern HOUR_SEPARATOR_PATTERN = Pattern
28              .compile("^\\d*$");
29  
30      public String applyChecker(IClientValidatorContext context, String value) {
31          if (value == null || value.length() == 0) {
32              return value;
33          }
34  
35          String sep = getParameter(context, "hour.sepSign");
36          String set = "[" + buildEscaped(sep) + "]";
37          char sepChar = sep.charAt(0);
38  
39          // String sTmp = value;
40          String h = null;
41          String m = null;
42          String sec = null;
43          boolean invalidFormat = true;
44  
45          Matcher matcher = HOUR_SEPARATOR_PATTERN.matcher(value);
46  
47          // Check if digits only
48          if (matcher.matches()) {
49              switch (matcher.end()) {
50              case 6:
51                  sec = value.substring(4, 2);
52              case 4:
53                  m = value.substring(2, 2);
54              case 2:
55                  h = value.substring(0, 2);
56                  invalidFormat = false;
57                  break;
58              case 1:
59                  h = value;
60              case 0:
61                  invalidFormat = false;
62                  break;
63              }
64              // Otherwise we have separators
65          } else {
66              Pattern pattern = getPattern("^(\\d{1,2})?" + set + "(\\d{1,2})?"
67                      + set + "?(\\d{1,2})?$");
68  
69              matcher = pattern.matcher(value);
70  
71              if (matcher.matches()) {
72                  invalidFormat = false;
73  
74                  h = matcher.group(0);
75                  m = matcher.group(1);
76                  sec = matcher.group(2);
77              }
78          }
79  
80          // Check valid string
81          if (invalidFormat) {
82              String formattedValue = value.replaceAll("(" + set + ")", String
83                      .valueOf(sepChar));
84  
85              context.setInputValue(formattedValue);
86              context.setOutputValue(formattedValue);
87              return null;
88          }
89  
90          // IProcessContext processContext =
91          // context.getComponentRenderContext().getRenderContext().getProcessContext();
92  
93          // Compute hour
94          int ih;
95          try {
96              ih = (h != null) ? Integer.parseInt(h) : 0;
97  
98          } catch (NumberFormatException ex) {
99              LOG.debug("Invalid hours '" + value + "'", ex);
100             ih = -1;
101         }
102 
103         if (ih < 0 || ih > 23) {
104             context.setLastError("VALIDATION HEURE", "Heure invalide",
105                     FacesMessage.SEVERITY_ERROR);
106             return null;
107         }
108 
109         int im;
110         try {
111             im = (m != null) ? Integer.parseInt(m) : 0;
112 
113         } catch (NumberFormatException ex) {
114             LOG.debug("Invalid minutes '" + value + "'", ex);
115             im = -1;
116         }
117 
118         if (im < 0 || im > 23) {
119             context.setLastError("VALIDATION HEURE", "Minutes invalides",
120                     FacesMessage.SEVERITY_ERROR);
121             return null;
122         }
123 
124         int is;
125         try {
126             is = (sec != null) ? Integer.parseInt(sec) : 0;
127 
128         } catch (NumberFormatException ex) {
129             LOG.debug("Invalid seconds '" + value + "'", ex);
130             is = -1;
131         }
132 
133         if (is < 0 || is > 23) {
134             context.setLastError("VALIDATION HEURE", "Secondes invalides",
135                     FacesMessage.SEVERITY_ERROR);
136             return null;
137         }
138 
139         // Build input and output value
140         String formattedValue = ((ih < 10) ? "0" : "") + ih + sepChar
141                 + ((im < 10) ? "0" : "") + im + sepChar
142                 + ((is < 10) ? "0" : "") + is;
143 
144         context.setInputValue(formattedValue);
145         context.setOutputValue(formattedValue);
146 
147         return formattedValue;
148     }
149 }