|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package net.sf.ffo; |
|
6 |
| |
|
7 |
| import java.io.IOException; |
|
8 |
| import java.io.Writer; |
|
9 |
| import java.text.SimpleDateFormat; |
|
10 |
| import java.util.Calendar; |
|
11 |
| |
|
12 |
| import org.apache.commons.lang.Validate; |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| public class FixedFormatWritter { |
|
21 |
| private final Writer writer; |
|
22 |
| private String defaultCalendarFormat = "yyyyMMdd";; |
|
23 |
| |
|
24 |
18
| public FixedFormatWritter(final Writer writer) {
|
|
25 |
18
| this.writer = writer;
|
|
26 |
| } |
|
27 |
| |
|
28 |
2
| public void write(final Integer val, final int len) throws IOException {
|
|
29 |
2
| Validate.notNull(val);
|
|
30 |
2
| write(""+val, len, '0', true);
|
|
31 |
| } |
|
32 |
| |
|
33 |
0
| public void write(final int val, final int len) throws IOException {
|
|
34 |
0
| write(""+val, len, '0', true);
|
|
35 |
| } |
|
36 |
| |
|
37 |
2
| public void write(final Long val, final int len) throws IOException {
|
|
38 |
2
| Validate.notNull(val);
|
|
39 |
2
| write(""+val, len, '0', true);
|
|
40 |
| } |
|
41 |
| |
|
42 |
1
| public void write(final String val, final int len) throws IOException {
|
|
43 |
1
| write(val, len, ' ', false);
|
|
44 |
| } |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
17
| public void write(final String val, final int len, final char pad, final boolean alignRight) throws IOException {
|
|
56 |
17
| Validate.notNull(val, "String val is null");
|
|
57 |
17
| Validate.isTrue(len>0, "Negative or zero field length");
|
|
58 |
17
| Validate.isTrue(val.length()<=len, "'"+val+"' does not fit a field of "+len+" bytes");
|
|
59 |
| |
|
60 |
17
| final StringBuffer chunk = new StringBuffer(len);
|
|
61 |
| |
|
62 |
17
| for (int i=0;i<len;i++)
|
|
63 |
136
| chunk.append(pad);
|
|
64 |
| |
|
65 |
17
| if (alignRight) {
|
|
66 |
16
| chunk.replace(chunk.length()-val.length(), chunk.length(), val);
|
|
67 |
| } else { |
|
68 |
1
| chunk.replace(0, val.length(), val);
|
|
69 |
| } |
|
70 |
| |
|
71 |
17
| writer.write(chunk.toString());
|
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
0
| public void write(final Calendar cal) throws IOException {
|
|
81 |
0
| internalWrite(defaultCalendarFormat, cal);
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
1
| private void internalWrite(final String fmt, final Calendar cal) throws IOException {
|
|
86 |
1
| Validate.notNull(cal, "Calendar es null");
|
|
87 |
1
| final SimpleDateFormat sdf = new SimpleDateFormat(fmt);
|
|
88 |
1
| final String formated = sdf.format(cal.getTime());
|
|
89 |
1
| writer.write(formated);
|
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
11
| public void write(final Double field, final int integerCount, final int decimalCount) throws IOException {
|
|
103 |
11
| Validate.notNull(field, "field to write is null");
|
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
11
| final String fmt = "%.0"+decimalCount+"f";
|
|
108 |
11
| System.out.println("fmt: "+fmt);
|
|
109 |
| |
|
110 |
11
| final PrintfFormat pfmt = new PrintfFormat(fmt);
|
|
111 |
11
| final String formatted = pfmt.sprintf(field.doubleValue());
|
|
112 |
| |
|
113 |
| |
|
114 |
11
| final StringBuffer sb = new StringBuffer(formatted);
|
|
115 |
11
| if (formatted.indexOf('.')>=0) {
|
|
116 |
11
| sb.deleteCharAt(formatted.indexOf('.'));
|
|
117 |
| } |
|
118 |
11
| write(sb.toString(), decimalCount+integerCount, '0', true);
|
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
1
| public void write(final String fmt, final Calendar cal) throws IOException {
|
|
130 |
1
| internalWrite(fmt, cal);
|
|
131 |
| } |
|
132 |
| |
|
133 |
0
| public Writer getWriter() {
|
|
134 |
0
| return this.writer;
|
|
135 |
| } |
|
136 |
| |
|
137 |
| } |