File Source: AutoformatPlugin.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.comment;
20
21 import java.io.BufferedReader;
22 import java.io.StringReader;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.roller.weblogger.pojos.WeblogEntryComment;
26
27
28 /**
29 * Comment plugin which turns plain text paragraph formatting into html
30 * paragraph formatting using <p> and <br/> tags.
31 */
32 public class AutoformatPlugin implements WeblogEntryCommentPlugin {
33
/*
P/P * Method: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init
*
* Postconditions:
* init'ed(log)
*/
34 private static final Log log = LogFactory.getLog(AutoformatPlugin.class);
35
36
/*
P/P * Method: void org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin()
*/
37 public AutoformatPlugin() {
38 // no-op
39 }
40
41
42 /**
43 * Unique identifier. This should never change.
44 */
45 public String getId() {
/*
P/P * Method: String getId()
*
* Postconditions:
* return_value == &"AutoFormat"
*/
46 return "AutoFormat";
47 }
48
49
50 public String getName() {
/*
P/P * Method: String getName()
*
* Postconditions:
* return_value == &"Auto Format"
*/
51 return "Auto Format";
52 }
53
54
55 public String getDescription() {
/*
P/P * Method: String getDescription()
*
* Postconditions:
* return_value == &"Converts plain text style paragraphs into html paragraphs."
*/
56 return "Converts plain text style paragraphs into html paragraphs.";
57 }
58
59
60 public String render(final WeblogEntryComment comment, String text) {
61
/*
P/P * Method: String render(WeblogEntryComment, String)
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@34 != null
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* return_value == &java.lang.StringBuffer:toString(...)
*
* Test Vectors:
* java.io.BufferedReader:readLine(...)@76: Addr_Set{null}, Inverse{null}
* java.lang.String:length(...)@78: {0}, {1..232-1}
* java.lang.String:length(...)@83: {0}, {1..232-1}
* java.lang.String:length(...)@87: {1..232-1}, {0}
*/
62 log.debug("starting value:\n"+text);
63
64 /*
65 * setup a buffered reader and iterate through each line
66 * inserting html as needed
67 *
68 * NOTE: we consider a paragraph to be 2 endlines with no text between them
69 */
70 StringBuffer buf = new StringBuffer();
71 try {
72 BufferedReader br = new BufferedReader(new StringReader(text));
73
74 String line = null;
75 boolean insidePara = false;
76 while((line = br.readLine()) != null) {
77
78 if(!insidePara && line.trim().length() > 0) {
79 // start of a new paragraph
80 buf.append("\n<p>");
81 buf.append(line);
82 insidePara = true;
83 } else if(insidePara && line.trim().length() > 0) {
84 // another line in an existing paragraph
85 buf.append("<br/>\n");
86 buf.append(line);
87 } else if(insidePara && line.trim().length() < 1) {
88 // end of a paragraph
89 buf.append("</p>\n\n");
90 insidePara = false;
91 }
92 }
93
94 // if the text ends without an empty line then we need to
95 // terminate the last paragraph now
96 if(insidePara)
97 buf.append("</p>\n\n");
98
99 } catch(Exception e) {
100 log.warn("trouble rendering text.", e);
101 }
102
103 log.debug("ending value:\n"+buf.toString());
104
105 return buf.toString();
106 }
107
108 }
SofCheck Inspector Build Version : 2.18479
| AutoformatPlugin.java |
2009-Jan-02 14:24:56 |
| AutoformatPlugin.class |
2009-Sep-04 03:12:31 |