1 package com.instantbank.component.lettertemplate.util;
2
3 import java.util.Hashtable;
4 import java.text.NumberFormat;
5 import java.text.DecimalFormat;
6 import java.text.ParseException;
7 import java.text.DateFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.Locale;
10 import java.util.Date;
11 import com.instantbank.common.utilcomponents.Debug;
12 import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
13
14
20 public class VariablesFormat {
21
24 Locale uslocale = Locale.US;
25
26
27
28
31 public static final int N9999 = 2;
32
35 public static final int NZZZ9 = 10;
36
39 public static final int NZcZZ9 = 11;
40
43 public static final int NZZZ9_ = 12;
44
47 public static final int NZcZZ9_ = 13;
48
51 public static final int NZcZZ9p99_ = 14;
52
55 public static final int NZZ9p999pc = 15;
56
59 public static final int NZZ9p9999pc = 16;
60
63 public static final int N9999p99 = 17;
64
67 public static final int NZZZ9p99 = 18;
68
71 public static final int N9c999 = 19;
72
75 public static final int N9c999p99 = 20;
76
79 public static final int NZcZZ9p99 = 21;
80
83 public static final int Nd9c999p99 = 22;
84
87 public static final int NdZcZZ9p99 = 23;
88
91 public static final int NSSN = 24;
92
95 public static final int NPhone = 25;
96
97
100
101 public static final int[] numberFormatCode = {
102 N9999, NZZZ9, NZcZZ9, NZZZ9_, NZcZZ9_, NZcZZ9p99_,
103 NZZ9p999pc, NZZ9p9999pc,
104 N9999p99, NZZZ9p99, N9c999, N9c999p99, NZcZZ9p99,
105 Nd9c999p99, NdZcZZ9p99, NSSN, NPhone
106 };
107
108
109
110
113 public static final int MMsDDsYY = 3;
114
117 public static final int MMsDDsYYYY = 4;
118
121 public static final int YYYYsMMsDD = 5;
122
125 public static final int DD_MMM_YYYY = 6;
126
129 public static final int MMMwDDcwYY = 7;
130
133 public static final int MMMwDDcwYYYY = 8;
134
137 public static final int MMMcwYYYY = 9;
138
139
142 public static final int[] dateFormatCode = {
143 MMsDDsYY, MMsDDsYYYY, YYYYsMMsDD, DD_MMM_YYYY,
144 MMMwDDcwYY, MMMwDDcwYYYY, MMMcwYYYY
145 };
146
147
150 public static final String[] dateFormatPattern = {
151 "MM/dd/yy", "MM/dd/yyyy", "yyyy/MM,dd", "dd-MMM-yyyy",
152 "MMM dd, yy", "MMM dd, yyyy", "MMM, yyyy"
153 };
154
155
159 Hashtable dateFormatter = new Hashtable(6);
160
164 Hashtable numberFormatter = new Hashtable(15);
165
166
169 NumberFormat numberParser;
170
173 SimpleDateFormat dateParser;
174
175 Debug debug;
176
177
178
183 public VariablesFormat() {
184
185 debug = new Debug();
186 debug.setDebugginOn(true);
187 debug.setPreMessage("** VariablesFormat: ");
188
189 numberParser = NumberFormat.getInstance(uslocale);
190
191 for(int i = 0; i < numberFormatCode.length; i++) {
192 int formatCode = numberFormatCode[i];
193 numberFormatter.put(
194 new Long(formatCode), buildNumberFormatter(formatCode));
195 }
196
197 dateParser =
198 new SimpleDateFormat(LetterTemplateGlobals.DATE_PARSING_PATTERN, uslocale);
199
200
201 for(int i = 0; i < dateFormatCode.length; i++) {
202 long formatCode = dateFormatCode[i];
203 dateFormatter.put(
204 new Long(formatCode),
205 new SimpleDateFormat(dateFormatPattern[i]));
206 }
207
208 }
209
210
211
218 private DecimalFormat baseFormatter(int formatNumber, String pattern) {
219 NumberFormat nf = NumberFormat.getInstance(uslocale);
220 DecimalFormat df;
221 df = (DecimalFormat)nf;
222 df.setMinimumFractionDigits(5);
223 df.setMaximumFractionDigits(5);
224 df.setDecimalSeparatorAlwaysShown(true);
225 df.applyPattern(pattern);
226 return df;
227 }
228
229
230
237 private IbNumberFormat buildNumberFormatter(int formatNumber) {
238
239 NumberFormat nf;
240 DecimalFormat df;
241 String pattern;
242 IbNumberFormat ib;
243 switch (formatNumber) {
244
245 case N9999:
246
247
248 df = baseFormatter(formatNumber, "0000.00000");
249 if(df == null) {
250 return null;
251 }
252 return new IbNumberFormat(df, new Rounder(), "", "", true);
253 case NZZZ9:
254
255
256 df = baseFormatter(formatNumber, "###0.00000");
257 if(df == null) {
258 return null;
259 }
260 return new IbNumberFormat(df, new Rounder(), "", "", true);
261 case NZcZZ9:
262
263
264 df = baseFormatter(formatNumber, "#,##0.00000");
265 if(df == null) {
266 return null;
267 }
268 return new IbNumberFormat(df, new Rounder(), "", "", true);
269 case NZZZ9_:
270
271
272 df = baseFormatter(formatNumber, "###0.00000");
273 if(df == null) {
274 return null;
275 }
276 df.setNegativePrefix("");
277 df.setNegativeSuffix("-");
278 return new IbNumberFormat(df, new Rounder(), "-", "", true);
279 case NZcZZ9_:
280
281
282 df = baseFormatter(formatNumber, "#,##0.00000");
283 if(df == null) {
284 return null;
285 }
286 return new IbNumberFormat(df, new Rounder(), "-", "", true);
287 case NZcZZ9p99_:
288
289
290 df = baseFormatter(formatNumber, "#,##0.00000");
291 if(df == null) {
292 return null;
293 }
294 return new IbNumberFormat(df, new Rounder(2), "-", "", false);
295 case NZZ9p999pc:
296
297
298 df = baseFormatter(formatNumber, "##0.00000");
299 if(df == null) {
300 return null;
301 }
302 ib = new IbNumberFormat(df, new Rounder(3, 3), "%", "", false);
303 ib.setRightPositive("%");
304 return ib;
305 case NZZ9p9999pc:
306
307
308 df = baseFormatter(formatNumber, "##0.00000");
309 if(df == null) {
310 return null;
311 }
312
313 ib = new IbNumberFormat(df, new Rounder(3, 4), "%", "", false);
314 ib.setRightPositive("%");
315 return ib;
316 case N9999p99:
317
318
319 df = baseFormatter(formatNumber, "0000.00000");
320 if(df == null) {
321 return null;
322 }
323 return new IbNumberFormat(df, new Rounder(2), "", "", false);
324 case NZZZ9p99:
325
326
327 df = baseFormatter(formatNumber, "###0.00000");
328 if(df == null) {
329 return null;
330 }
331 return new IbNumberFormat(df, new Rounder(2), "", "", false);
332 case N9c999:
333
334
335 df = baseFormatter(formatNumber, "0,000.00000");
336 if(df == null) {
337 return null;
338 }
339 return new IbNumberFormat(df, new Rounder(), "", "", true);
340 case N9c999p99:
341
342
343 df = baseFormatter(formatNumber, "0,000.00000");
344 if(df == null) {
345 return null;
346 }
347 return new IbNumberFormat(df, new Rounder(2), "", "", false);
348 case NZcZZ9p99:
349
350
351 df = baseFormatter(formatNumber, "#,##0.00000");
352 if(df == null) {
353 return null;
354 }
355 return new IbNumberFormat(df, new Rounder(2), "", "", false);
356 case Nd9c999p99:
357
358 df = baseFormatter(formatNumber, "0,000.00000");
359 if(df == null) {
360 return null;
361 }
362 return new IbNumberFormat(df, new Rounder(2), "", "$", false);
363 case NdZcZZ9p99:
364
365 df = baseFormatter(formatNumber, "#,##0.00000");
366 if(df == null) {
367 return null;
368 }
369 return new IbNumberFormat(df, new Rounder(2), "", "$", false);
370 case NSSN:
371
372 df = baseFormatter(formatNumber, "000000000");
373 return new IbNumberFormat(df, new SSN_MakeUp(), "", "", true);
374 case NPhone:
375
376 df = baseFormatter(formatNumber, "0000000000");
377 return new IbNumberFormat(df, new Phone_MakeUp(), "", "", true);
378 default:
379 return null;
380 }
381
382 }
383
384
385
394 public String format(String varValue, Long fmtCode) {
395
396 if(varValue.trim().equals("")) {
397 return varValue;
398 }
399
400 DateFormat df = (DateFormat)dateFormatter.get(fmtCode);
401
402 if(df != null) {
403
404 try {
405 Date d = dateParser.parse(varValue);
406 return df.format(d);
407 }
408 catch(ParseException ex) {
409 debug.println(ex.toString());
410 return "----/--/--";
411 }
412 }
413
414 IbNumberFormat ibf = (IbNumberFormat)numberFormatter.get(fmtCode);
415 if(ibf != null) {
416
417 try {
418 Number num = numberParser.parse(varValue);
419 if(num.getClass() == Long.class) {
420 return ibf.format(num.longValue());
421 }
422 else if(num.getClass() == Double.class) {
423 return ibf.format(num.doubleValue());
424 }
425 else {
426 return "#########";
427 }
428 }
429 catch(ParseException ex) {
430 debug.println(ex.toString());
431 return "##########";
432 }
433 }
434
435 return varValue;
436 }
437
438
439
442 class IbNumberFormat {
443
447 NumberFormat formatter;
448
452 MakeUpFormat rounder;
453 boolean trimPeriod = false;
454
455
458 String rightNegative = "";
459
462 String leftPositive = "";
463
466 String rightPositive = "";
467
470 String leftNegative = "";
471
472
473
479 public IbNumberFormat(NumberFormat numFormat, MakeUpFormat rounder) {
480 formatter = numFormat;
481 rounder = rounder;
482 }
483
484
485
494 public IbNumberFormat(NumberFormat numFormat, MakeUpFormat rounder, String rNeg,
495 String lPos, boolean trimPeriod) {
496 this.formatter = numFormat;
497 this.rounder = rounder;
498 this.rightNegative = rNeg;
499 this.leftPositive = lPos;
500 this.trimPeriod = trimPeriod;
501 }
502
503
504
509 public void setLeftPositive(String s) {
510 leftPositive = s;
511 }
512
513
514
519 public void setLeftNegative(String s) {
520 leftNegative = s;
521 }
522
523
524
529 public void setRightPositive(String s) {
530 rightPositive = s;
531 }
532
533
534
539 public void setRightNegative(String s) {
540 rightNegative = s;
541 }
542
543
544
550 public String format(long lval) {
551 long tmp = Math.abs(lval);
552 String s = formatter.format(tmp);
553 System.out.println(s);
554 if(rounder != null) {
555 if(trimPeriod) {
556 s = rounder.trimPeriod(s);
557 }
558 else {
559 s = rounder.trim(s);
560 }
561 }
562
563 if(lval < 0) {
564 return leftNegative + s + rightNegative;
565 }
566 else {
567 return leftPositive + s + rightPositive;
568 }
569 }
570
571
572
578
579 public String format(double dval) {
580 double tmp = Math.abs(dval);
581 String s = formatter.format(tmp);
582 System.out.println(s);
583 if(rounder != null) {
584 if(trimPeriod) {
585 s = rounder.trimPeriod(s);
586 }
587 else {
588 s = rounder.trim(s);
589 }
590 }
591
592 if(dval < 0.0) {
593 return leftPositive + s + rightNegative;
594 }
595 else {
596 return leftPositive + s + rightPositive;
597 }
598 }
599 }
600
601
602
605 interface MakeUpFormat {
606
613 public String trim(String s);
614
615
616
622 public String trimPeriod(String s);
623 }
624
625
626
630 class Rounder
631 implements MakeUpFormat {
632
634 int left;
635 int right;
636 boolean leftTrimmer;
637
638
639
642 public Rounder() {
643 left = right = 0;
644 leftTrimmer = false;
645 }
646
647
648
653 public Rounder(int right) {
654 left = 0;
655 this.right = right;
656 leftTrimmer = false;
657 }
658
659
660
667 public Rounder(int left, int right) {
668 this.left = left;
669 this.right = right;
670 leftTrimmer = true;
671 }
672
673
674
680 public String trim(String s) {
681 int period = s.indexOf(".");
682 int end = s.length();
683 int start = 0;
684
685 if(period >= 0) {
686 if(period + right + 1 <= s.length()) {
687 end = period + right + 1;
688 }
689 if(leftTrimmer && period - left >= 0) {
690 start = period - left;
691 }
692 }
693 else if(leftTrimmer && s.length() - left >= 0) {
694 start = s.length() - left;
695 }
696 return s.substring(start, end);
697 }
698
699
700
706 public String trimPeriod(String s) {
707 int period = s.indexOf(".");
708 if(period >= 0) {
709 return trim(s.substring(0, period));
710 }
711 else {
712 return trim(s);
713 }
714 }
715 }
716
717
718
721 class SSN_MakeUp extends Rounder {
722
725 public SSN_MakeUp() {
726 super(9, 0);
727 }
728
729
730
736 public String trim(String s) {
737 StringBuffer bf = new StringBuffer(super.trim(s));
738 bf.insert(5, '-');
739 bf.insert(3, '-');
740 return bf.toString();
741 }
742 }
743
744
745
748 class Phone_MakeUp extends Rounder {
749
752 public Phone_MakeUp() {
753 super(10, 0);
754 }
755
756
757
763 public String trim(String s) {
764 StringBuffer bf = new StringBuffer(super.trim(s));
765 bf.insert(6, '-');
766 bf.insert(3, ')');
767 bf.insert(0, '(');
768 return bf.toString();
769 }
770 }
771 }
772
773