[6785] | 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 | |
---|
| 20 | package org.apache.james.mime4j.field; |
---|
| 21 | |
---|
| 22 | import java.io.StringReader; |
---|
| 23 | import java.util.Date; |
---|
| 24 | |
---|
| 25 | import org.apache.james.mime4j.codec.DecodeMonitor; |
---|
| 26 | import org.apache.james.mime4j.field.datetime.parser.DateTimeParser; |
---|
| 27 | import org.apache.james.mime4j.field.datetime.parser.ParseException; |
---|
| 28 | import org.apache.james.mime4j.field.datetime.parser.TokenMgrError; |
---|
| 29 | import org.apache.james.mime4j.util.ByteSequence; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Date-time field such as <code>Date</code> or <code>Resent-Date</code>. |
---|
| 33 | */ |
---|
| 34 | public 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 | } |
---|