//# 0 errors, 57 messages
//#
/*
    //#AutoformatPlugin.java:1:1: class: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  The ASF licenses this file to You
 * under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

package org.apache.roller.weblogger.business.plugins.comment;

import java.io.BufferedReader;
import java.io.StringReader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.pojos.WeblogEntryComment;


/**
 * Comment plugin which turns plain text paragraph formatting into html
 * paragraph formatting using <p> and <br/> tags.
 */
public class AutoformatPlugin implements WeblogEntryCommentPlugin {
    
    private static final Log log = LogFactory.getLog(AutoformatPlugin.class);
    //#AutoformatPlugin.java:34: method: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init
    //#AutoformatPlugin.java:34: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin
    //#    method: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Descendant_Table[org/apache/roller/weblogger/business/plugins/comment/AutoformatPlugin]
    //#output(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Dispatch_Table.getDescription()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Dispatch_Table.getId()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Dispatch_Table.getName()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Dispatch_Table.render(Lorg/apache/roller/weblogger/pojos/WeblogEntryComment;Ljava/lang/String;)Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): log
    //#output(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): org/apache/roller/weblogger/business/plugins/comment/WeblogEntryCommentPlugin.__Descendant_Table[org/apache/roller/weblogger/business/plugins/comment/AutoformatPlugin]
    //#post(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Descendant_Table[org/apache/roller/weblogger/business/plugins/comment/AutoformatPlugin] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): org/apache/roller/weblogger/business/plugins/comment/WeblogEntryCommentPlugin.__Descendant_Table[org/apache/roller/weblogger/business/plugins/comment/AutoformatPlugin] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Dispatch_Table.getDescription()Ljava/lang/String; == &getDescription
    //#post(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Dispatch_Table.getId()Ljava/lang/String; == &getId
    //#post(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Dispatch_Table.getName()Ljava/lang/String; == &getName
    //#post(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): __Dispatch_Table.render(Lorg/apache/roller/weblogger/pojos/WeblogEntryComment;Ljava/lang/String;)Ljava/lang/String; == &render
    //#post(org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init): init'ed(log)
    //#AutoformatPlugin.java:34: end of method: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin__static_init
    
    
    public AutoformatPlugin() {
    //#AutoformatPlugin.java:37: method: void org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin()
        // no-op
    }
    //#AutoformatPlugin.java:39: end of method: void org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin()
    
    
    /**
     * Unique identifier.  This should never change. 
     */
    public String getId() {
        return "AutoFormat";
    //#AutoformatPlugin.java:46: method: String org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.getId()
    //#output(String getId()): return_value
    //#post(String getId()): return_value == &"AutoFormat"
    //#AutoformatPlugin.java:46: end of method: String org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.getId()
    }
    
    
    public String getName() {
        return "Auto Format";
    //#AutoformatPlugin.java:51: method: String org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.getName()
    //#output(String getName()): return_value
    //#post(String getName()): return_value == &"Auto Format"
    //#AutoformatPlugin.java:51: end of method: String org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.getName()
    }
    
    
    public String getDescription() {
        return "Converts plain text style paragraphs into html paragraphs.";
    //#AutoformatPlugin.java:56: method: String org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.getDescription()
    //#output(String getDescription()): return_value
    //#post(String getDescription()): return_value == &"Converts plain text style paragraphs into html paragraphs."
    //#AutoformatPlugin.java:56: end of method: String org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.getDescription()
    }
    
    
    public String render(final WeblogEntryComment comment, String text) {
        
        log.debug("starting value:\n"+text);
    //#AutoformatPlugin.java:62: method: String org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.render(WeblogEntryComment, String)
    //#AutoformatPlugin.java:62: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin
    //#    method: String render(WeblogEntryComment, String)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
    //#input(String render(WeblogEntryComment, String)): "&#10;<p>"._tainted
    //#input(String render(WeblogEntryComment, String)): "<.p>&#10;&#10;"._tainted
    //#input(String render(WeblogEntryComment, String)): "<br.>&#10;"._tainted
    //#input(String render(WeblogEntryComment, String)): "ending value:&#10;"._tainted
    //#input(String render(WeblogEntryComment, String)): "starting value:&#10;"._tainted
    //#input(String render(WeblogEntryComment, String)): log
    //#input(String render(WeblogEntryComment, String)): text
    //#input(String render(WeblogEntryComment, String)): text._tainted
    //#output(String render(WeblogEntryComment, String)): java.lang.StringBuffer:toString(...)._tainted
    //#output(String render(WeblogEntryComment, String)): return_value
    //#new obj(String render(WeblogEntryComment, String)): java.lang.StringBuffer:toString(...)
    //#presumption(String render(WeblogEntryComment, String)): org.apache.commons.logging.LogFactory:getLog(...)@34 != null
    //#post(String render(WeblogEntryComment, String)): java.lang.StringBuffer:toString(...)._tainted == 0
    //#post(String render(WeblogEntryComment, String)): return_value == &java.lang.StringBuffer:toString(...)
    //#test_vector(String render(WeblogEntryComment, String)): java.io.BufferedReader:readLine(...)@76: Addr_Set{null}, Inverse{null}
    //#test_vector(String render(WeblogEntryComment, String)): java.lang.String:length(...)@78: {0}, {1..4_294_967_295}
    //#test_vector(String render(WeblogEntryComment, String)): java.lang.String:length(...)@83: {0}, {1..4_294_967_295}
    //#test_vector(String render(WeblogEntryComment, String)): java.lang.String:length(...)@87: {1..4_294_967_295}, {0}
        
        /* 
         * setup a buffered reader and iterate through each line
         * inserting html as needed
         *
         * NOTE: we consider a paragraph to be 2 endlines with no text between them
         */
        StringBuffer buf = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new StringReader(text));
            
            String line = null;
    //#AutoformatPlugin.java:74: Warning: unused assignment
    //#    unused assignment into line
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin
    //#    method: String render(WeblogEntryComment, String)
    //#    Attribs:  Uncertain
            boolean insidePara = false;
            while((line = br.readLine()) != null) {
                
                if(!insidePara && line.trim().length() > 0) {
                    // start of a new paragraph
                    buf.append("\n<p>");
                    buf.append(line);
                    insidePara = true;
                } else if(insidePara && line.trim().length() > 0) {
                    // another line in an existing paragraph
                    buf.append("<br/>\n");
                    buf.append(line);
                } else if(insidePara && line.trim().length() < 1) {
                    // end of a paragraph
                    buf.append("</p>\n\n");
                    insidePara = false;
                }
            }
            
            // if the text ends without an empty line then we need to
            // terminate the last paragraph now
            if(insidePara)
                buf.append("</p>\n\n");
            
        } catch(Exception e) {
            log.warn("trouble rendering text.", e);
    //#AutoformatPlugin.java:100: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:warn(Object, Throwable)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin
    //#    method: String render(WeblogEntryComment, String)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:warn(Object, Throwable)
        }
        
        log.debug("ending value:\n"+buf.toString());
    //#AutoformatPlugin.java:103: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin
    //#    method: String render(WeblogEntryComment, String)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
        
        return buf.toString();
    //#AutoformatPlugin.java:105: end of method: String org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin.render(WeblogEntryComment, String)
    }
    
}
    //#AutoformatPlugin.java:: end of class: org.apache.roller.weblogger.business.plugins.comment.AutoformatPlugin
