//# 0 errors, 63 messages
//#
/*
    //#ConvertLineBreaksPlugin.java:1:1: class: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin
 * 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.entry;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.pojos.WeblogEntry;
import org.apache.roller.weblogger.pojos.Weblog;
import org.apache.roller.weblogger.business.plugins.entry.WeblogEntryPlugin;


/**
 * Simple page plugin that converts paragraphs of plain text into html paragraphs.
 * We wrap each full paragraph in html &lt;p&gt; opening and closing tags, and
 * also add &lt;br&gt; tags to the end of lines with breaks inside a paragraph.
 *
 * Example:
 * This is one
 * paragraph
 *
 * Becomes:
 * &lt;p&gt;This is one&lt;br/&gt;
 * paragraph&lt;/p&gt;
 *
 */
public class ConvertLineBreaksPlugin implements WeblogEntryPlugin {
    
    private static Log mLogger = LogFactory.getLog(ConvertLineBreaksPlugin.class);
    //#ConvertLineBreaksPlugin.java:48: method: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init
    //#ConvertLineBreaksPlugin.java:48: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin
    //#    method: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/ConvertLineBreaksPlugin]
    //#output(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Dispatch_Table.getDescription()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Dispatch_Table.getName()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Dispatch_Table.init(Lorg/apache/roller/weblogger/pojos/Weblog;)V
    //#output(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Dispatch_Table.render(Lorg/apache/roller/weblogger/pojos/WeblogEntry;Ljava/lang/String;)Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): mLogger
    //#output(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): org/apache/roller/weblogger/business/plugins/entry/WeblogEntryPlugin.__Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/ConvertLineBreaksPlugin]
    //#post(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/ConvertLineBreaksPlugin] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): org/apache/roller/weblogger/business/plugins/entry/WeblogEntryPlugin.__Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/ConvertLineBreaksPlugin] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Dispatch_Table.getDescription()Ljava/lang/String; == &getDescription
    //#post(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Dispatch_Table.getName()Ljava/lang/String; == &getName
    //#post(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Dispatch_Table.init(Lorg/apache/roller/weblogger/pojos/Weblog;)V == &init
    //#post(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): __Dispatch_Table.render(Lorg/apache/roller/weblogger/pojos/WeblogEntry;Ljava/lang/String;)Ljava/lang/String; == &render
    //#post(org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init): init'ed(mLogger)
    //#ConvertLineBreaksPlugin.java:48: end of method: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin__static_init
    
    private static final String name = "Convert Line Breaks";
    private static final String description = "Convert plain text paragraphs to html by adding p and br tags";
    private static final String version = "0.1";
    
    
    public ConvertLineBreaksPlugin() {
    //#ConvertLineBreaksPlugin.java:55: method: void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin()
    //#input(void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin()): "0.1"._tainted
    //#input(void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin()): "Instantiating ConvertLineBreaksPlugin v"._tainted
    //#input(void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin()): mLogger
    //#pre[1] (void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin()): mLogger != null
        mLogger.debug("Instantiating ConvertLineBreaksPlugin v"+this.version);
    //#ConvertLineBreaksPlugin.java:56: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin
    //#    method: void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin()
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
    }
    //#ConvertLineBreaksPlugin.java:57: end of method: void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin()
    
    
    public String getName() {
        return name;
    //#ConvertLineBreaksPlugin.java:61: method: String org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.getName()
    //#output(String getName()): return_value
    //#post(String getName()): return_value == &"Convert Line Breaks"
    //#ConvertLineBreaksPlugin.java:61: end of method: String org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.getName()
    }
    
    
    public String getDescription() {
        return description;
    //#ConvertLineBreaksPlugin.java:66: method: String org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.getDescription()
    //#output(String getDescription()): return_value
    //#post(String getDescription()): return_value == &"Convert plain text paragraphs to html by adding p and br tags"
    //#ConvertLineBreaksPlugin.java:66: end of method: String org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.getDescription()
    }
    
    
    public void init(Weblog website) throws WebloggerException {
        // we don't need to do any init.
        mLogger.debug("initing");
    //#ConvertLineBreaksPlugin.java:72: method: void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.init(Weblog)
    //#ConvertLineBreaksPlugin.java:72: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin
    //#    method: void init(Weblog)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
    //#input(void init(Weblog)): mLogger
    //#pre[1] (void init(Weblog)): mLogger != null
    }
    //#ConvertLineBreaksPlugin.java:73: end of method: void org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.init(Weblog)
    
    
    /**
     * Transform the given plain text into html text by inserting p and br
     * tags around paragraphs and after line breaks.
     */
    public String render(WeblogEntry entry, String str) {
        
        if(str == null || str.trim().equals(""))
    //#ConvertLineBreaksPlugin.java:82: method: String org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.render(WeblogEntry, String)
    //#input(String render(WeblogEntry, String)): "&#10;<p>"._tainted
    //#input(String render(WeblogEntry, String)): "<.p>&#10;&#10;"._tainted
    //#input(String render(WeblogEntry, String)): "<br.>&#10;"._tainted
    //#input(String render(WeblogEntry, String)): "Rendering string of length "._tainted
    //#input(String render(WeblogEntry, String)): mLogger
    //#input(String render(WeblogEntry, String)): str
    //#output(String render(WeblogEntry, String)): java.lang.StringBuffer:toString(...)._tainted
    //#output(String render(WeblogEntry, String)): return_value
    //#new obj(String render(WeblogEntry, String)): java.lang.StringBuffer:toString(...)
    //#pre[1] (String render(WeblogEntry, String)): (soft) mLogger != null
    //#post(String render(WeblogEntry, String)): java.lang.StringBuffer:toString(...)._tainted == 0
    //#post(String render(WeblogEntry, String)): return_value == One-of{&"", str, &java.lang.StringBuffer:toString(...)}
    //#post(String render(WeblogEntry, String)): return_value != null
    //#test_vector(String render(WeblogEntry, String)): str: Addr_Set{null}, Inverse{null}
    //#test_vector(String render(WeblogEntry, String)): java.io.BufferedReader:readLine(...)@98: Addr_Set{null}, Inverse{null}
    //#test_vector(String render(WeblogEntry, String)): java.lang.String:equals(...)@82: {0}, {1}
    //#test_vector(String render(WeblogEntry, String)): java.lang.String:length(...)@100: {0}, {1..4_294_967_295}
    //#test_vector(String render(WeblogEntry, String)): java.lang.String:length(...)@105: {0}, {1..4_294_967_295}
    //#test_vector(String render(WeblogEntry, String)): java.lang.String:length(...)@109: {1..4_294_967_295}, {0}
            return "";
        
        mLogger.debug("Rendering string of length "+str.length());
    //#ConvertLineBreaksPlugin.java:85: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin
    //#    method: String render(WeblogEntry, String)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
        
        /* 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(str));
            
            String line = null;
    //#ConvertLineBreaksPlugin.java:96: Warning: unused assignment
    //#    unused assignment into line
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin
    //#    method: String render(WeblogEntry, 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) {
            mLogger.warn("trouble rendering text.", e);
    //#ConvertLineBreaksPlugin.java:122: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:warn(Object, Throwable)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin
    //#    method: String render(WeblogEntry, String)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:warn(Object, Throwable)
            return str;
        }
        
        return buf.toString();
    //#ConvertLineBreaksPlugin.java:126: end of method: String org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin.render(WeblogEntry, String)
    }
    
}
    //#ConvertLineBreaksPlugin.java:: end of class: org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin
