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