14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/*
24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  Copyright 2006 the mime4j project
34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  Licensed under the Apache License, Version 2.0 (the "License");
54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  you may not use this file except in compliance with the License.
64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  You may obtain a copy of the License at
74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  Unless required by applicable law or agreed to in writing, software
114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  distributed under the License is distributed on an "AS IS" BASIS,
124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  See the License for the specific language governing permissions and
144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *  limitations under the License.
154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.james.mime4j.field;
174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.util.HashMap;
194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.util.Map;
204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class DelegatingFieldParser implements FieldParser {
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private Map<String, FieldParser> parsers = new HashMap<String, FieldParser>();
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private FieldParser defaultParser = new UnstructuredField.Parser();
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Sets the parser used for the field named <code>name</code>.
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param name the name of the field
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param parser the parser for fields named <code>name</code>
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void setFieldParser(final String name, final FieldParser parser) {
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        parsers.put(name.toLowerCase(), parser);
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public FieldParser getParser(final String name) {
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        final FieldParser field = parsers.get(name.toLowerCase());
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if(field==null) {
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            return defaultParser;
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return field;
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public Field parse(final String name, final String body, final String raw) {
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        final FieldParser parser = getParser(name);
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return parser.parse(name, body, raw);
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
48