ContentTransferEncodingField.java revision bc47398187c6ffd132435e51d8d61e6ec79a79db
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
22
23
24/**
25 * Represents a <code>Content-Transfer-Encoding</code> field.
26 *
27 *
28 * @version $Id: ContentTransferEncodingField.java,v 1.2 2004/10/02 12:41:11 ntherning Exp $
29 */
30public class ContentTransferEncodingField extends Field {
31    /**
32     * The <code>7bit</code> encoding.
33     */
34    public static final String ENC_7BIT = "7bit";
35    /**
36     * The <code>8bit</code> encoding.
37     */
38    public static final String ENC_8BIT = "8bit";
39    /**
40     * The <code>binary</code> encoding.
41     */
42    public static final String ENC_BINARY = "binary";
43    /**
44     * The <code>quoted-printable</code> encoding.
45     */
46    public static final String ENC_QUOTED_PRINTABLE = "quoted-printable";
47    /**
48     * The <code>base64</code> encoding.
49     */
50    public static final String ENC_BASE64 = "base64";
51
52    private String encoding;
53
54    protected ContentTransferEncodingField(String name, String body, String raw, String encoding) {
55        super(name, body, raw);
56        this.encoding = encoding;
57    }
58
59    /**
60     * Gets the encoding defined in this field.
61     *
62     * @return the encoding or an empty string if not set.
63     */
64    public String getEncoding() {
65        return encoding;
66    }
67
68    /**
69     * Gets the encoding of the given field if. Returns the default
70     * <code>7bit</code> if not set or if
71     * <code>f</code> is <code>null</code>.
72     *
73     * @return the encoding.
74     */
75    public static String getEncoding(ContentTransferEncodingField f) {
76        if (f != null && f.getEncoding().length() != 0) {
77            return f.getEncoding();
78        }
79        return ENC_7BIT;
80    }
81
82    public static class Parser implements FieldParser {
83        public Field parse(final String name, final String body, final String raw) {
84            final String encoding = body.trim().toLowerCase();
85            return new ContentTransferEncodingField(name, body, raw, encoding);
86        }
87    }
88}
89