001/* 002 * (C) Copyright 2006-2011 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 * bstefanescu 018 */ 019package org.nuxeo.ecm.platform.rendering.api; 020 021import java.io.IOException; 022import java.io.OutputStream; 023import java.io.OutputStreamWriter; 024import java.io.Writer; 025import java.nio.charset.Charset; 026import java.util.HashMap; 027import java.util.Map; 028 029/** 030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 031 */ 032public class View { 033 034 protected RenderingEngine renderingEngine; 035 036 protected String name; 037 038 protected Object object; 039 040 protected Map<String, Object> args; 041 042 public View(RenderingEngine renderingEngine, String name) { 043 this(renderingEngine, name, null); 044 } 045 046 public View(RenderingEngine renderingEngine, String name, Object object) { 047 this.renderingEngine = renderingEngine; 048 this.name = name; 049 this.args = new HashMap<String, Object>(); 050 forObject(object); 051 } 052 053 public String getName() { 054 return name; 055 } 056 057 /** 058 * @return the object 059 */ 060 public Object getObject() { 061 return object; 062 } 063 064 /** 065 * @return the renderingEngine 066 */ 067 public RenderingEngine getRenderingEngine() { 068 return renderingEngine; 069 } 070 071 public View forObject(Object object) { 072 this.object = object; 073 args.put("This", object); 074 return this; 075 } 076 077 public View arg(String key, Object value) { 078 args.put(key, value); 079 return this; 080 } 081 082 public View args(Map<String, Object> args) { 083 this.args.putAll(args); 084 return this; 085 } 086 087 public void render(OutputStream out) throws RenderingException { 088 render(new OutputStreamWriter(out)); 089 } 090 091 public void render(OutputStream out, String charset) throws RenderingException { 092 try { 093 render(new OutputStreamWriter(out, charset)); 094 } catch (RenderingException e) { 095 throw e; 096 } catch (IOException e) { 097 throw new RenderingException(e); 098 } 099 } 100 101 public void render(OutputStream out, Charset charset) throws RenderingException { 102 render(new OutputStreamWriter(out, charset)); 103 } 104 105 public void render(Writer writer) throws RenderingException { 106 try { 107 renderingEngine.render(name, args, writer); 108 } finally { 109 try { 110 writer.flush(); 111 } catch (IOException e) { 112 throw new RenderingException(e); 113 } 114 } 115 } 116 117 @Override 118 public String toString() { 119 return object != null ? object.getClass().getName() + "#" + name : name; 120 } 121 122}