KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > plugins > readmore > ReadMorePlugin


1 /*
2  * Created on Nov 2, 2003
3  *
4  */

5 package org.roller.presentation.velocity.plugins.readmore;
6
7 import org.apache.commons.lang.StringEscapeUtils;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.apache.velocity.context.Context;
11 import org.roller.RollerException;
12 import org.roller.model.RollerFactory;
13 import org.roller.model.UserManager;
14 import org.roller.pojos.WeblogEntryData;
15 import org.roller.pojos.WebsiteData;
16 import org.roller.presentation.RollerRequest;
17 import org.roller.presentation.velocity.PagePlugin;
18 import org.roller.util.Utilities;
19
20 /**
21  * @author lance
22  */

23 public class ReadMorePlugin implements PagePlugin
24 {
25     protected String JavaDoc name = "Read More Summary";
26     protected String JavaDoc description = "Stops entry after 250 characters and creates " +
27         "a link to the full entry.";
28     
29     private static Log mLogger =
30        LogFactory.getFactory().getInstance(ReadMorePlugin.class);
31        
32     String JavaDoc ctxPath = "";
33     
34     public ReadMorePlugin()
35     {
36         mLogger.debug("ReadMorePlugin instantiated.");
37     }
38     
39     public String JavaDoc toString() { return name; }
40
41     /* (non-Javadoc)
42      * @see org.roller.presentation.velocity.PagePlugin#init(org.roller.presentation.RollerRequest, org.apache.velocity.context.Context)
43      */

44     public void init(RollerRequest rreq, Context ctx) throws RollerException
45     {
46         if (rreq == null) throw new RollerException("RollerRequest is null.");
47   
48         ctxPath = rreq.getRequest().getContextPath();
49     }
50
51     /**
52      * @param mgr
53      * @param website
54      * @return
55      */

56     private String JavaDoc getPageLink(UserManager mgr, WebsiteData website) throws RollerException
57     {
58         return mgr.retrievePage(website.getDefaultPageId()).getLink();
59     }
60
61     /*
62      * This method cannot do it's intended job (since it cannot
63      * read the current Entry) so it is to do no work!
64      *
65      * (non-Javadoc)
66      * @see org.roller.presentation.velocity.PagePlugin#render(java.lang.String)
67      */

68     public String JavaDoc render(String JavaDoc str)
69     {
70         return str;
71     }
72     
73     public String JavaDoc render(WeblogEntryData entry, boolean skipFlag)
74     {
75         if (skipFlag)
76             return entry.getText();
77         
78         // in case it didn't initialize
79
String JavaDoc pageLink = "Weblog";
80         try
81         {
82             pageLink = getPageLink(
83                 RollerFactory.getRoller().getUserManager(), entry.getWebsite());
84         }
85         catch (RollerException e)
86         {
87             mLogger.warn("Unable to get pageLink", e);
88         }
89         
90         String JavaDoc result = Utilities.removeHTML(entry.getText(), true);
91         result = Utilities.truncateText(result, 240, 260, "...");
92         //String result = Utilities.truncateNicely(entry.getText(), 240, 260, "... ");
93

94         // if the result is shorter, we need to add "Read More" link
95
if (result.length() < entry.getText().length())
96         {
97             String JavaDoc link = "<div class=\"readMore\"><a HREF=\"" +
98                 ctxPath + "/comments/" +
99                 entry.getWebsite().getUser().getUserName() +
100                 "/" + pageLink + "/" + Utilities.encode(entry.getAnchor()) +
101                 "\">Read More</a></div>";
102             
103             result += link;
104         }
105         return result;
106     }
107
108
109     public String JavaDoc getName() { return name; }
110     public String JavaDoc getDescription() { return StringEscapeUtils.escapeJavaScript(description); }
111 }
Popular Tags