File Source: ConvertLineBreaksPlugin.java
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.weblogger.business.plugins.entry;
20
21 import java.io.BufferedReader;
22 import java.io.StringReader;
23 import java.util.Map;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.weblogger.WebloggerException;
27 import org.apache.roller.weblogger.pojos.WeblogEntry;
28 import org.apache.roller.weblogger.pojos.Weblog;
29 import org.apache.roller.weblogger.business.plugins.entry.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
/*
P/P * Method: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init
*
* Postconditions:
* init'ed(mLogger)
*/
48 private static Log mLogger = LogFactory.getLog(ConvertLineBreaksPlugin.class);
49
50 private static final String name = "Convert Line Breaks";
51 private static final String description = "Convert plain text paragraphs to html by adding p and br tags";
52 private static final String version = "0.1";
53
54
/*
P/P * Method: void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin()
*
* Preconditions:
* mLogger != null
*/
55 public ConvertLineBreaksPlugin() {
56 mLogger.debug("Instantiating ConvertLineBreaksPlugin v"+this.version);
57 }
58
59
60 public String getName() {
/*
P/P * Method: String getName()
*
* Postconditions:
* return_value == &"Convert Line Breaks"
*/
61 return name;
62 }
63
64
65 public String getDescription() {
/*
P/P * Method: String getDescription()
*
* Postconditions:
* return_value == &"Convert plain text paragraphs to html by adding p and br tags"
*/
66 return description;
67 }
68
69
70 public void init(Weblog website) throws WebloggerException {
71 // we don't need to do any init.
/*
P/P * Method: void init(Weblog)
*
* Preconditions:
* mLogger != null
*/
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 render(WeblogEntry entry, String str) {
81
/*
P/P * Method: String render(WeblogEntry, String)
*
* Preconditions:
* (soft) mLogger != null
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* return_value == One-of{&"", str, &java.lang.StringBuffer:toString(...)}
* return_value != null
*
* Test Vectors:
* str: Addr_Set{null}, Inverse{null}
* java.io.BufferedReader:readLine(...)@98: Addr_Set{null}, Inverse{null}
* java.lang.String:equals(...)@82: {0}, {1}
* java.lang.String:length(...)@100: {0}, {1..232-1}
* java.lang.String:length(...)@105: {0}, {1..232-1}
* java.lang.String:length(...)@109: {1..232-1}, {0}
*/
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 buf = new StringBuffer();
93 try {
94 BufferedReader br = new BufferedReader(new StringReader(str));
95
96 String 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 e) {
122 mLogger.warn("trouble rendering text.", e);
123 return str;
124 }
125
126 return buf.toString();
127 }
128
129 }
SofCheck Inspector Build Version : 2.18479
| ConvertLineBreaksPlugin.java |
2009-Jan-02 14:24:48 |
| ConvertLineBreaksPlugin.class |
2009-Sep-04 03:12:31 |