KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > plugins > ReadMorePlugin


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.plugins;
20
21 import org.apache.commons.lang.StringEscapeUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.roller.RollerException;
25 import org.apache.roller.pojos.WeblogEntryData;
26 import org.apache.roller.pojos.WebsiteData;
27 import org.apache.roller.model.WeblogEntryPlugin;
28 import org.apache.roller.util.Utilities;
29
30
31 /**
32  * Truncates the string passed in and applies a "Read More" link if the
33  * text is longer than the truncation limit.
34  */

35 public class ReadMorePlugin implements WeblogEntryPlugin {
36     
37     private static Log log = LogFactory.getLog(ReadMorePlugin.class);
38     
39     private String JavaDoc name = "Read More Summary";
40     private String JavaDoc description = "Stops entry after 250 characters and creates " +
41             "a link to the full entry.";
42     
43     
44     public ReadMorePlugin() {
45         log.debug("ReadMorePlugin instantiated.");
46     }
47     
48     
49     public String JavaDoc getName() {
50         return name;
51     }
52     
53     public String JavaDoc getDescription() {
54         return StringEscapeUtils.escapeJavaScript(description);
55     }
56     
57     
58     public void init(WebsiteData website) throws RollerException {
59         // no-op
60
}
61     
62     
63     public String JavaDoc render(WeblogEntryData entry, String JavaDoc str) {
64         
65         String JavaDoc result = Utilities.removeHTML(str, true);
66         result = Utilities.truncateText(result, 240, 260, "...");
67         
68         // if the result is shorter, we need to add "Read More" link
69
if (result.length() < str.length()) {
70             String JavaDoc link = "<div class=\"readMore\"><a HREF=\"" +
71                     entry.getPermalink() + "\">Read More</a></div>";
72             
73             result += link;
74         }
75         
76         return result;
77     }
78     
79 }
80
Popular Tags