View Javadoc

1   /*
2    * $Id: NumChecker.java,v 1.2 2008/06/05 13:50:33 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 org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.rcfaces.core.validator.ICheckerTask;
12  import org.rcfaces.core.validator.IClientValidatorContext;
13  
14  /**
15   * 
16   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
17   * @version $Revision: 1.2 $ $Date: 2008/06/05 13:50:33 $
18   */
19  public class NumChecker extends AbstractClientValidatorTask implements
20          ICheckerTask {
21      private static final String REVISION = "$Revision: 1.2 $";
22  
23      private static final Log LOG = LogFactory.getLog(NumChecker.class);
24  
25      private static final Pattern ZERO_SEPARATOR_PATTERN = Pattern
26              .compile("^(0+)([0-9]*)$");
27  
28      public String applyChecker(IClientValidatorContext context, String value) {
29          if (value == null || value.length() == 0) {
30              return value;
31          }
32  
33          String sep = getParameter(context, "num.sepSign");
34          if (sep != null && sep.length() > 0) {
35              Pattern pattern = getPattern("[" + buildEscaped(sep) + "]");
36  
37              value = pattern.matcher(value).replaceAll("");
38          }
39  
40          String neg = getParameter(context, "num.negSign", "-");
41          if (value.lastIndexOf(neg) > 0) {
42              value = neg + value.replaceAll(buildEscaped(neg), "");
43          }
44  
45          String dec = getParameter(context, "num.decSign");
46          Pattern pattern = getPattern("^(" + buildEscaped(neg) + "?)(\\d*)(["
47                  + buildEscaped(dec) + "]?)(\\d*)$");
48  
49          // Check expression
50          Matcher matcher = pattern.matcher(value);
51  
52          // No match
53          if (matcher.matches() == false) {
54              return null;
55          }
56  
57          int showDecimal = 0;
58          String numDecimalParameter = getParameter(context, "num.decimal");
59          if (numDecimalParameter != null) {
60              if ("true".equalsIgnoreCase(numDecimalParameter)) {
61                  showDecimal = -1;
62              } else {
63                  showDecimal = Integer.parseInt(numDecimalParameter);
64              }
65          }
66  
67          // Get parts
68          String ip = "0";
69          String d = "";
70          String dp = (showDecimal > 0) ? "0" : "";
71  
72          int groupCount = matcher.groupCount();
73          String n = matcher.group(1);
74          if (groupCount > 1) {
75              ip = matcher.group(2);
76              if (groupCount > 2) {
77                  d = dec.substring(0, 1);
78                  if (groupCount > 3) {
79                      dp = matcher.group(4);
80                  }
81              }
82          }
83  
84          int decimal = getIntParameter(context, "num.cutDecimal", -1);
85  
86          if (decimal > 0 && dp.length() > decimal) {
87              dp = dp.substring(0, decimal);
88  
89          } else if (decimal == 0) {
90              d = "";
91              dp = "";
92          }
93  
94          if (ip.length() > 1) {
95              // Retire les 0 au debut !
96  
97              matcher = ZERO_SEPARATOR_PATTERN.matcher(ip);
98  
99              if (matcher.matches()) {
100                 ip = (matcher.groupCount() > 1) ? matcher.group(1) : "0";
101             }
102         }
103 
104         int lenDp = showDecimal;
105         if (lenDp < 1) {
106             lenDp = 1;
107         }
108 
109         if (dp.length() > lenDp) {
110             char chs[] = dp.toCharArray();
111             int last = chs.length;
112             for (int i = chs.length; i > lenDp; i--) {
113                 if (chs[i - 1] != '0') {
114                     break;
115                 }
116 
117                 last = i - 1;
118             }
119 
120             if (last != chs.length) {
121                 dp = new String(chs, 0, last);
122             }
123         }
124 
125         if (d.length() > 0 && dp.length() == 0) {
126             if (showDecimal > 0) {
127                 dp = "0";
128             } else {
129                 d = "";
130             }
131         }
132 
133         // Rebuild string
134         value = n + ip + d + dp;
135 
136         return value;
137     }
138 }