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

Revision 6785, 6.9 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.Collections;
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27
28import org.apache.james.mime4j.codec.DecodeMonitor;
29import org.apache.james.mime4j.dom.field.ContentTypeField;
30import org.apache.james.mime4j.field.contenttype.parser.ContentTypeParser;
31import org.apache.james.mime4j.field.contenttype.parser.ParseException;
32import org.apache.james.mime4j.field.contenttype.parser.TokenMgrError;
33import org.apache.james.mime4j.util.ByteSequence;
34
35/**
36 * Represents a <code>Content-Type</code> field.
37 */
38public class ContentTypeFieldImpl extends AbstractField implements ContentTypeField {
39    private boolean parsed = false;
40
41    private String mimeType = "";
42    private Map<String, String> parameters = new HashMap<String, String>();
43    private ParseException parseException;
44
45    ContentTypeFieldImpl(String name, String body, ByteSequence raw, DecodeMonitor monitor) {
46        super(name, body, raw, monitor);
47    }
48
49    /**
50     * @see org.apache.james.mime4j.dom.field.ContentTypeField#getParseException()
51     */
52    @Override
53    public ParseException getParseException() {
54        if (!parsed)
55            parse();
56
57        return parseException;
58    }
59
60    /**
61     * @see org.apache.james.mime4j.dom.field.ContentTypeField#getMimeType()
62     */
63    public String getMimeType() {
64        if (!parsed)
65            parse();
66
67        return mimeType;
68    }
69
70    /**
71     * @see org.apache.james.mime4j.dom.field.ContentTypeField#getParameter(java.lang.String)
72     */
73    public String getParameter(String name) {
74        if (!parsed)
75            parse();
76
77        return parameters.get(name.toLowerCase());
78    }
79
80    /**
81     * @see org.apache.james.mime4j.dom.field.ContentTypeField#getParameters()
82     */
83    public Map<String, String> getParameters() {
84        if (!parsed)
85            parse();
86
87        return Collections.unmodifiableMap(parameters);
88    }
89
90    /**
91     * @see org.apache.james.mime4j.dom.field.ContentTypeField#isMimeType(java.lang.String)
92     */
93    public boolean isMimeType(String mimeType) {
94        if (!parsed)
95            parse();
96
97        return this.mimeType.equalsIgnoreCase(mimeType);
98    }
99
100    /**
101     * @see org.apache.james.mime4j.dom.field.ContentTypeField#isMultipart()
102     */
103    public boolean isMultipart() {
104        if (!parsed)
105            parse();
106
107        return mimeType.startsWith(TYPE_MULTIPART_PREFIX);
108    }
109
110    /**
111     * @see org.apache.james.mime4j.dom.field.ContentTypeField#getBoundary()
112     */
113    public String getBoundary() {
114        return getParameter(PARAM_BOUNDARY);
115    }
116
117    /**
118     * @see org.apache.james.mime4j.dom.field.ContentTypeField#getCharset()
119     */
120    public String getCharset() {
121        return getParameter(PARAM_CHARSET);
122    }
123
124    /**
125     * Gets the MIME type defined in the child's Content-Type field or derives a
126     * MIME type from the parent if child is <code>null</code> or hasn't got a
127     * MIME type value set. If child's MIME type is multipart but no boundary
128     * has been set the MIME type of child will be derived from the parent.
129     *
130     * @param child
131     *            the child.
132     * @param parent
133     *            the parent.
134     * @return the MIME type.
135     */
136    public static String getMimeType(ContentTypeField child,
137            ContentTypeField parent) {
138        if (child == null || child.getMimeType().length() == 0
139                || child.isMultipart() && child.getBoundary() == null) {
140
141            if (parent != null && parent.isMimeType(TYPE_MULTIPART_DIGEST)) {
142                return TYPE_MESSAGE_RFC822;
143            } else {
144                return TYPE_TEXT_PLAIN;
145            }
146        }
147
148        return child.getMimeType();
149    }
150
151    /**
152     * Gets the value of the <code>charset</code> parameter if set for the
153     * given field. Returns the default <code>us-ascii</code> if not set or if
154     * <code>f</code> is <code>null</code>.
155     *
156     * @return the <code>charset</code> parameter value.
157     */
158    public static String getCharset(ContentTypeField f) {
159        if (f != null) {
160            String charset = f.getCharset();
161            if (charset != null && charset.length() > 0) {
162                return charset;
163            }
164        }
165        return "us-ascii";
166    }
167
168    private void parse() {
169        String body = getBody();
170
171        ContentTypeParser parser = new ContentTypeParser(new StringReader(body));
172        try {
173            parser.parseAll();
174        } catch (ParseException e) {
175            parseException = e;
176        } catch (TokenMgrError e) {
177            parseException = new ParseException(e.getMessage());
178        }
179
180        final String type = parser.getType();
181        final String subType = parser.getSubType();
182
183        if (type != null && subType != null) {
184            mimeType = (type + "/" + subType).toLowerCase();
185
186            List<String> paramNames = parser.getParamNames();
187            List<String> paramValues = parser.getParamValues();
188
189            if (paramNames != null && paramValues != null) {
190                final int len = Math.min(paramNames.size(), paramValues.size());
191                for (int i = 0; i < len; i++) {
192                    String paramName = paramNames.get(i).toLowerCase();
193                    String paramValue = paramValues.get(i);
194                    parameters.put(paramName, paramValue);
195                }
196            }
197        }
198
199        parsed = true;
200    }
201
202    static final FieldParser<ContentTypeFieldImpl> PARSER = new FieldParser<ContentTypeFieldImpl>() {
203        public ContentTypeFieldImpl parse(final String name, final String body,
204                final ByteSequence raw, DecodeMonitor monitor) {
205            return new ContentTypeFieldImpl(name, body, raw, monitor);
206        }
207    };
208}
Note: See TracBrowser for help on using the repository browser.