KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > markup > MarkupWriterSourceImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.markup;
16
17 import java.io.PrintWriter JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.hivemind.util.Defense;
22 import org.apache.tapestry.IMarkupWriter;
23 import org.apache.tapestry.util.ContentType;
24
25 /**
26  * @author Howard M. Lewis Ship
27  * @since 4.0
28  */

29 public class MarkupWriterSourceImpl implements MarkupWriterSource
30 {
31     private Log _log;
32
33     private MarkupFilter _defaultFilter = new AsciiMarkupFilter();
34
35     private Map JavaDoc _contributions;
36
37     public void setContributions(Map JavaDoc contributions)
38     {
39         _contributions = contributions;
40     }
41
42     public IMarkupWriter newMarkupWriter(PrintWriter JavaDoc writer, ContentType contentType)
43     {
44         Defense.notNull(writer, "writer");
45         Defense.notNull(contentType, "contentType");
46
47         MarkupFilter filter = findFilter(contentType);
48
49         return new MarkupWriterImpl(contentType.toString(), writer, filter);
50     }
51
52     private MarkupFilter findFilter(ContentType contentType)
53     {
54         // Look for an exact match (caseless).
55

56         String JavaDoc key = contentType.toString().toLowerCase();
57
58         MarkupFilter result = (MarkupFilter) _contributions.get(key);
59
60         if (result == null)
61             result = (MarkupFilter) _contributions.get(contentType.getMimeType());
62
63         if (result == null)
64         {
65             _log.error(MarkupMessages.noFilterMatch(contentType));
66
67             result = _defaultFilter;
68         }
69
70         return result;
71     }
72
73     public void setLog(Log log)
74     {
75         _log = log;
76     }
77 }
Popular Tags