1 package org.apache.maven.scm.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.log.ScmLogger;
23 import org.codehaus.plexus.util.StringUtils;
24 import org.codehaus.plexus.util.cli.StreamConsumer;
25
26 import java.text.DateFormat;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.Locale;
31
32
33
34
35
36 public abstract class AbstractConsumer
37 implements StreamConsumer
38 {
39 private ScmLogger logger;
40
41
42
43
44
45
46 public AbstractConsumer( ScmLogger logger )
47 {
48 setLogger( logger );
49 }
50
51 public ScmLogger getLogger()
52 {
53 return logger;
54 }
55
56 public void setLogger( ScmLogger logger )
57 {
58 this.logger = logger;
59 }
60
61
62
63
64
65
66 protected Date parseDate( String date, String userPattern, String defaultPattern )
67 {
68 return parseDate( date, userPattern, defaultPattern, null );
69 }
70
71
72
73
74
75
76 protected Date parseDate( String date, String userPattern, String defaultPattern, Locale locale )
77 {
78 DateFormat format;
79
80 String patternUsed = null;
81
82 if ( StringUtils.isNotEmpty( userPattern ) )
83 {
84 format = new SimpleDateFormat( userPattern );
85 patternUsed = userPattern;
86 }
87 else
88 {
89 if ( StringUtils.isNotEmpty( defaultPattern ) )
90 {
91 if ( locale != null )
92 {
93 format = new SimpleDateFormat( defaultPattern, locale );
94 }
95 else
96 {
97 format = new SimpleDateFormat( defaultPattern );
98 }
99 patternUsed = defaultPattern;
100 }
101 else
102 {
103
104 format = DateFormat.getDateInstance( DateFormat.SHORT, Locale.ENGLISH );
105
106 patternUsed = " DateFormat.SHORT ";
107 }
108 }
109
110 try
111 {
112 return format.parse( date );
113 }
114 catch ( ParseException e )
115 {
116 if ( getLogger() != null && getLogger().isWarnEnabled() )
117 {
118 getLogger().warn(
119 "skip ParseException: " + e.getMessage() + " during parsing date " + date
120 + " with pattern " + patternUsed + " with Locale "
121 + ( locale == null ? Locale.ENGLISH : locale ), e );
122 }
123
124 return null;
125 }
126 }
127 }