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
020 package org.apache.james.mime4j.field;
021
022 import java.io.StringReader;
023
024 import org.apache.james.mime4j.MimeException;
025 import org.apache.james.mime4j.codec.DecodeMonitor;
026 import org.apache.james.mime4j.dom.FieldParser;
027 import org.apache.james.mime4j.dom.field.MimeVersionField;
028 import org.apache.james.mime4j.field.mimeversion.parser.MimeVersionParser;
029 import org.apache.james.mime4j.field.mimeversion.parser.ParseException;
030 import org.apache.james.mime4j.stream.Field;
031
032 /**
033 * Represents a <code>MIME-Version</code> field.
034 */
035 public class MimeVersionFieldImpl extends AbstractField implements MimeVersionField {
036
037 public static final int DEFAULT_MINOR_VERSION = 0;
038 public static final int DEFAULT_MAJOR_VERSION = 1;
039
040 private boolean parsed = false;
041 private int major = DEFAULT_MAJOR_VERSION;
042 private int minor = DEFAULT_MINOR_VERSION;
043 private ParseException parsedException;
044
045 MimeVersionFieldImpl(Field rawField, DecodeMonitor monitor) {
046 super(rawField, monitor);
047 }
048
049 private void parse() {
050 parsed = true;
051 major = DEFAULT_MAJOR_VERSION;
052 minor = DEFAULT_MINOR_VERSION;
053 String body = getBody();
054 if (body != null) {
055 StringReader reader = new StringReader(body);
056 MimeVersionParser parser = new MimeVersionParser(reader);
057 try {
058 parser.parse();
059 int v = parser.getMajorVersion();
060 if (v != MimeVersionParser.INITIAL_VERSION_VALUE) {
061 major = v;
062 }
063 v = parser.getMinorVersion();
064 if (v != MimeVersionParser.INITIAL_VERSION_VALUE) {
065 minor = v;
066 }
067 } catch (MimeException ex) {
068 parsedException = new ParseException(ex);
069 }
070 }
071 }
072
073 public int getMinorVersion() {
074 if (!parsed) {
075 parse();
076 }
077 return minor;
078 }
079
080 public int getMajorVersion() {
081 if (!parsed) {
082 parse();
083 }
084 return major;
085 }
086
087 @Override
088 public org.apache.james.mime4j.dom.field.ParseException getParseException() {
089 return parsedException;
090 }
091
092 public static final FieldParser<MimeVersionField> PARSER = new FieldParser<MimeVersionField>() {
093
094 public MimeVersionField parse(final Field rawField, final DecodeMonitor monitor) {
095 return new MimeVersionFieldImpl(rawField, monitor);
096 }
097
098 };
099
100 }