1 12 13 17 package com.openedit.generators; 18 19 import java.io.File ; 20 import java.io.InputStream ; 21 import java.io.Reader ; 22 import java.io.Writer ; 23 import java.util.Properties ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.velocity.VelocityContext; 28 import org.apache.velocity.app.Velocity; 29 import org.apache.velocity.exception.MethodInvocationException; 30 import org.apache.velocity.runtime.RuntimeConstants; 31 import org.openedit.repository.filesystem.FileItem; 32 33 import com.openedit.Generator; 34 import com.openedit.OpenEditException; 35 import com.openedit.WebPageRequest; 36 import com.openedit.error.ContentNotAvailableException; 37 import com.openedit.page.Page; 38 import com.openedit.util.OutputFiller; 39 40 46 public class VelocityGenerator extends BaseGenerator implements Generator 47 { 48 public static Log log = LogFactory.getLog(VelocityGenerator.class); 49 public File fieldRootDirectory; 50 private static boolean initCompleted = false; 51 52 public VelocityGenerator() 53 { 54 } 55 56 public void generate( WebPageRequest inContext, Page inPage, Output inOut ) 57 throws OpenEditException 58 { 59 debug( "Rendering " + inPage ); 60 if( !inPage.exists() ) 61 { 62 String vir = inPage.get("virtual"); 63 if ( !Boolean.parseBoolean(vir) ) 64 { 65 log.info("Missing: " +inPage.getPath()); 66 throw new ContentNotAvailableException("Missing: " +inPage.getPath(),inPage.getPath()); 67 } 68 else 69 { 70 return; } 72 } 73 try 74 { 75 if ( inPage.isBinary() ) 76 { 77 InputStream in = inPage.getInputStream(); 78 new OutputFiller().fill(in, inContext.getOutputStream()); 79 return; 80 } 81 82 VelocityContext context = new VelocityContext(inContext.getPageMap()); 83 Writer writer = inOut.getWriter(); 84 85 if( !initCompleted ) 86 { 87 initStatic(); 88 } 89 90 if ( inPage.getContentItem() instanceof FileItem) { 92 String path = inPage.getContentItem().getPath(); 93 Velocity.mergeTemplate( path, inPage.getCharacterEncoding(),context, writer ); 94 } 95 else { 97 Reader in = inPage.getReader(); 98 Velocity.evaluate(context, writer, inPage.getPath(), in); 99 } 100 writer.flush(); 101 } 102 catch (MethodInvocationException ex) 103 { 104 if (ex.getWrappedThrowable() instanceof OpenEditException) 105 { 106 throw (OpenEditException) ex.getWrappedThrowable(); 107 } 108 Throwable wrapped = ex.getWrappedThrowable(); 109 if( ignoreError( wrapped )) 110 { 111 return; 113 } 114 throw new OpenEditException(wrapped); 115 } 116 catch ( Exception ioex ) 117 { 118 if( ignoreError(ioex) ) 119 { 120 log.info("Browser canceled request"); 121 return; 122 } 123 if (ioex instanceof OpenEditException) 124 { 125 throw (OpenEditException) ioex; 126 } 127 throw new OpenEditException(ioex); 128 } 129 } 130 131 protected synchronized void initStatic() throws Exception 132 { 133 137 142 Properties velocityProperties = new Properties (); 144 velocityProperties.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, 145 "com.openedit.generators.VelocityLogger"); 146 147 154 velocityProperties.put("runtime.log.invalid.references", "false"); 155 velocityProperties.put("resource.manager.logwhenfound", "false"); 156 velocityProperties.put("file.resource.loader.cache", "true"); 157 velocityProperties.put("velocimacro.permissions.allow.inline", "true"); 158 159 velocityProperties.setProperty("file.resource.loader.path", getRootDirectory() 160 .getAbsolutePath()); 161 162 Velocity.init(velocityProperties); 165 initCompleted = true; 166 } 167 168 protected void debug(String inMessage) 169 { 170 log.debug(inMessage); 171 } 172 173 181 public void init() throws Exception 182 { 183 184 } 185 186 public File getRootDirectory() 187 { 188 return fieldRootDirectory; 189 } 190 191 public void setRootDirectory(File inRootDirectory) 192 { 193 fieldRootDirectory = inRootDirectory; 194 } 195 196 } 197 | Popular Tags |