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.util.ArrayList;
023 import java.util.BitSet;
024 import java.util.List;
025
026 import org.apache.james.mime4j.codec.DecodeMonitor;
027 import org.apache.james.mime4j.dom.FieldParser;
028 import org.apache.james.mime4j.dom.field.ContentLanguageField;
029 import org.apache.james.mime4j.stream.Field;
030 import org.apache.james.mime4j.stream.ParserCursor;
031 import org.apache.james.mime4j.stream.RawField;
032 import org.apache.james.mime4j.stream.RawFieldParser;
033 import org.apache.james.mime4j.util.ByteSequence;
034 import org.apache.james.mime4j.util.ContentUtil;
035
036 /**
037 * Represents a <code>Content-Transfer-Encoding</code> field.
038 */
039 public class ContentLanguageFieldLenientImpl extends AbstractField implements ContentLanguageField {
040
041 private final static int COMMA = ',';
042 private final static BitSet DELIM = RawFieldParser.INIT_BITSET(COMMA);
043
044 private boolean parsed = false;
045 private List<String> languages;
046
047 ContentLanguageFieldLenientImpl(final Field rawField, final DecodeMonitor monitor) {
048 super(rawField, monitor);
049 }
050
051 private void parse() {
052 parsed = true;
053 languages = new ArrayList<String>();
054 RawField f = getRawField();
055 ByteSequence buf = f.getRaw();
056 int pos = f.getDelimiterIdx() + 1;
057 if (buf == null) {
058 String body = f.getBody();
059 if (body == null) {
060 return;
061 }
062 buf = ContentUtil.encode(body);
063 pos = 0;
064 }
065 RawFieldParser parser = RawFieldParser.DEFAULT;
066 ParserCursor cursor = new ParserCursor(pos, buf.length());
067 for (;;) {
068 String token = parser.parseToken(buf, cursor, DELIM);
069 if (token.length() > 0) {
070 languages.add(token);
071 }
072 if (cursor.atEnd()) {
073 break;
074 } else {
075 pos = cursor.getPos();
076 if (buf.byteAt(pos) == COMMA) {
077 cursor.updatePos(pos + 1);
078 }
079 }
080 }
081 }
082
083 public List<String> getLanguages() {
084 if (!parsed) {
085 parse();
086 }
087 return new ArrayList<String>(languages);
088 }
089
090 public static final FieldParser<ContentLanguageField> PARSER = new FieldParser<ContentLanguageField>() {
091
092 public ContentLanguageField parse(final Field rawField, final DecodeMonitor monitor) {
093 return new ContentLanguageFieldLenientImpl(rawField, monitor);
094 }
095
096 };
097
098 }
099