KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.BufferedReader JavaDoc;
22 import java.io.StringReader JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.RollerException;
27 import org.apache.roller.pojos.WeblogEntryData;
28 import org.apache.roller.pojos.WebsiteData;
29 import org.apache.roller.model.WeblogEntryPlugin;
30
31
32 /**
33  * Simple page plugin that converts paragraphs of plain text into html paragraphs.
34  * We wrap each full paragraph in html <p> opening and closing tags, and
35  * also add <br> tags to the end of lines with breaks inside a paragraph.
36  *
37  * Example:
38  * This is one
39  * paragraph
40  *
41  * Becomes:
42  * <p>This is one<br/>
43  * paragraph</p>
44  *
45  */

46 public class ConvertLineBreaksPlugin implements WeblogEntryPlugin {
47     
48     private static Log mLogger = LogFactory.getLog(ConvertLineBreaksPlugin.class);
49     
50     private static final String JavaDoc name = "Convert Line Breaks";
51     private static final String JavaDoc description = "Convert plain text paragraphs to html by adding p and br tags";
52     private static final String JavaDoc version = "0.1";
53     
54     
55     public ConvertLineBreaksPlugin() {
56         mLogger.debug("Instantiating ConvertLineBreaksPlugin v"+this.version);
57     }
58     
59     
60     public String JavaDoc getName() {
61         return name;
62     }
63     
64     
65     public String JavaDoc getDescription() {
66         return description;
67     }
68     
69     
70     public void init(WebsiteData website) throws RollerException {
71         // we don't need to do any init.
72
mLogger.debug("initing");
73     }
74     
75     
76     /**
77      * Transform the given plain text into html text by inserting p and br
78      * tags around paragraphs and after line breaks.
79      */

80     public String JavaDoc render(WeblogEntryData entry, String JavaDoc str) {
81         
82         if(str == null || str.trim().equals(""))
83             return "";
84         
85         mLogger.debug("Rendering string of length "+str.length());
86         
87         /* setup a buffered reader and iterate through each line
88          * inserting html as needed
89          *
90          * NOTE: we consider a paragraph to be 2 endlines with no text between them
91          */

92         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
93         try {
94             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc(str));
95             
96             String JavaDoc line = null;
97             boolean insidePara = false;
98             while((line = br.readLine()) != null) {
99                 
100                 if(!insidePara && line.trim().length() > 0) {
101                     // start of a new paragraph
102
buf.append("\n<p>");
103                     buf.append(line);
104                     insidePara = true;
105                 } else if(insidePara && line.trim().length() > 0) {
106                     // another line in an existing paragraph
107
buf.append("<br/>\n");
108                     buf.append(line);
109                 } else if(insidePara && line.trim().length() < 1) {
110                     // end of a paragraph
111
buf.append("</p>\n\n");
112                     insidePara = false;
113                 }
114             }
115             
116             // if the text ends without an empty line then we need to
117
// terminate the last paragraph now
118
if(insidePara)
119                 buf.append("</p>\n\n");
120             
121         } catch(Exception JavaDoc e) {
122             mLogger.warn("trouble rendering text.", e);
123             return str;
124         }
125         
126         return buf.toString();
127     }
128     
129 }
130
Popular Tags