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.cli.StreamConsumer;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
33   */
34  public abstract class AbstractConsumer implements StreamConsumer {
35      protected final Logger logger = LoggerFactory.getLogger(getClass());
36  
37      /**
38       * Converts the date timestamp from the output into a date object.
39       *
40       * @param date TODO
41       * @param userPattern TODO
42       * @param defaultPattern TODO
43       * @return a date representing the timestamp of the log entry
44       */
45      protected Date parseDate(String date, String userPattern, String defaultPattern) {
46          return parseDate(date, userPattern, defaultPattern, null);
47      }
48  
49      /**
50       * Converts the date timestamp from the output into a date object.
51       *
52       * @param date TODO
53       * @param userPattern TODO
54       * @param defaultPattern TODO
55       * @param locale TODO
56       * @return a date representing the timestamp of the log entry
57       */
58      protected Date parseDate(String date, String userPattern, String defaultPattern, Locale locale) {
59          DateFormat format;
60  
61          String patternUsed = null;
62          Locale localeUsed = null;
63  
64          if (userPattern != null && !userPattern.isEmpty()) {
65              if (locale != null) {
66                  format = new SimpleDateFormat(userPattern, locale);
67                  localeUsed = locale;
68              } else {
69                  format = new SimpleDateFormat(userPattern);
70                  localeUsed = Locale.getDefault();
71              }
72              patternUsed = userPattern;
73          } else {
74              if (defaultPattern != null && !defaultPattern.isEmpty()) {
75                  if (locale != null) {
76                      format = new SimpleDateFormat(defaultPattern, locale);
77                      localeUsed = locale;
78                  } else {
79                      format = new SimpleDateFormat(defaultPattern);
80                      localeUsed = Locale.getDefault();
81                  }
82                  patternUsed = defaultPattern;
83              } else {
84                  // Use the English short date pattern if no pattern is specified
85                  format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
86                  patternUsed = "DateFormat.SHORT";
87                  localeUsed = Locale.ENGLISH;
88              }
89          }
90  
91          try {
92              return format.parse(date);
93          } catch (ParseException e) {
94              if (logger.isWarnEnabled()) {
95                  logger.warn(
96                          "skip ParseException: " + e.getMessage() + " during parsing date '" + date
97                                  + "' with pattern '" + patternUsed + "' and locale '"
98                                  + localeUsed + "'",
99                          e);
100             }
101 
102             return null;
103         }
104     }
105 }