source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldImpl.java @ 6785

Revision 6785, 3.1 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20package org.apache.james.mime4j.field;
21
22import java.io.StringReader;
23import java.util.Date;
24
25import org.apache.james.mime4j.codec.DecodeMonitor;
26import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
27import org.apache.james.mime4j.field.datetime.parser.ParseException;
28import org.apache.james.mime4j.field.datetime.parser.TokenMgrError;
29import org.apache.james.mime4j.util.ByteSequence;
30
31/**
32 * Date-time field such as <code>Date</code> or <code>Resent-Date</code>.
33 */
34public class DateTimeFieldImpl extends AbstractField implements org.apache.james.mime4j.dom.field.DateTimeField {
35    private boolean parsed = false;
36
37    private Date date;
38    private ParseException parseException;
39
40    DateTimeFieldImpl(String name, String body, ByteSequence raw, DecodeMonitor monitor) {
41        super(name, body, raw, monitor);
42    }
43
44    /**
45     * @see org.apache.james.mime4j.dom.field.DateTimeField#getDate()
46     */
47    public Date getDate() {
48        if (!parsed)
49            parse();
50
51        return date;
52    }
53
54    /**
55     * @see org.apache.james.mime4j.dom.field.DateTimeField#getParseException()
56     */
57    @Override
58    public ParseException getParseException() {
59        if (!parsed)
60            parse();
61
62        return parseException;
63    }
64
65    private void parse() {
66        String body = getBody();
67
68        try {
69            date = new DateTimeParser(new StringReader(body)).parseAll()
70                    .getDate();
71        } catch (ParseException e) {
72            parseException = e;
73        } catch (TokenMgrError e) {
74            parseException = new ParseException(e.getMessage());
75        }
76
77        parsed = true;
78    }
79
80    static final FieldParser<DateTimeFieldImpl> PARSER = new FieldParser<DateTimeFieldImpl>() {
81        public DateTimeFieldImpl parse(final String name, final String body,
82                final ByteSequence raw, DecodeMonitor monitor) {
83            return new DateTimeFieldImpl(name, body, raw, monitor);
84        }
85    };
86}
Note: See TracBrowser for help on using the repository browser.