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 import java.util.ArrayList;
024 import java.util.Collections;
025 import java.util.List;
026
027 import org.apache.james.mime4j.codec.DecodeMonitor;
028 import org.apache.james.mime4j.dom.FieldParser;
029 import org.apache.james.mime4j.dom.field.ContentLanguageField;
030 import org.apache.james.mime4j.field.language.parser.ContentLanguageParser;
031 import org.apache.james.mime4j.field.language.parser.ParseException;
032 import org.apache.james.mime4j.stream.Field;
033
034 /**
035 * Represents a <code>Content-Transfer-Encoding</code> field.
036 */
037 public class ContentLanguageFieldImpl extends AbstractField implements ContentLanguageField {
038
039 private boolean parsed = false;
040 private List<String> languages;
041 private ParseException parseException;
042
043 ContentLanguageFieldImpl(Field rawField, DecodeMonitor monitor) {
044 super(rawField, monitor);
045 }
046
047 private void parse() {
048 parsed = true;
049 languages = Collections.<String>emptyList();
050 String body = getBody();
051 if (body != null) {
052 ContentLanguageParser parser = new ContentLanguageParser(new StringReader(body));
053 try {
054 languages = parser.parse();
055 } catch (ParseException ex) {
056 parseException = ex;
057 }
058 }
059 }
060
061 @Override
062 public org.apache.james.mime4j.dom.field.ParseException getParseException() {
063 return parseException;
064 }
065
066 public List<String> getLanguages() {
067 if (!parsed) {
068 parse();
069 }
070 return new ArrayList<String>(languages);
071 }
072
073 public static final FieldParser<ContentLanguageField> PARSER = new FieldParser<ContentLanguageField>() {
074
075 public ContentLanguageField parse(final Field rawField, final DecodeMonitor monitor) {
076 return new ContentLanguageFieldImpl(rawField, monitor);
077 }
078
079 };
080
081 }
082