View Javadoc

1   /*
2    * $Id: NumFormatter.java,v 1.5 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.internal.lang.StringAppender;
12  import org.rcfaces.core.validator.IClientValidatorContext;
13  import org.rcfaces.core.validator.IFormatterTask;
14  
15  /**
16   * 
17   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
18   * @version $Revision: 1.5 $ $Date: 2008/06/05 13:50:33 $
19   */
20  public class NumFormatter extends AbstractClientValidatorTask implements
21          IFormatterTask {
22      private static final String REVISION = "$Revision: 1.5 $";
23  
24      private static final Log LOG = LogFactory.getLog(NumFormatter.class);
25  
26      private static final Pattern LEADING_SPACES_PATTERN = Pattern
27              .compile("^(\\s)");
28  
29      public String applyFormatter(IClientValidatorContext context, String value) {
30          if (value.length() < 1) {
31              return value;
32          }
33  
34          // int cutDecimal = getIntParameter(context, "num.cutDecimal", -1);
35          int showDecimal = 0;
36          String numDecimal = getParameter(context, "num.decimal");
37          if (numDecimal != null) {
38              if ("true".equalsIgnoreCase(numDecimal)) {
39                  showDecimal = -1;
40              } else {
41                  showDecimal = Integer.parseInt(numDecimal);
42              }
43          }
44          String dec = getParameter(context, "num.decSign");
45          String neg = getParameter(context, "num.negSign", "-");
46          String sep = getParameter(context, "num.sepSign");
47  
48          if (sep != null && sep.length() > 0) {
49              Pattern pattern = getPattern("[" + buildEscaped(sep) + "]");
50  
51              value = pattern.matcher(value).replaceAll("");
52          }
53  
54          Pattern pattern = getPattern("^(" + buildEscaped(neg) + "?)(\\d*)(["
55                  + buildEscaped(dec) + "]?)(\\d*)$");
56  
57          // Check expression
58          Matcher matcher = pattern.matcher(value);
59  
60          // No match
61          if (matcher.matches() == false) {
62              return null;
63          }
64  
65          String ip = null;
66          String d = null;
67          String dp = null;
68  
69          int groupCount = matcher.groupCount();
70          String n = matcher.group(1);
71          if (groupCount > 1) {
72              ip = matcher.group(2);
73              if (groupCount > 2) {
74                  d = dec.substring(0, 1);
75                  if (groupCount > 3) {
76                      dp = matcher.group(4);
77                  }
78              }
79          }
80  
81          for (; ip.length() > 1 && ip.charAt(0) == '0'; ip = ip.substring(1)) {
82              // Nothing
83          }
84  
85          if (showDecimal == 0) { // Attention au false ou -1
86              d = "";
87              dp = "";
88  
89          } else if (showDecimal > 0) {
90              d = dec.substring(0, 1);
91  
92              if (dp.length() > showDecimal) {
93                  dp = dp.substring(0, showDecimal);
94  
95              } else {
96                  for (; dp.length() < showDecimal;) {
97                      dp += "0";
98                  }
99              }
100 
101         } else if (dp.length() == 0) {
102             // Nombre décimal inconnu, mais pas de décimal
103             d = "";
104         }
105 
106         // Check if no need
107         if (sep == null || sep.length() < 1 || (ip.length() < 4)) {
108             return n + ip + d + dp;
109         }
110 
111         // Traitement des milliers ...
112 
113         // Otherwise format integer part
114 
115         if (ip.length() > 3) {
116             StringAppender sa = new StringAppender(ip);
117 
118             char c = sep.charAt(0);
119 
120             for (int l = ip.length() - 3; l > 0; l -= 3) {
121                 sa.insert(l, c);
122             }
123 
124             ip = sa.toString();
125         }
126 
127         // Rebuild string
128         value = n + ip + d + dp;
129 
130         return value;
131     }
132 }