001 // Copyright 2007, 2008, 2009 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.tapestry5.internal.services; 016 017 import org.apache.tapestry5.ContentType; 018 import org.apache.tapestry5.MarkupWriter; 019 import org.apache.tapestry5.dom.DefaultMarkupModel; 020 import org.apache.tapestry5.dom.MarkupModel; 021 import org.apache.tapestry5.dom.XMLMarkupModel; 022 import org.apache.tapestry5.internal.structure.Page; 023 import org.apache.tapestry5.services.MarkupWriterFactory; 024 025 public class MarkupWriterFactoryImpl implements MarkupWriterFactory 026 { 027 private final PageContentTypeAnalyzer analyzer; 028 029 private final RequestPageCache cache; 030 031 private final MarkupModel htmlModel = new DefaultMarkupModel(); 032 033 private final MarkupModel xmlModel = new XMLMarkupModel(); 034 035 private final MarkupModel htmlPartialModel = new DefaultMarkupModel(true); 036 037 private final MarkupModel xmlPartialModel = new XMLMarkupModel(true); 038 039 public MarkupWriterFactoryImpl(PageContentTypeAnalyzer analyzer, RequestPageCache cache) 040 { 041 this.analyzer = analyzer; 042 this.cache = cache; 043 } 044 045 public MarkupWriter newMarkupWriter(ContentType contentType) 046 { 047 return newMarkupWriter(contentType, false); 048 } 049 050 public MarkupWriter newPartialMarkupWriter(ContentType contentType) 051 { 052 return newMarkupWriter(contentType, true); 053 } 054 055 @SuppressWarnings({"UnusedDeclaration"}) 056 private MarkupWriter newMarkupWriter(ContentType contentType, boolean partial) 057 { 058 boolean isHTML = contentType.getMimeType().equalsIgnoreCase("text/html"); 059 060 MarkupModel model = partial 061 ? (isHTML ? htmlPartialModel : xmlPartialModel) 062 : (isHTML ? htmlModel : xmlModel); 063 064 // The charset parameter sets the encoding attribute of the XML declaration, if 065 // not null and if using the XML model. 066 067 return new MarkupWriterImpl(model, contentType.getCharset()); 068 } 069 070 public MarkupWriter newMarkupWriter(String pageName) 071 { 072 Page page = cache.get(pageName); 073 074 ContentType contentType = analyzer.findContentType(page); 075 076 return newMarkupWriter(contentType); 077 } 078 }