001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.markup;
016
017import java.io.PrintWriter;
018import java.util.Map;
019
020import org.apache.commons.logging.Log;
021import org.apache.hivemind.util.Defense;
022import org.apache.tapestry.IMarkupWriter;
023import org.apache.tapestry.util.ContentType;
024
025/**
026 * @author Howard M. Lewis Ship
027 * @since 4.0
028 */
029public class MarkupWriterSourceImpl implements MarkupWriterSource
030{
031    private Log _log;
032
033    private MarkupFilter _defaultFilter = new AsciiMarkupFilter();
034
035    private Map _contributions;
036
037    public void setContributions(Map contributions)
038    {
039        _contributions = contributions;
040    }
041
042    public IMarkupWriter newMarkupWriter(PrintWriter writer, ContentType contentType)
043    {
044        Defense.notNull(writer, "writer");
045        Defense.notNull(contentType, "contentType");
046
047        MarkupFilter filter = findFilter(contentType);
048
049        return new MarkupWriterImpl(contentType.toString(), writer, filter);
050    }
051
052    private MarkupFilter findFilter(ContentType contentType)
053    {
054        // Look for an exact match (caseless).
055
056        String key = contentType.toString().toLowerCase();
057
058        MarkupFilter result = (MarkupFilter) _contributions.get(key);
059
060        if (result == null)
061            result = (MarkupFilter) _contributions.get(contentType.getMimeType());
062
063        if (result == null)
064        {
065            _log.error(MarkupMessages.noFilterMatch(contentType));
066
067            result = _defaultFilter;
068        }
069
070        return result;
071    }
072
073    public void setLog(Log log)
074    {
075        _log = log;
076    }
077}