001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Jean-Marc Orliaguet, Chalmers 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.theme.views; 023 024import java.io.BufferedReader; 025import java.io.IOException; 026import java.io.InputStream; 027import java.io.InputStreamReader; 028import java.io.Reader; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.theme.ResourceResolver; 033import org.nuxeo.theme.rendering.RenderingInfo; 034 035public class TemplateView extends AbstractView { 036 037 private static final Log log = LogFactory.getLog(TemplateView.class); 038 039 @Override 040 public String render(final RenderingInfo info) { 041 final ViewType viewType = getViewType(); 042 final String template = viewType.getTemplate(); 043 return getTemplateContent(template); 044 } 045 046 public String getTemplateContent(final String template) { 047 String result = ""; 048 InputStream is = null; 049 try { 050 // checks through FacesResourceResolver 051 is = ResourceResolver.getInstance().getResourceAsStream(template); 052 if (is == null) { 053 log.warn("Template file not found: " + template); 054 } else { 055 Reader in = null; 056 try { 057 in = new BufferedReader(new InputStreamReader(is)); 058 StringBuilder rendered = new StringBuilder(); 059 int ch; 060 while ((ch = in.read()) > -1) { 061 rendered.append((char) ch); 062 } 063 result = rendered.toString(); 064 } catch (IOException e) { 065 log.error(e, e); 066 } finally { 067 if (in != null) { 068 in.close(); 069 } 070 } 071 } 072 } catch (IOException e) { 073 log.error(e, e); 074 } finally { 075 if (is != null) { 076 try { 077 is.close(); 078 } catch (IOException e) { 079 log.error(e, e); 080 } finally { 081 is = null; 082 } 083 } 084 } 085 return result.trim(); 086 } 087}