01: package org.andromda.utils;
02:
03: import org.apache.commons.lang.time.DateUtils;
04:
05: /**
06: * Provides additional methods supporting various date-related features
07: */
08: public class DateUtilsHelper extends DateUtils {
09: // order is important !
10: private static final FormatPattern[] JAVA2PERL_FORMAT_PATTERNS = {
11: new FormatPattern("y{4,}", "%Y"),
12: new FormatPattern("y{1,3}", "%y"),
13: new FormatPattern("M{4,}", "%B"),
14: new FormatPattern("M{3}", "%b"),
15: new FormatPattern("M{1,2}", "%m"),
16: new FormatPattern("d{4,}", "%A"),
17: new FormatPattern("d{3,}", "%a"),
18: new FormatPattern("d{2}", "%d"),
19: new FormatPattern("d{1}", "%e"),
20: new FormatPattern("E{4,}", "%A"),
21: new FormatPattern("E{1,3}", "%a"),
22: new FormatPattern("H{2,}", "%H"),
23: new FormatPattern("H{1}", "%k"),
24: new FormatPattern("h{2,}", "%I"),
25: new FormatPattern("h{1}", "%l"),
26: new FormatPattern("D{3}", "%j"),
27: new FormatPattern("s{2,}", "%S"),
28: new FormatPattern("s{1}", "%s"),
29: new FormatPattern("m{2,}", "%M") };
30:
31: /**
32: * Converts a Java SimpleDateFormat into an equivalent String suited for dates used on Perl/PHP platforms.
33: */
34: public static String formatJavaToPerl(String javaFormat) {
35: String perlFormat = null;
36:
37: if (javaFormat != null) {
38: perlFormat = javaFormat;
39:
40: // this implementation is quite rough, and not at all performant, but it works for now
41: // @todo (wouter): re-implement
42: for (int i = 0; i < JAVA2PERL_FORMAT_PATTERNS.length; i++) {
43: final FormatPattern formatPattern = JAVA2PERL_FORMAT_PATTERNS[i];
44: perlFormat = perlFormat.replaceAll(formatPattern
45: .getPattern(), "%%%" + i + "%%%");
46: }
47:
48: for (int i = 0; i < JAVA2PERL_FORMAT_PATTERNS.length; i++) {
49: final FormatPattern formatPattern = JAVA2PERL_FORMAT_PATTERNS[i];
50: perlFormat = perlFormat.replaceAll("%%%" + i + "%%%",
51: formatPattern.getReplacement());
52: }
53: }
54:
55: return perlFormat;
56: }
57:
58: private static final String[] PERL_TIME_FORMATS = new String[] {
59: "%H", "%I", "%k", "%l", "%p", "%P", "%s", "%S" };
60:
61: /**
62: * Checks whether a perl formatted date contains information about displaying time.
63: */
64: public static boolean containsTimeFormat(String perlFormat) {
65: boolean containsTimeFormat = false;
66:
67: for (int i = 0; i < PERL_TIME_FORMATS.length
68: && !containsTimeFormat; i++) {
69: String timeFormatPattern = PERL_TIME_FORMATS[i];
70: containsTimeFormat = perlFormat.indexOf(timeFormatPattern) > -1;
71: }
72:
73: return containsTimeFormat;
74: }
75:
76: private static final class FormatPattern {
77: private final String pattern;
78: private final String replacement;
79:
80: public FormatPattern(final String formatPattern,
81: final String replacement) {
82: this .pattern = formatPattern;
83: this .replacement = replacement;
84: }
85:
86: public String getPattern() {
87: return pattern;
88: }
89:
90: public String getReplacement() {
91: return replacement;
92: }
93: }
94: }
|