001package org.apache.maven.scm.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.scm.log.ScmLogger; 023import org.codehaus.plexus.util.StringUtils; 024import org.codehaus.plexus.util.cli.StreamConsumer; 025 026import java.text.DateFormat; 027import java.text.ParseException; 028import java.text.SimpleDateFormat; 029import java.util.Date; 030import java.util.Locale; 031 032/** 033 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> 034 * 035 */ 036public abstract class AbstractConsumer 037 implements StreamConsumer 038{ 039 private ScmLogger logger; 040 041 /** 042 * AbstractConsumer constructor. 043 * 044 * @param logger The logger to use in the consumer 045 */ 046 public AbstractConsumer( ScmLogger logger ) 047 { 048 setLogger( logger ); 049 } 050 051 public ScmLogger getLogger() 052 { 053 return logger; 054 } 055 056 public void setLogger( ScmLogger logger ) 057 { 058 this.logger = logger; 059 } 060 061 /** 062 * Converts the date timestamp from the output into a date object. 063 * 064 * @return A date representing the timestamp of the log entry. 065 */ 066 protected Date parseDate( String date, String userPattern, String defaultPattern ) 067 { 068 return parseDate( date, userPattern, defaultPattern, null ); 069 } 070 071 /** 072 * Converts the date timestamp from the output into a date object. 073 * 074 * @return A date representing the timestamp of the log entry. 075 */ 076 protected Date parseDate( String date, String userPattern, String defaultPattern, Locale locale ) 077 { 078 DateFormat format; 079 080 String patternUsed = null; 081 Locale localeUsed = null; 082 083 if ( StringUtils.isNotEmpty( userPattern ) ) 084 { 085 if (locale != null ) 086 { 087 format = new SimpleDateFormat( userPattern, locale ); 088 localeUsed = locale; 089 } 090 else 091 { 092 format = new SimpleDateFormat( userPattern ); 093 localeUsed = Locale.getDefault(); 094 } 095 patternUsed = userPattern; 096 } 097 else 098 { 099 if ( StringUtils.isNotEmpty( defaultPattern ) ) 100 { 101 if ( locale != null ) 102 { 103 format = new SimpleDateFormat( defaultPattern, locale ); 104 localeUsed = locale; 105 } 106 else 107 { 108 format = new SimpleDateFormat( defaultPattern ); 109 localeUsed = Locale.getDefault(); 110 } 111 patternUsed = defaultPattern; 112 } 113 else 114 { 115 // Use the English short date pattern if no pattern is specified 116 format = DateFormat.getDateInstance( DateFormat.SHORT, Locale.ENGLISH ); 117 patternUsed = "DateFormat.SHORT"; 118 localeUsed = Locale.ENGLISH; 119 } 120 } 121 122 try 123 { 124 return format.parse( date ); 125 } 126 catch ( ParseException e ) 127 { 128 if ( getLogger() != null && getLogger().isWarnEnabled() ) 129 { 130 getLogger().warn( 131 "skip ParseException: " + e.getMessage() + " during parsing date '" + date 132 + "' with pattern '" + patternUsed + "' and locale '" 133 + localeUsed + "'", e ); 134 } 135 136 return null; 137 } 138 } 139}