View Javadoc

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          
82          if ( StringUtils.isNotEmpty( userPattern ) )
83          {
84              format = new SimpleDateFormat( userPattern );
85              patternUsed = userPattern;
86          }
87          else
88          {
89              if ( StringUtils.isNotEmpty( defaultPattern ) )
90              {
91                  if ( locale != null )
92                  {
93                      format = new SimpleDateFormat( defaultPattern, locale );
94                  }
95                  else
96                  {
97                      format = new SimpleDateFormat( defaultPattern );
98                  }
99                  patternUsed = defaultPattern;
100             }
101             else
102             {
103                 // Use the English short date pattern if no pattern is specified
104                 format = DateFormat.getDateInstance( DateFormat.SHORT, Locale.ENGLISH );
105                 
106                 patternUsed = " DateFormat.SHORT ";
107             }
108         }
109 
110         try
111         {
112             return format.parse( date );
113         }
114         catch ( ParseException e )
115         {
116             if ( getLogger() != null && getLogger().isWarnEnabled() )
117             {
118                 getLogger().warn(
119                                    "skip ParseException: " + e.getMessage() + " during parsing date " + date
120                                        + " with pattern " + patternUsed + " with Locale "
121                                        + ( locale == null ? Locale.ENGLISH : locale ), e );
122             }
123 
124             return null;
125         }
126     }
127 }