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 015 package org.apache.tapestry.markup; 016 017 import java.io.PrintWriter; 018 import java.util.Map; 019 020 import org.apache.commons.logging.Log; 021 import org.apache.hivemind.util.Defense; 022 import org.apache.tapestry.IMarkupWriter; 023 import org.apache.tapestry.json.IJSONWriter; 024 import org.apache.tapestry.util.ContentType; 025 026 /** 027 * @author Howard M. Lewis Ship 028 * @since 4.0 029 */ 030 public class MarkupWriterSourceImpl implements MarkupWriterSource 031 { 032 private Log _log; 033 034 private MarkupFilter _defaultFilter = new AsciiMarkupFilter(); 035 036 private Map _contributions; 037 038 public void setContributions(Map contributions) 039 { 040 _contributions = contributions; 041 } 042 043 public IMarkupWriter newMarkupWriter(PrintWriter writer, ContentType contentType) 044 { 045 Defense.notNull(writer, "writer"); 046 Defense.notNull(contentType, "contentType"); 047 048 MarkupFilter filter = findFilter(contentType); 049 050 return new MarkupWriterImpl(contentType.toString(), writer, filter); 051 } 052 053 public IJSONWriter newJSONWriter(PrintWriter writer, ContentType contentType) 054 { 055 Defense.notNull(writer, "writer"); 056 Defense.notNull(contentType, "contentType"); 057 058 //TODO: Use the content type to add a filter 059 //MarkupFilter filter = findFilter(contentType); 060 061 return new JSONWriterImpl(writer); 062 } 063 064 private MarkupFilter findFilter(ContentType contentType) 065 { 066 // Look for an exact match (caseless). 067 068 String key = contentType.toString().toLowerCase(); 069 070 MarkupFilter result = (MarkupFilter) _contributions.get(key); 071 072 if (result == null) 073 result = (MarkupFilter) _contributions.get(contentType.getMimeType()); 074 075 if (result == null) 076 { 077 _log.error(MarkupMessages.noFilterMatch(contentType)); 078 079 result = _defaultFilter; 080 } 081 082 return result; 083 } 084 085 public void setLog(Log log) 086 { 087 _log = log; 088 } 089 }