001/**************************************************************** 002 * Licensed to the Apache Software Foundation (ASF) under one * 003 * or more contributor license agreements. See the NOTICE file * 004 * distributed with this work for additional information * 005 * regarding copyright ownership. The ASF licenses this file * 006 * to you under the Apache License, Version 2.0 (the * 007 * "License"); you may not use this file except in compliance * 008 * with the License. You may obtain a copy of the License at * 009 * * 010 * http://www.apache.org/licenses/LICENSE-2.0 * 011 * * 012 * Unless required by applicable law or agreed to in writing, * 013 * software distributed under the License is distributed on an * 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 015 * KIND, either express or implied. See the License for the * 016 * specific language governing permissions and limitations * 017 * under the License. * 018 ****************************************************************/ 019 020package org.apache.james.mime4j.field; 021 022import java.util.Locale; 023 024import org.apache.james.mime4j.codec.DecodeMonitor; 025import org.apache.james.mime4j.dom.FieldParser; 026import org.apache.james.mime4j.dom.field.ContentTransferEncodingField; 027import org.apache.james.mime4j.stream.Field; 028import org.apache.james.mime4j.util.MimeUtil; 029 030/** 031 * Represents a <code>Content-Transfer-Encoding</code> field. 032 */ 033public class ContentTransferEncodingFieldImpl extends AbstractField implements ContentTransferEncodingField { 034 035 private boolean parsed = false; 036 private String encoding; 037 038 ContentTransferEncodingFieldImpl(Field rawField, DecodeMonitor monitor) { 039 super(rawField, monitor); 040 } 041 042 private void parse() { 043 parsed = true; 044 String body = getBody(); 045 if (body != null) { 046 encoding = body.trim().toLowerCase(Locale.US); 047 } else { 048 encoding = null; 049 } 050 } 051 052 /** 053 * @see org.apache.james.mime4j.dom.field.ContentTransferEncodingField#getEncoding() 054 */ 055 public String getEncoding() { 056 if (!parsed) { 057 parse(); 058 } 059 return encoding; 060 } 061 062 /** 063 * Gets the encoding of the given field if. Returns the default 064 * <code>7bit</code> if not set or if <code>f</code> is 065 * <code>null</code>. 066 * 067 * @return the encoding. 068 */ 069 public static String getEncoding(ContentTransferEncodingField f) { 070 if (f != null && f.getEncoding().length() != 0) { 071 return f.getEncoding(); 072 } 073 return MimeUtil.ENC_7BIT; 074 } 075 076 public static final FieldParser<ContentTransferEncodingField> PARSER = new FieldParser<ContentTransferEncodingField>() { 077 078 public ContentTransferEncodingField parse(final Field rawField, final DecodeMonitor monitor) { 079 return new ContentTransferEncodingFieldImpl(rawField, monitor); 080 } 081 082 }; 083}