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