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