Better to include name as well as number
[mime4j.git] / src / main / java / org / apache / james / mime4j / MaximalBodyDescriptor.java
blobbcb0f9ca516e3123cf4a55f857337c6028d60eaf
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.apache.james.mime4j;
21 import java.io.StringReader;
23 import org.apache.james.mime4j.field.mimeversion.MimeVersionParser;
24 import org.apache.james.mime4j.util.MimeUtil;
27 /**
28 * Parses and stores values for standard MIME header values.
31 public class MaximalBodyDescriptor extends DefaultBodyDescriptor implements RFC2045MimeDescriptor {
33 private static final int DEFAULT_MINOR_VERSION = 0;
34 private static final int DEFAULT_MAJOR_VERSION = 1;
35 private boolean isMimeVersionSet;
36 private int mimeMinorVersion;
37 private int mimeMajorVersion;
38 private MimeException mimeVersionException;
39 private String contentId;
40 private boolean isContentIdSet;
41 private String contentDescription;
42 private boolean isContentDescriptionSet;
44 protected MaximalBodyDescriptor() {
45 this(null);
48 protected MaximalBodyDescriptor(BodyDescriptor parent) {
49 super(parent);
50 isMimeVersionSet = false;
51 mimeMajorVersion = DEFAULT_MAJOR_VERSION;
52 mimeMinorVersion = DEFAULT_MINOR_VERSION;
53 this.contentId = null;
54 this.isContentIdSet = false;
55 this.contentDescription = null;
56 this.isContentDescriptionSet = false;
59 public void addField(String name, String value) {
60 name = name.trim().toLowerCase();
61 if (MimeUtil.MIME_HEADER_MIME_VERSION.equals(name) && !isMimeVersionSet) {
62 parseMimeVersion(value);
63 } else if (MimeUtil.MIME_HEADER_CONTENT_ID.equals(name) && !isContentIdSet) {
64 parseContentId(value);
65 } else if (MimeUtil.MIME_HEADER_CONTENT_DESCRIPTION.equals(name) && !isContentDescriptionSet) {
66 parseContentDescription(value);
67 } else {
68 super.addField(name, value);
72 private void parseContentDescription(String value) {
73 if (value == null) {
74 contentDescription = "";
75 } else {
76 contentDescription = value.trim();
78 isContentDescriptionSet = true;
81 private void parseContentId(final String value) {
82 if (value == null) {
83 contentId = "";
84 } else {
85 contentId = value.trim();
87 isContentIdSet = true;
90 private void parseMimeVersion(String value) {
91 final StringReader reader = new StringReader(value);
92 final MimeVersionParser parser = new MimeVersionParser(reader);
93 try {
94 parser.parse();
95 final int major = parser.getMajorVersion();
96 if (major != MimeVersionParser.INITIAL_VERSION_VALUE) {
97 mimeMajorVersion = major;
99 final int minor = parser.getMinorVersion();
100 if (minor != MimeVersionParser.INITIAL_VERSION_VALUE) {
101 mimeMinorVersion = minor;
103 } catch (MimeException e) {
104 this.mimeVersionException = e;
106 isMimeVersionSet = true;
110 * @see org.apache.james.mime4j.RFC2045MimeDescriptor#getMimeMajorVersion()
112 public int getMimeMajorVersion() {
113 return mimeMajorVersion;
117 * @see org.apache.james.mime4j.RFC2045MimeDescriptor#getMimeMinorVersion()
119 public int getMimeMinorVersion() {
120 return mimeMinorVersion;
124 * @see org.apache.james.mime4j.RFC2045MimeDescriptor#getMimeVersionParseException()
126 public MimeException getMimeVersionParseException() {
127 return mimeVersionException;
131 * @see org.apache.james.mime4j.RFC2045MimeDescriptor#getContentDescription()
133 public String getContentDescription() {
134 return contentDescription;
138 * @see org.apache.james.mime4j.RFC2045MimeDescriptor#getContentId()
140 public String getContentId() {
141 return contentId;