1 package org.roller.presentation.velocity; 2 3 import org.apache.velocity.Template; 4 import org.apache.velocity.VelocityContext; 5 import org.apache.velocity.app.VelocityEngine; 6 import org.apache.velocity.exception.ParseErrorException; 7 import org.apache.velocity.exception.ResourceNotFoundException; 8 import org.roller.RollerException; 9 import org.roller.model.RollerFactory; 10 import org.roller.pojos.UserData; 11 import org.roller.pojos.WebsiteData; 12 import org.roller.presentation.RollerContext; 13 import org.roller.util.RegexUtil; 14 import org.roller.util.StringUtils; 15 import org.roller.util.Utilities; 16 17 import java.io.File ; 18 import java.io.FileNotFoundException ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.StringWriter ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Collection ; 24 import java.util.Date ; 25 import java.util.Locale ; 26 import java.util.Properties ; 27 import java.util.TimeZone ; 28 29 38 public class ExportRss 39 { 40 private VelocityEngine ve = null; 41 private VelocityContext ctx = null; 42 private UserData user = null; 43 private boolean exportAtom; 44 45 private static final String RSS_TEMPLATE = "org/roller/presentation/velocity/export_rss.vm"; 46 private static final String ATOM_TEMPLATE = "org/roller/presentation/velocity/export_atom.vm"; 47 48 public ExportRss(WebsiteData website) throws Exception 49 { 50 Properties props = new Properties (); 51 props.load(RollerContext.getServletContext(). 52 getResourceAsStream("/WEB-INF/velocity.properties")); 53 ve = new VelocityEngine(); 54 ve.info("*******************************************"); 55 ve.info("Initializing VelocityEngine for ExportRss"); 56 ve.init( props ); 57 ve.info("Done initializing VelocityEngine for ExportRss"); 58 ve.info("************************************************"); 59 60 ctx = new VelocityContext(); 61 62 user = website.getUser(); 63 RollerContext rollerCtx = RollerContext.getRollerContext( 64 RollerContext.getServletContext()); 65 loadPageHelper(); 66 67 loadDates(website); 68 69 loadWebsiteInfo(rollerCtx, website); 70 71 loadTheRest(rollerCtx); 72 } 73 74 public void setExportAtom(boolean atom) 75 { 76 exportAtom = atom; 77 } 78 79 87 public void exportEntries(Collection entries, String fileName) throws ResourceNotFoundException, ParseErrorException, Exception 88 { 89 ctx.put("entries", entries); 90 91 String templateFile = RSS_TEMPLATE; 92 if (exportAtom) templateFile = ATOM_TEMPLATE; 93 Template template = ve.getTemplate( templateFile, "utf-8" ); 94 StringWriter sw = new StringWriter (); 95 template.merge(ctx, sw); 96 97 writeResultsToFile((String )ctx.get("uploadPath"), sw, fileName); 98 } 99 100 103 private void writeResultsToFile(String filePath, StringWriter sw, String fileName) 104 throws RollerException, IOException 105 { 106 filePath += "/" + user.getUserName(); 107 new java.io.File ( filePath ).mkdirs(); 109 filePath += "/" + fileName; 110 111 File outputFile = new java.io.File ( filePath ); 112 FileOutputStream out = null; 113 try 114 { 115 out = new FileOutputStream ( outputFile ); 117 out.write( sw.toString().getBytes() ); 118 out.flush(); 119 } 120 catch ( FileNotFoundException e ) 121 { 122 throw new RollerException( "Unable to write to: " + outputFile.getAbsolutePath(), e ); 123 } 124 finally 125 { 126 try 127 { 128 if ( out != null ) 129 { 130 out.close(); 131 } 132 } 133 catch ( java.io.IOException ioe ) 134 { 135 System.err.println( "ExportRss unable to close OutputStream" ); 136 } 137 } 138 } 139 140 144 private void loadTheRest(RollerContext rollerCtx) 145 { 146 ctx.put("utilities", new Utilities() ); 147 ctx.put("stringUtils", new StringUtils() ); 148 ctx.put("entryLength", new Integer (-1)); 149 } 150 151 156 private void loadWebsiteInfo(RollerContext rollerCtx, WebsiteData website) 157 { 158 ctx.put("website", website); 159 ctx.put("userName", user.getUserName() ); 160 ctx.put("fullName", user.getFullName() ); 161 ctx.put("emailAddress", user.getEmailAddress() ); 162 163 ctx.put("encodedEmail", RegexUtil.encode(user.getEmailAddress())); 164 ctx.put("obfuscatedEmail", RegexUtil.obfuscateEmail(user.getEmailAddress())); 165 166 167 StringBuffer sb = new StringBuffer (); 169 String uploadDir = null; 170 try { 171 uploadDir = RollerFactory.getRoller().getFileManager().getUploadDir(); 172 } catch(Exception e) {} 173 174 ctx.put("uploadPath", uploadDir); 175 } 176 177 181 private void loadDates(WebsiteData website) 182 { 183 try 184 { 185 Date updateTime = RollerFactory.getRoller().getWeblogManager() 187 .getWeblogLastPublishTime( user.getUserName(), null ); 188 ctx.put("updateTime", updateTime); 189 } 190 catch (RollerException e) 191 { 192 ctx.put("updateTime", new Date ()); 193 } 194 ctx.put("now", new Date ()); 195 196 Locale locale = website.getLocaleInstance(); 198 ctx.put("locale", locale); 199 200 ctx.put("timezone", website.getTimeZoneInstance()); 202 203 SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd", locale); 206 sdf.setTimeZone( (TimeZone )ctx.get("timezone") ); 207 ctx.put("plainFormat", sdf.toLocalizedPattern()); 208 209 sdf.applyPattern("EEEE MMMM dd, yyyy"); 210 ctx.put("toStringFormat", sdf.toLocalizedPattern()); 211 212 sdf.applyPattern("MMM dd yyyy, hh:mm:ss a z"); 213 ctx.put("timestampFormat", sdf.toLocalizedPattern()); 214 215 ctx.put("dateFormatter", sdf ); 216 } 217 224 private void loadPageHelper() 225 { 226 PageHelper pageHelper = new PageHelper(null, null, ctx); 228 ctx.put("pageHelper", pageHelper ); 230 } 231 } 232 | Popular Tags |