View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.util;
20  
21  import java.text.DateFormat;
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.Date;
25  import java.util.Locale;
26  
27  import org.codehaus.plexus.util.StringUtils;
28  import org.codehaus.plexus.util.cli.StreamConsumer;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
34   *
35   */
36  public abstract class AbstractConsumer implements StreamConsumer {
37      protected final Logger logger = LoggerFactory.getLogger(getClass());
38  
39      /**
40       * Converts the date timestamp from the output into a date object.
41       *
42       * @param date TODO
43       * @param userPattern TODO
44       * @param defaultPattern TODO
45       * @return A date representing the timestamp of the log entry.
46       */
47      protected Date parseDate(String date, String userPattern, String defaultPattern) {
48          return parseDate(date, userPattern, defaultPattern, null);
49      }
50  
51      /**
52       * Converts the date timestamp from the output into a date object.
53       *
54       * @param date TODO
55       * @param userPattern TODO
56       * @param defaultPattern TODO
57       * @param locale TODO
58       * @return A date representing the timestamp of the log entry.
59       */
60      protected Date parseDate(String date, String userPattern, String defaultPattern, Locale locale) {
61          DateFormat format;
62  
63          String patternUsed = null;
64          Locale localeUsed = null;
65  
66          if (StringUtils.isNotEmpty(userPattern)) {
67              if (locale != null) {
68                  format = new SimpleDateFormat(userPattern, locale);
69                  localeUsed = locale;
70              } else {
71                  format = new SimpleDateFormat(userPattern);
72                  localeUsed = Locale.getDefault();
73              }
74              patternUsed = userPattern;
75          } else {
76              if (StringUtils.isNotEmpty(defaultPattern)) {
77                  if (locale != null) {
78                      format = new SimpleDateFormat(defaultPattern, locale);
79                      localeUsed = locale;
80                  } else {
81                      format = new SimpleDateFormat(defaultPattern);
82                      localeUsed = Locale.getDefault();
83                  }
84                  patternUsed = defaultPattern;
85              } else {
86                  // Use the English short date pattern if no pattern is specified
87                  format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
88                  patternUsed = "DateFormat.SHORT";
89                  localeUsed = Locale.ENGLISH;
90              }
91          }
92  
93          try {
94              return format.parse(date);
95          } catch (ParseException e) {
96              if (logger.isWarnEnabled()) {
97                  logger.warn(
98                          "skip ParseException: " + e.getMessage() + " during parsing date '" + date
99                                  + "' with pattern '" + patternUsed + "' and locale '"
100                                 + localeUsed + "'",
101                         e);
102             }
103 
104             return null;
105         }
106     }
107 }