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

Revision 6785, 6.2 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 org.apache.james.mime4j.MimeException;
23import org.apache.james.mime4j.codec.DecodeMonitor;
24import org.apache.james.mime4j.dom.field.FieldName;
25import org.apache.james.mime4j.dom.field.ParsedField;
26import org.apache.james.mime4j.field.AddressListFieldImpl;
27import org.apache.james.mime4j.field.ContentDispositionFieldImpl;
28import org.apache.james.mime4j.field.ContentTransferEncodingFieldImpl;
29import org.apache.james.mime4j.field.ContentTypeFieldImpl;
30import org.apache.james.mime4j.field.DateTimeFieldImpl;
31import org.apache.james.mime4j.field.MailboxFieldImpl;
32import org.apache.james.mime4j.field.MailboxListFieldImpl;
33import org.apache.james.mime4j.field.UnstructuredFieldImpl;
34import org.apache.james.mime4j.stream.RawField;
35import org.apache.james.mime4j.util.ByteSequence;
36import org.apache.james.mime4j.util.ContentUtil;
37
38public class DefaultFieldParser extends DelegatingFieldParser {
39
40    private static final DefaultFieldParser PARSER = new DefaultFieldParser();
41   
42
43    /**
44     * Gets the default parser used to parse fields.
45     *
46     * @return the default field parser
47     */
48    public static DefaultFieldParser getParser() {
49        return PARSER;
50    }
51
52
53    /**
54     * Parses the given byte sequence and returns an instance of the
55     * <code>Field</code> class. The type of the class returned depends on the
56     * field name; see {@link #parse(String)} for a table of field names and
57     * their corresponding classes.
58     *
59     * @param raw the bytes to parse.
60     * @param monitor a DecodeMonitor object used while parsing/decoding.
61     * @return a <code>ParsedField</code> instance.
62     * @throws MimeException if the raw string cannot be split into field name and body.
63     */
64    public static ParsedField parse(
65            final ByteSequence raw,
66            final DecodeMonitor monitor) throws MimeException {
67        RawField rawField = new RawField(raw);
68        return PARSER.parse(rawField.getName(), rawField.getBody(), raw, monitor);
69    }
70
71    /**
72     * Parses the given string and returns an instance of the
73     * <code>Field</code> class. The type of the class returned depends on
74     * the field name:
75     * <p>
76     * <table>
77     *   <tr><th>Class returned</th><th>Field names</th></tr>
78     *   <tr><td>{@link ContentTypeFieldImpl}</td><td>Content-Type</td></tr>
79     *   <tr><td>{@link ContentTransferEncodingFieldImpl}</td><td>Content-Transfer-Encoding</td></tr>
80     *   <tr><td>{@link ContentDispositionFieldImpl}</td><td>Content-Disposition</td></tr>
81     *   <tr><td>{@link DateTimeFieldImpl}</td><td>Date, Resent-Date</td></tr>
82     *   <tr><td>{@link MailboxFieldImpl}</td><td>Sender, Resent-Sender</td></tr>
83     *   <tr><td>{@link MailboxListFieldImpl}</td><td>From, Resent-From</td></tr>
84     *   <tr><td>{@link AddressListFieldImpl}</td><td>To, Cc, Bcc, Reply-To, Resent-To, Resent-Cc, Resent-Bcc</td></tr>
85     *   <tr><td>{@link UnstructuredFieldImpl}</td><td>Subject and others</td></tr>
86     * </table>
87     *
88     * @param rawStr the string to parse.
89     * @return a <code>ParsedField</code> instance.
90     * @throws MimeException if the raw string cannot be split into field name and body.
91     */
92    public static ParsedField parse(
93            final String rawStr,
94            final DecodeMonitor monitor) throws MimeException {
95        ByteSequence raw = ContentUtil.encode(rawStr);
96        return parse(raw, monitor);
97    }
98
99    public static ParsedField parse(final String rawStr) throws MimeException {
100        ByteSequence raw = ContentUtil.encode(rawStr);
101        return parse(raw, DecodeMonitor.SILENT);
102    }
103
104    public DefaultFieldParser() {
105        setFieldParser(FieldName.CONTENT_TRANSFER_ENCODING,
106                ContentTransferEncodingFieldImpl.PARSER);
107        setFieldParser(FieldName.CONTENT_TYPE, ContentTypeFieldImpl.PARSER);
108        setFieldParser(FieldName.CONTENT_DISPOSITION,
109                ContentDispositionFieldImpl.PARSER);
110
111        final FieldParser<DateTimeFieldImpl> dateTimeParser = DateTimeFieldImpl.PARSER;
112        setFieldParser(FieldName.DATE, dateTimeParser);
113        setFieldParser(FieldName.RESENT_DATE, dateTimeParser);
114
115        final FieldParser<MailboxListFieldImpl> mailboxListParser = MailboxListFieldImpl.PARSER;
116        setFieldParser(FieldName.FROM, mailboxListParser);
117        setFieldParser(FieldName.RESENT_FROM, mailboxListParser);
118
119        final FieldParser<MailboxFieldImpl> mailboxParser = MailboxFieldImpl.PARSER;
120        setFieldParser(FieldName.SENDER, mailboxParser);
121        setFieldParser(FieldName.RESENT_SENDER, mailboxParser);
122
123        final FieldParser<AddressListFieldImpl> addressListParser = AddressListFieldImpl.PARSER;
124        setFieldParser(FieldName.TO, addressListParser);
125        setFieldParser(FieldName.RESENT_TO, addressListParser);
126        setFieldParser(FieldName.CC, addressListParser);
127        setFieldParser(FieldName.RESENT_CC, addressListParser);
128        setFieldParser(FieldName.BCC, addressListParser);
129        setFieldParser(FieldName.RESENT_BCC, addressListParser);
130        setFieldParser(FieldName.REPLY_TO, addressListParser);
131    }
132
133}
Note: See TracBrowser for help on using the repository browser.