/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.mime4j.field;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
//BEGIN android-changed: Stubbing out logging
import org.apache.james.mime4j.Log;
import org.apache.james.mime4j.LogFactory;
//END android-changed
import org.apache.james.mime4j.field.contenttype.parser.ContentTypeParser;
import org.apache.james.mime4j.field.contenttype.parser.ParseException;
import org.apache.james.mime4j.field.contenttype.parser.TokenMgrError;
/**
* Represents a Content-Type
field.
*
*
TODO: Remove dependency on Java 1.4 regexps
* * * @version $Id: ContentTypeField.java,v 1.6 2005/01/27 14:16:31 ntherning Exp $ */ public class ContentTypeField extends Field { /** * The prefix of allmultipart
MIME types.
*/
public static final String TYPE_MULTIPART_PREFIX = "multipart/";
/**
* The multipart/digest
MIME type.
*/
public static final String TYPE_MULTIPART_DIGEST = "multipart/digest";
/**
* The text/plain
MIME type.
*/
public static final String TYPE_TEXT_PLAIN = "text/plain";
/**
* The message/rfc822
MIME type.
*/
public static final String TYPE_MESSAGE_RFC822 = "message/rfc822";
/**
* The name of the boundary
parameter.
*/
public static final String PARAM_BOUNDARY = "boundary";
/**
* The name of the charset
parameter.
*/
public static final String PARAM_CHARSET = "charset";
private String mimeType = "";
private Mapnull
or hasn't got a MIME type value set.
* If child's MIME type is multipart but no boundary
* has been set the MIME type of child will be derived from
* the parent.
*
* @param child the child.
* @param parent the parent.
* @return the MIME type.
*/
public static String getMimeType(ContentTypeField child,
ContentTypeField parent) {
if (child == null || child.getMimeType().length() == 0
|| child.isMultipart() && child.getBoundary() == null) {
if (parent != null && parent.isMimeType(TYPE_MULTIPART_DIGEST)) {
return TYPE_MESSAGE_RFC822;
} else {
return TYPE_TEXT_PLAIN;
}
}
return child.getMimeType();
}
/**
* Gets the value of a parameter. Parameter names are case-insensitive.
*
* @param name the name of the parameter to get.
* @return the parameter value or null
if not set.
*/
public String getParameter(String name) {
return parameters != null
? parameters.get(name.toLowerCase())
: null;
}
/**
* Gets all parameters.
*
* @return the parameters.
*/
public Mapboundary
parameter if set.
*
* @return the boundary
parameter value or null
* if not set.
*/
public String getBoundary() {
return getParameter(PARAM_BOUNDARY);
}
/**
* Gets the value of the charset
parameter if set.
*
* @return the charset
parameter value or null
* if not set.
*/
public String getCharset() {
return getParameter(PARAM_CHARSET);
}
/**
* Gets the value of the charset
parameter if set for the
* given field. Returns the default us-ascii
if not set or if
* f
is null
.
*
* @return the charset
parameter value.
*/
public static String getCharset(ContentTypeField f) {
if (f != null) {
if (f.getCharset() != null && f.getCharset().length() > 0) {
return f.getCharset();
}
}
return "us-ascii";
}
/**
* Determines if the MIME type of this field matches the given one.
*
* @param mimeType the MIME type to match against.
* @return true
if the MIME type of this field matches,
* false
otherwise.
*/
public boolean isMimeType(String mimeType) {
return this.mimeType.equalsIgnoreCase(mimeType);
}
/**
* Determines if the MIME type of this field is multipart/*
.
*
* @return true
if this field is has a multipart/*
* MIME type, false
otherwise.
*/
public boolean isMultipart() {
return mimeType.startsWith(TYPE_MULTIPART_PREFIX);
}
public static class Parser implements FieldParser {
private static Log log = LogFactory.getLog(Parser.class);
public Field parse(final String name, final String body, final String raw) {
ParseException parseException = null;
String mimeType = "";
Map