1 package net.sf.layoutParser.typeHandler;
2
3 import java.text.DecimalFormat;
4 import java.text.ParseException;
5 import java.util.Locale;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8
9 import net.sf.layoutParser.processor.MalformedInputException;
10 import net.sf.layoutParser.processor.MalformedOutputException;
11
12 public abstract class NumberTypeHandler implements TypeHandler<Number> {
13 protected DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat
14 .getInstance(Locale.US);
15
16 public String format(Number data, String format, String filler, int size)
17 throws MalformedOutputException {
18
19 decimalFormat.applyPattern(format != null ? format : simpleFormat(size) );
20 decimalFormat.setDecimalSeparatorAlwaysShown(false);
21
22 Character c = decimalFormat.getDecimalFormatSymbols()
23 .getDecimalSeparator();
24
25 return decimalFormat.format(data).replace(c.toString(), "");
26 }
27
28 public Number parse(String dados, String format, String filler)
29 throws MalformedInputException {
30
31 decimalFormat.applyPattern(format != null ? format : simpleFormat(dados.length()));
32
33 try {
34 return decimalFormat.parse(dados);
35 } catch (ParseException e) {
36 throw new MalformedInputException(e);
37 }
38 }
39
40 private String simpleFormat(int size) {
41 StringBuilder builder = new StringBuilder();
42
43 for (int i = 0; i < size; i++) {
44 builder.append('#');
45 }
46 return builder.toString();
47 }
48 }