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 ****************************************************************/
019
020package org.apache.james.mime4j.field;
021
022import java.io.StringReader;
023import java.util.Date;
024
025import org.apache.james.mime4j.codec.DecodeMonitor;
026import org.apache.james.mime4j.dom.FieldParser;
027import org.apache.james.mime4j.dom.field.DateTimeField;
028import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
029import org.apache.james.mime4j.field.datetime.parser.ParseException;
030import org.apache.james.mime4j.field.datetime.parser.TokenMgrError;
031import org.apache.james.mime4j.stream.Field;
032
033/**
034 * Date-time field such as <code>Date</code> or <code>Resent-Date</code>.
035 */
036public class DateTimeFieldImpl extends AbstractField implements DateTimeField {
037    private boolean parsed = false;
038
039    private Date date;
040    private ParseException parseException;
041
042    DateTimeFieldImpl(Field rawField, DecodeMonitor monitor) {
043        super(rawField, monitor);
044    }
045
046    /**
047     * @see org.apache.james.mime4j.dom.field.DateTimeField#getDate()
048     */
049    public Date getDate() {
050        if (!parsed)
051            parse();
052
053        return date;
054    }
055
056    /**
057     * @see org.apache.james.mime4j.dom.field.DateTimeField#getParseException()
058     */
059    @Override
060    public ParseException getParseException() {
061        if (!parsed)
062            parse();
063
064        return parseException;
065    }
066
067    private void parse() {
068        String body = getBody();
069
070        try {
071            date = new DateTimeParser(new StringReader(body)).parseAll()
072                    .getDate();
073        } catch (ParseException e) {
074            parseException = e;
075        } catch (TokenMgrError e) {
076            parseException = new ParseException(e.getMessage());
077        }
078
079        parsed = true;
080    }
081
082    public static final FieldParser<DateTimeField> PARSER = new FieldParser<DateTimeField>() {
083
084        public DateTimeField parse(final Field rawField, final DecodeMonitor monitor) {
085            return new DateTimeFieldImpl(rawField, monitor);
086        }
087
088    };
089}