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 * @param date TODO
65 * @param userPattern TODO
66 * @param defaultPattern TODO
67 * @return A date representing the timestamp of the log entry.
68 */
69 protected Date parseDate( String date, String userPattern, String defaultPattern )
70 {
71 return parseDate( date, userPattern, defaultPattern, null );
72 }
73
74 /**
75 * Converts the date timestamp from the output into a date object.
76 *
77 * @param date TODO
78 * @param userPattern TODO
79 * @param defaultPattern TODO
80 * @param locale TODO
81 * @return A date representing the timestamp of the log entry.
82 */
83 protected Date parseDate( String date, String userPattern, String defaultPattern, Locale locale )
84 {
85 DateFormat format;
86
87 String patternUsed = null;
88 Locale localeUsed = null;
89
90 if ( StringUtils.isNotEmpty( userPattern ) )
91 {
92 if ( locale != null )
93 {
94 format = new SimpleDateFormat( userPattern, locale );
95 localeUsed = locale;
96 }
97 else
98 {
99 format = new SimpleDateFormat( userPattern );
100 localeUsed = Locale.getDefault();
101 }
102 patternUsed = userPattern;
103 }
104 else
105 {
106 if ( StringUtils.isNotEmpty( defaultPattern ) )
107 {
108 if ( locale != null )
109 {
110 format = new SimpleDateFormat( defaultPattern, locale );
111 localeUsed = locale;
112 }
113 else
114 {
115 format = new SimpleDateFormat( defaultPattern );
116 localeUsed = Locale.getDefault();
117 }
118 patternUsed = defaultPattern;
119 }
120 else
121 {
122 // Use the English short date pattern if no pattern is specified
123 format = DateFormat.getDateInstance( DateFormat.SHORT, Locale.ENGLISH );
124 patternUsed = "DateFormat.SHORT";
125 localeUsed = Locale.ENGLISH;
126 }
127 }
128
129 try
130 {
131 return format.parse( date );
132 }
133 catch ( ParseException e )
134 {
135 if ( getLogger() != null && getLogger().isWarnEnabled() )
136 {
137 getLogger().warn(
138 "skip ParseException: " + e.getMessage() + " during parsing date '" + date
139 + "' with pattern '" + patternUsed + "' and locale '"
140 + localeUsed + "'", e );
141 }
142
143 return null;
144 }
145 }
146 }