View Javadoc

1   package org.apache.maven.plugin.multichanges.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.Locale;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * This class provides a method to convert a date in the changes plugin format
31   * (yyyy-MM-dd) to a date in RSS format (RFC 822).
32   * 
33   */
34  public class DateFormatter {
35  	/**
36  	 * The date format used in changes report file (changes.xml used in
37  	 * maven-changes-plugin).
38  	 */
39  	private final static SimpleDateFormat changesSDF = new SimpleDateFormat(
40  			"yyyy-MM-dd");
41  
42  	/**
43  	 * The date format defined in RFC 822 and used in RSS feeds.
44  	 */
45  	private final static SimpleDateFormat rfc822DF = new SimpleDateFormat(
46  			"EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
47  
48  	/**
49  	 * Log for debug output
50  	 */
51  	private static Log log = LogFactory.getLog(DateFormatter.class);
52  
53  	/**
54  	 * Converts a date in the changes plugin format (yyyy-MM-dd) to a date in
55  	 * RSS format (RFC 822).
56  	 * 
57  	 * @param changesDate
58  	 *            the date to convert
59  	 * @return the date in the RFC 822 format. An empty String if an exception
60  	 *         occurs.
61  	 */
62  	public static final String convertDate(final String changesDate) {
63  		try {
64  			return rfc822DF.format(changesSDF.parse(changesDate));
65  		} catch (ParseException e) {
66  			if (log.isDebugEnabled())
67  				log
68  						.error("Unable to convert the date [" + changesDate
69  								+ "]", e);
70  			else
71  				log.error("Unable to convert the date [" + changesDate + "] : "
72  						+ e.getMessage());
73  			return "";
74  		}
75  	}
76  
77  	/**
78  	 * Test if a String is a date in the changes plugin format (yyyy-MM-dd).
79  	 * 
80  	 * @param aString
81  	 *            the string to test
82  	 * @return true if the String can be parsed as a Date in the format
83  	 *         yyyy-MM-dd.
84  	 */
85  	public static final boolean isChangesDate(final String aString) {
86  		try {
87  			changesSDF.parse(aString);
88  			return true;
89  		} catch (ParseException e) {
90  			return false;
91  		}
92  	}
93  }