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 * @param date TODO 065 * @param userPattern TODO 066 * @param defaultPattern TODO 067 * @return A date representing the timestamp of the log entry. 068 */ 069 protected Date parseDate( String date, String userPattern, String defaultPattern ) 070 { 071 return parseDate( date, userPattern, defaultPattern, null ); 072 } 073 074 /** 075 * Converts the date timestamp from the output into a date object. 076 * 077 * @param date TODO 078 * @param userPattern TODO 079 * @param defaultPattern TODO 080 * @param locale TODO 081 * @return A date representing the timestamp of the log entry. 082 */ 083 protected Date parseDate( String date, String userPattern, String defaultPattern, Locale locale ) 084 { 085 DateFormat format; 086 087 String patternUsed = null; 088 Locale localeUsed = null; 089 090 if ( StringUtils.isNotEmpty( userPattern ) ) 091 { 092 if ( locale != null ) 093 { 094 format = new SimpleDateFormat( userPattern, locale ); 095 localeUsed = locale; 096 } 097 else 098 { 099 format = new SimpleDateFormat( userPattern ); 100 localeUsed = Locale.getDefault(); 101 } 102 patternUsed = userPattern; 103 } 104 else 105 { 106 if ( StringUtils.isNotEmpty( defaultPattern ) ) 107 { 108 if ( locale != null ) 109 { 110 format = new SimpleDateFormat( defaultPattern, locale ); 111 localeUsed = locale; 112 } 113 else 114 { 115 format = new SimpleDateFormat( defaultPattern ); 116 localeUsed = Locale.getDefault(); 117 } 118 patternUsed = defaultPattern; 119 } 120 else 121 { 122 // Use the English short date pattern if no pattern is specified 123 format = DateFormat.getDateInstance( DateFormat.SHORT, Locale.ENGLISH ); 124 patternUsed = "DateFormat.SHORT"; 125 localeUsed = Locale.ENGLISH; 126 } 127 } 128 129 try 130 { 131 return format.parse( date ); 132 } 133 catch ( ParseException e ) 134 { 135 if ( getLogger() != null && getLogger().isWarnEnabled() ) 136 { 137 getLogger().warn( 138 "skip ParseException: " + e.getMessage() + " during parsing date '" + date 139 + "' with pattern '" + patternUsed + "' and locale '" 140 + localeUsed + "'", e ); 141 } 142 143 return null; 144 } 145 } 146}