1 package org.apache.maven.scm.util;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
34 *
35 */
36 public abstract class AbstractConsumer
37 implements StreamConsumer
38 {
39 private ScmLogger logger;
40
41 /**
42 * AbstractConsumer constructor.
43 *
44 * @param logger The logger to use in the consumer
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 * Converts the date timestamp from the output into a date object.
63 *
64 * @return A date representing the timestamp of the log entry.
65 */
66 protected Date parseDate( String date, String userPattern, String defaultPattern )
67 {
68 return parseDate( date, userPattern, defaultPattern, null );
69 }
70
71 /**
72 * Converts the date timestamp from the output into a date object.
73 *
74 * @return A date representing the timestamp of the log entry.
75 */
76 protected Date parseDate( String date, String userPattern, String defaultPattern, Locale locale )
77 {
78 DateFormat format;
79
80 String patternUsed = null;
81 Locale localeUsed = null;
82
83 if ( StringUtils.isNotEmpty( userPattern ) )
84 {
85 if (locale != null )
86 {
87 format = new SimpleDateFormat( userPattern, locale );
88 localeUsed = locale;
89 }
90 else
91 {
92 format = new SimpleDateFormat( userPattern );
93 localeUsed = Locale.getDefault();
94 }
95 patternUsed = userPattern;
96 }
97 else
98 {
99 if ( StringUtils.isNotEmpty( defaultPattern ) )
100 {
101 if ( locale != null )
102 {
103 format = new SimpleDateFormat( defaultPattern, locale );
104 localeUsed = locale;
105 }
106 else
107 {
108 format = new SimpleDateFormat( defaultPattern );
109 localeUsed = Locale.getDefault();
110 }
111 patternUsed = defaultPattern;
112 }
113 else
114 {
115 // Use the English short date pattern if no pattern is specified
116 format = DateFormat.getDateInstance( DateFormat.SHORT, Locale.ENGLISH );
117 patternUsed = "DateFormat.SHORT";
118 localeUsed = Locale.ENGLISH;
119 }
120 }
121
122 try
123 {
124 return format.parse( date );
125 }
126 catch ( ParseException e )
127 {
128 if ( getLogger() != null && getLogger().isWarnEnabled() )
129 {
130 getLogger().warn(
131 "skip ParseException: " + e.getMessage() + " during parsing date '" + date
132 + "' with pattern '" + patternUsed + "' and locale '"
133 + localeUsed + "'", e );
134 }
135
136 return null;
137 }
138 }
139 }