| Home >> All >> org >> roller >> presentation >> [ weblog Javadoc ] |
Source code: org/roller/presentation/weblog/WeblogEntryDataEx.java
1 package org.roller.presentation.weblog; 2 3 import org.apache.velocity.VelocityContext; 4 import org.apache.velocity.app.Velocity; 5 import org.apache.velocity.exception.MethodInvocationException; 6 import org.apache.velocity.exception.ParseErrorException; 7 import org.apache.velocity.exception.ResourceNotFoundException; 8 import org.roller.model.UserData; 9 import org.roller.model.WeblogEntryData; 10 import org.roller.presentation.RollerRequest; 11 import org.roller.util.Utilities; 12 13 import java.io.IOException; 14 import java.io.PrintWriter; 15 import java.io.StringWriter; 16 import java.util.StringTokenizer; 17 import java.text.SimpleDateFormat; 18 19 import javax.servlet.ServletContext; 20 import javax.servlet.http.HttpServletRequest; 21 22 /** 23 * Extends WeblogEntryData so that it can be really useful in Macros. 24 */ 25 public class WeblogEntryDataEx extends WeblogEntryData 26 { 27 private VelocityContext mVelocityContext = null; 28 private HttpServletRequest mRequest = null; 29 30 /** 31 * Constructor for WeblogEntryDataEx. 32 * @param otherData 33 */ 34 public WeblogEntryDataEx( WeblogEntryData wd, 35 VelocityContext vcontext, HttpServletRequest req ) 36 { 37 super(wd); 38 mVelocityContext = vcontext; 39 mRequest = req; 40 } 41 42 /** 43 * Constructor for WeblogEntryDataEx that will not support getText. 44 * @param otherData 45 */ 46 public WeblogEntryDataEx( WeblogEntryData wd, HttpServletRequest req) 47 { 48 super(wd); 49 mRequest = req; 50 } 51 52 /** 53 * Returns weblog entry text after passing it through Velocity. 54 * If an error occurs, the exception will be returned instead. 55 * @return Velocity processed weblog entry text, or error message 56 */ 57 public String getText() 58 { 59 StringWriter sw = new StringWriter(); 60 PrintWriter pw = new PrintWriter(sw); 61 try 62 { 63 Velocity.evaluate( 64 mVelocityContext, pw, "weblogday", super.getText() ); 65 } 66 catch ( IOException ioe ) 67 { 68 sw.write("[VELOCITY ERROR: IOExcepion]\n"); 69 ioe.printStackTrace(pw); 70 } 71 catch ( ResourceNotFoundException rnfe ) 72 { 73 sw.write("[VELOCITY ERROR: resource not found]\n"); 74 rnfe.printStackTrace(pw); 75 } 76 catch ( MethodInvocationException mie ) 77 { 78 sw.write("[VELOCITY ERROR: invoking method in weblog entry]\n"); 79 mie.printStackTrace(pw); 80 } 81 catch ( ParseErrorException pee ) 82 { 83 sw.write("[VELOCITY ERROR: parsing weblog entry]\n"); 84 pee.printStackTrace(pw); 85 } 86 return sw.toString(); 87 } 88 89 public String getPermaLink() 90 { 91 String link = null; 92 try 93 { 94 // Figure out host and port 95 boolean usePort = true; 96 ServletContext ctx = mRequest.getSession().getServletContext(); 97 String usePortStr= ctx.getInitParameter("org.roller.rss.usePort"); 98 if ( usePortStr == null || usePortStr.equals("false") ) 99 { 100 usePort = false; 101 } 102 103 RollerRequest rreq = RollerRequest.getRollerRequest(mRequest); 104 UserData ud = rreq.getUser(); 105 106 StringBuffer fullUrlSB = mRequest.getRequestURL(); 107 StringTokenizer toker = 108 new StringTokenizer(fullUrlSB.toString(),":/"); 109 toker.nextElement(); // protocol 110 String host = (String)toker.nextElement(); 111 String port = (String)toker.nextElement(); 112 113 String baseUrl = null; 114 if ( usePort ) 115 { 116 baseUrl = "http://"+host+":"+port+mRequest.getContextPath(); 117 } 118 else 119 { 120 baseUrl = "http://"+host+mRequest.getContextPath(); 121 } 122 123 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); 124 String dayString=formatter.format(getPubTime()); 125 126 link = Utilities.escapeHTML( 127 baseUrl+"/page/"+ud.getUserName()+"/"+dayString+"#"+getAnchor()); 128 } 129 catch (Exception e) 130 { 131 e.printStackTrace(); 132 } 133 return link; 134 } 135 136 public String formatPubTime( String pattern ) 137 { 138 try 139 { 140 SimpleDateFormat format = new SimpleDateFormat( pattern ); 141 return format.format( getPubTime() ); 142 } 143 catch (RuntimeException e) 144 { 145 e.printStackTrace(); 146 } 147 return "ERROR: formatting date"; 148 } 149 150 public String formatUpdateTime( String pattern ) 151 { 152 try 153 { 154 SimpleDateFormat format = new SimpleDateFormat( pattern ); 155 return format.format( getUpdateTime() ); 156 } 157 catch (RuntimeException e) 158 { 159 e.printStackTrace(); 160 } 161 return "ERROR: formatting date"; 162 } 163 }