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.BitSet;
023
024import org.apache.james.mime4j.codec.DecodeMonitor;
025import org.apache.james.mime4j.dom.FieldParser;
026import org.apache.james.mime4j.dom.field.MimeVersionField;
027import org.apache.james.mime4j.stream.Field;
028import org.apache.james.mime4j.stream.ParserCursor;
029import org.apache.james.mime4j.stream.RawField;
030import org.apache.james.mime4j.stream.RawFieldParser;
031import org.apache.james.mime4j.util.ByteSequence;
032import org.apache.james.mime4j.util.ContentUtil;
033
034/**
035 * Represents a <code>MIME-Version</code> field.
036 */
037public class MimeVersionFieldLenientImpl extends AbstractField implements MimeVersionField {
038
039    private final static int FULL_STOP  = '.';
040    private final static BitSet DELIM = RawFieldParser.INIT_BITSET(FULL_STOP);
041
042    public static final int DEFAULT_MINOR_VERSION = 0;
043    public static final int DEFAULT_MAJOR_VERSION = 1;
044
045    private boolean parsed = false;
046    private int major = DEFAULT_MAJOR_VERSION;
047    private int minor = DEFAULT_MINOR_VERSION;
048
049    MimeVersionFieldLenientImpl(Field rawField, DecodeMonitor monitor) {
050        super(rawField, monitor);
051    }
052
053    private void parse() {
054        parsed = true;
055        major = DEFAULT_MAJOR_VERSION;
056        minor = DEFAULT_MINOR_VERSION;
057        RawField f = getRawField();
058        ByteSequence buf = f.getRaw();
059        int pos = f.getDelimiterIdx() + 1;
060        if (buf == null) {
061            String body = f.getBody();
062            if (body == null) {
063                return;
064            }
065            buf = ContentUtil.encode(body);
066            pos = 0;
067        }
068        RawFieldParser parser = RawFieldParser.DEFAULT;
069        ParserCursor cursor = new ParserCursor(pos, buf.length());
070        String token1 = parser.parseValue(buf, cursor, DELIM);
071        try {
072            major = Integer.parseInt(token1);
073            if (major < 0) {
074                major = 0;
075            }
076        } catch (NumberFormatException ex) {
077        }
078        if (!cursor.atEnd() && buf.byteAt(cursor.getPos()) == FULL_STOP) {
079            cursor.updatePos(cursor.getPos() + 1);
080        }
081        String token2 = parser.parseValue(buf, cursor, null);
082        try {
083            minor = Integer.parseInt(token2);
084            if (minor < 0) {
085                minor = 0;
086            }
087        } catch (NumberFormatException ex) {
088        }
089    }
090
091    public int getMinorVersion() {
092        if (!parsed) {
093            parse();
094        }
095        return minor;
096    }
097
098    public int getMajorVersion() {
099        if (!parsed) {
100            parse();
101        }
102        return major;
103    }
104
105    public static final FieldParser<MimeVersionField> PARSER = new FieldParser<MimeVersionField>() {
106
107        public MimeVersionField parse(final Field rawField, final DecodeMonitor monitor) {
108            return new MimeVersionFieldLenientImpl(rawField, monitor);
109        }
110
111    };
112
113}