001/**
002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
003 * and GZip files. (http://jwat.org/)
004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jwat.warc;
019
020import java.text.DateFormat;
021import java.text.SimpleDateFormat;
022import java.util.Date;
023import java.util.TimeZone;
024
025/**
026 * WARC-Date parser and format validator. The format "yyyy-MM-dd'T'HH:mm:ss'Z'"
027 * is specified in the WARC ISO standard.
028 *
029 * @author lbihanic, selghissassi, nicl
030 */
031@Deprecated
032public final class WarcDateParser {
033
034    /** WARC <code>DateFormat</code> as specified in the WARC ISO standard. */
035    private final DateFormat dateFormat;
036
037    /** Basic <code>DateFormat</code> is not thread safe. */
038    private static final ThreadLocal<WarcDateParser> DateParserTL =
039        new ThreadLocal<WarcDateParser>() {
040        @Override
041        public WarcDateParser initialValue() {
042            return new WarcDateParser();
043        }
044    };
045
046    /**
047     * Creates a new <code>DateParser</code>.
048     */
049    private WarcDateParser() {
050        dateFormat = new SimpleDateFormat(WarcConstants.WARC_DATE_FORMAT);
051        dateFormat.setLenient(false);
052        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
053    }
054
055    /**
056     * Parses a date.
057     * @param dateStr date to parse
058     * @return the formatted date or null if unable to parse date
059     */
060    private Date parseDate(String dateStr) {
061        Date date = null;
062        try {
063            // We subtract 4 from the format because of the ' characters.
064            // These characters are to specify constants in the format string.
065            if ((dateStr != null) && dateStr.length()
066                            == WarcConstants.WARC_DATE_FORMAT.length() - 4) {
067                // Support upper/lower-case.
068                date = dateFormat.parse(dateStr.toUpperCase());
069            }
070        } catch (Exception e) { /* Ignore */ }
071        return date;
072    }
073
074    /**
075     * Parses the date using the format "yyyy-MM-ddTHH:mm:ssZ".
076     * @param dateStr the date to parse
077     * @return the formatted date or <code>null</code> based on whether the date
078     * to parse is compliant with the format "yyyy-MM-ddTHH:mm:ssZ" or not
079     */
080    public static Date getDate(String dateStr) {
081        Date date = DateParserTL.get().parseDate(dateStr);
082        boolean isValid = (date == null) ? false
083                                         : (date.getTime() > 0);
084        return isValid ? date : null;
085    }
086
087    /**
088     * Return a <code>DateFormat</code> object which can be used to string
089     * format WARC dates.
090     * @return <code>DateFormat</code> object which can be used to string
091     * format WARC dates.
092     */
093    public static DateFormat getDateFormat() {
094        return DateParserTL.get().dateFormat;
095    }
096
097}
098