source: contrib/MailArchiver/sources/vendor/mime4j/custom/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java @ 6785

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