View Javadoc
1 /* 2 * Copyright (c) 2003 3 * Information Desire GmbH 4 * All rights reserved. 5 */ 6 package com.infodesire.infobit.render.velocity; 7 8 import com.infodesire.infobit.render.InfobitRenderer; 9 10 import java.io.InputStream; 11 import java.io.Reader; 12 import java.io.BufferedReader; 13 import java.io.Writer; 14 import java.io.PipedOutputStream; 15 import java.io.PipedInputStream; 16 import java.io.OutputStreamWriter; 17 import java.io.InputStreamReader; 18 19 import com.infodesire.infobit.InfobitManager; 20 import com.infodesire.infobit.InfobitException; 21 import com.infodesire.infobit.InfobitSecurityException; 22 23 import com.infodesire.infobit.data.Infobit; 24 import com.infodesire.infobit.data.Content; 25 import com.infodesire.infobit.data.Script; 26 import com.infodesire.infobit.data.Template; 27 import com.infodesire.infobit.data.BinaryContent; 28 import com.infodesire.infobit.data.PrimitiveContent; 29 import com.infodesire.infobit.data.TextContent; 30 31 import org.apache.velocity.VelocityContext; 32 import org.apache.velocity.context.Context; 33 import org.apache.velocity.app.Velocity; 34 import org.apache.velocity.app.VelocityEngine; 35 import org.apache.velocity.runtime.RuntimeConstants; 36 37 import java.util.Iterator; 38 import java.util.Properties; 39 import java.util.Map; 40 import java.util.HashMap; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 /*** 46 * render infobits in a way suitable for servlet usage 47 * 48 * @author konstantin 49 * @created August 21, 2003 50 * @version $Revision: 1.3 $ 51 */ 52 public class ServletVelocityRenderer extends VelocityRenderer { 53 54 private static Log _log = LogFactory.getLog(ServletVelocityRenderer.class); 55 56 57 58 /*** 59 * setup context for infobit rendering. this method is called by #inbfobit() 60 * directive it places following objects on context: 61 * <ul> 62 * <li> $infobit - infobit itself 63 * <li> $version - selected infobit version ( currently it's active 64 * version ) 65 * <li> $content - content belonging to this version 66 * <li> $linkname where linkname is name of script link - just a 67 * convenience ( may dissapear ) script names will be also available in 68 * template as $content.links.get("name of the link").name 69 * </ul> 70 * 71 * 72 * @param context velocity context to be augmented 73 * @param infobitName Description of Parameter 74 */ 75 public void setupContext(Context context, String infobitName) { 76 try { 77 Infobit ibit = _manager.getInfobit(infobitName); 78 if (ibit == null) { 79 // well, this shall not happen, since this method shall be called after 80 // resource manager loaded infobit template. 81 // log anyway, but do not bomb. 82 _log.error("did not found infobit: " + infobitName); 83 return; 84 } 85 86 Content content = getInfobitContent(ibit); 87 88 context.put("infobit", ibit); 89 context.put("content", content); 90 context.put("version", content.getVersion()); 91 92 if (content instanceof Script) { 93 Map links = ((Script) content).getLinks(); 94 95 Infobit link; 96 String key; 97 for (Iterator iter = ((Script) content).getLinks().keySet().iterator(); iter.hasNext(); ) { 98 key = (String) iter.next(); 99 link = (Infobit) ((Script) content).getLinks().get(key); 100 context.put(key, link.getName()); 101 } 102 } 103 } catch (Exception ex) { 104 _log.error("exception occured while acquiring infobit params: " + infobitName, ex); 105 } 106 107 } 108 109 110 /*** 111 * not sure it's good here. just a noop 112 * 113 * @param writer Description of Parameter 114 * @param content Description of Parameter 115 */ 116 public void renderContent(Writer writer, Content content) { 117 } 118 119 120 121 /*** 122 * initialize itself and velocity with given properties. 123 * 124 * @param properties Description of Parameter 125 */ 126 public void init(Properties properties) { 127 super.init(properties); 128 } 129 130 131 /*** 132 * returns proper version of infobit content. currently we support only 133 * actual version, but later we may made it more advanced to be able to view 134 * preview versions / or from certain date. 135 * 136 * @param ibit Description of Parameter 137 * @return The InfobitContent value 138 * @exception InfobitException Description of Exception 139 * @exception InfobitSecurityException Description of Exception 140 */ 141 Content getInfobitContent(Infobit ibit) throws InfobitException, InfobitSecurityException { 142 return ibit.getActualVersion().getContent(); 143 } 144 145 146 /*** 147 * read content as stream out of infobit. this is callback for your template 148 * loader. we substitute proxy templates for images. for templates we will 149 * also tweak out context... 150 * 151 * @param name Description of Parameter 152 * @return Description of the Returned Value 153 */ 154 155 InputStream getTemplate(String name) { 156 try { 157 Infobit ibit = _manager.getInfobit(name); 158 if (ibit == null) { 159 _log.error("did not found infobit: " + name); 160 return null; 161 } 162 163 Content content = getInfobitContent(ibit); 164 if (_log.isDebugEnabled()) { 165 _log.debug("loaded content for " + name + ": " + content); 166 } 167 if (content instanceof PrimitiveContent) { 168 // render primitive content 169 if (content instanceof Template) { 170 return _manager.getContent((PrimitiveContent) content); 171 } 172 else if (content instanceof TextContent) { 173 // we just return blobby stream with text content velocity will 174 // do the right thing 175 return _manager.getContent((PrimitiveContent) content); 176 } 177 else if (content instanceof BinaryContent) { 178 // we just return template which provides correct links for binary 179 // content embedding. 180 return AdvancedClasspathLoader.getResourceAsStream("com/infodesire/infobit/render/velocity/binary.vm"); 181 } 182 183 } 184 else if (content instanceof Script) { 185 // render scripts. we assemble new context, and merge it with template 186 if (_log.isDebugEnabled()) { 187 _log.debug("processing script"); 188 } 189 Script script = (Script) content; 190 191 String templateName = script.getTemplate().getName(); 192 if (_log.isDebugEnabled()) { 193 _log.debug("processing script with template: " + templateName); 194 } 195 Template template = (Template) getInfobitContent(script.getTemplate()); 196 return _manager.getContent(template); 197 } 198 throw new InfobitException("rendering content of type " + content.getClass() + " is not supported yet"); 199 } catch (Exception ex) { 200 _log.error("exception occured while rendering infobit: " + name, ex); 201 } 202 return null; 203 } 204 }

This page was automatically generated by Maven