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 org.apache.james.mime4j.codec.DecodeMonitor;
023import org.apache.james.mime4j.dom.field.ParseException;
024import org.apache.james.mime4j.dom.field.ParsedField;
025import org.apache.james.mime4j.stream.Field;
026import org.apache.james.mime4j.stream.RawField;
027import org.apache.james.mime4j.util.ByteSequence;
028
029/**
030 * The base class of all field classes.
031 */
032public abstract class AbstractField implements ParsedField {
033
034    protected final Field rawField;
035    protected final DecodeMonitor monitor;
036
037    protected AbstractField(final Field rawField, final DecodeMonitor monitor) {
038        this.rawField = rawField;
039        this.monitor = monitor != null ? monitor : DecodeMonitor.SILENT;
040    }
041
042    /**
043     * Gets the name of the field (<code>Subject</code>,
044     * <code>From</code>, etc).
045     *
046     * @return the field name.
047     */
048    public String getName() {
049        return rawField.getName();
050    }
051
052    /**
053     * Gets the unfolded, unparsed and possibly encoded (see RFC 2047) field
054     * body string.
055     *
056     * @return the unfolded unparsed field body string.
057     */
058    public String getBody() {
059        return rawField.getBody();
060    }
061
062    /**
063     * Gets original (raw) representation of the field, if available,
064     * <code>null</code> otherwise.
065     */
066    public ByteSequence getRaw() {
067        return rawField.getRaw();
068    }
069
070    /**
071     * @see ParsedField#isValidField()
072     */
073    public boolean isValidField() {
074        return getParseException() == null;
075    }
076
077    /**
078     * @see ParsedField#getParseException()
079     */
080    public ParseException getParseException() {
081        return null;
082    }
083
084    protected RawField getRawField() {
085        if (rawField instanceof RawField) {
086            return ((RawField) rawField);
087        } else {
088            return new RawField(rawField.getName(), rawField.getBody());
089        }
090    }
091
092    @Override
093    public String toString() {
094        return rawField.toString();
095    }
096
097}