//# 0 errors, 52 messages
//#
/*
    //#LinkMarkupPlugin.java:1:1: class: org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin
 * 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.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.roller.weblogger.pojos.WeblogEntryComment;
import org.apache.roller.weblogger.util.Utilities;


/**
 * Comment plugin which turns plain text URLs into hyperlinks using
 * the html anchor (&lt;a&gt;) tag.
 * 
 * Contributed by Matthew Montgomery.
 */
public class LinkMarkupPlugin implements WeblogEntryCommentPlugin {
    //#LinkMarkupPlugin.java:34: method: void org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin()
    //#LinkMarkupPlugin.java:34: end of method: void org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin()

    private static final Pattern pattern = Pattern.compile(
    //#LinkMarkupPlugin.java:36: method: org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init
    //#output(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): __Descendant_Table[org/apache/roller/weblogger/business/plugins/comment/LinkMarkupPlugin]
    //#output(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): __Dispatch_Table.getDescription()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): __Dispatch_Table.getId()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): __Dispatch_Table.getName()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__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.LinkMarkupPlugin__static_init): org/apache/roller/weblogger/business/plugins/comment/WeblogEntryCommentPlugin.__Descendant_Table[org/apache/roller/weblogger/business/plugins/comment/LinkMarkupPlugin]
    //#output(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): pattern
    //#post(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): __Descendant_Table[org/apache/roller/weblogger/business/plugins/comment/LinkMarkupPlugin] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): org/apache/roller/weblogger/business/plugins/comment/WeblogEntryCommentPlugin.__Descendant_Table[org/apache/roller/weblogger/business/plugins/comment/LinkMarkupPlugin] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): __Dispatch_Table.getDescription()Ljava/lang/String; == &getDescription
    //#post(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): __Dispatch_Table.getId()Ljava/lang/String; == &getId
    //#post(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init): __Dispatch_Table.getName()Ljava/lang/String; == &getName
    //#post(org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__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.LinkMarkupPlugin__static_init): init'ed(pattern)
    //#LinkMarkupPlugin.java:36: end of method: org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init
            "http[s]?://[^/][\\S]+", Pattern.CASE_INSENSITIVE);
    
    
    /**
     * Unique identifier.  This should never change. 
     */
    public String getId() {
        return "LinkMarkup";
    //#LinkMarkupPlugin.java:44: method: String org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.getId()
    //#output(String getId()): return_value
    //#post(String getId()): return_value == &"LinkMarkup"
    //#LinkMarkupPlugin.java:44: end of method: String org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.getId()
    }
    
    
    /** Returns the display name of this Plugin. */
    public String getName() {
        return "Link Markup";
    //#LinkMarkupPlugin.java:50: method: String org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.getName()
    //#output(String getName()): return_value
    //#post(String getName()): return_value == &"Link Markup"
    //#LinkMarkupPlugin.java:50: end of method: String org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.getName()
    }
    
    
    /** Briefly describes the function of the Plugin.  May contain HTML. */
    public String getDescription() {
        return "Automatically creates hyperlinks out of embedded URLs.";
    //#LinkMarkupPlugin.java:56: method: String org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.getDescription()
    //#output(String getDescription()): return_value
    //#post(String getDescription()): return_value == &"Automatically creates hyperlinks out of embedded URLs."
    //#LinkMarkupPlugin.java:56: end of method: String org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.getDescription()
    }
    
    
    /**
     * Apply plugin to the specified text.
     *
     * @param comment The comment being rendered.
     * @param text String to which plugin should be applied.
     *
     * @return Results of applying plugin to string.
     */
    public String render(final WeblogEntryComment comment, String text) {
        StringBuilder result;
        result = new StringBuilder();
    //#LinkMarkupPlugin.java:70: method: String org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.render(WeblogEntryComment, String)
    //#input(String render(WeblogEntryComment, String)): "">"._tainted
    //#input(String render(WeblogEntryComment, String)): "<.a>"._tainted
    //#input(String render(WeblogEntryComment, String)): "<a href=""._tainted
    //#input(String render(WeblogEntryComment, String)): pattern
    //#input(String render(WeblogEntryComment, String)): text
    //#input(String render(WeblogEntryComment, String)): text._tainted
    //#output(String render(WeblogEntryComment, String)): java.lang.StringBuilder:toString(...)._tainted
    //#output(String render(WeblogEntryComment, String)): return_value
    //#new obj(String render(WeblogEntryComment, String)): java.lang.StringBuilder:toString(...)
    //#presumption(String render(WeblogEntryComment, String)): java.util.regex.Pattern:compile(...)@36 != null
    //#presumption(String render(WeblogEntryComment, String)): java.util.regex.Pattern:matcher(...)@74 != null
    //#post(String render(WeblogEntryComment, String)): init'ed(java.lang.StringBuilder:toString(...)._tainted)
    //#post(String render(WeblogEntryComment, String)): return_value == &java.lang.StringBuilder:toString(...)
    //#test_vector(String render(WeblogEntryComment, String)): text: Addr_Set{null}, Inverse{null}
    //#test_vector(String render(WeblogEntryComment, String)): java.util.regex.Matcher:find(...)@80: {0}, {1}
        
        if (text != null) {
            Matcher matcher;
            matcher = pattern.matcher(text);

            int start = 0;
            int end = text.length();
            
            while (start < end) {
                if (matcher.find()) {
                    // Copy up to the match
                    result.append(text.substring(start, (matcher.start())));

                    // Copy the URL and create the hyperlink
                    // Unescape HTML as we don't know if that setting is on
                    String url;
                    url = Utilities.unescapeHTML(text.substring(
    //#LinkMarkupPlugin.java:87: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.util.Utilities:unescapeHTML(String)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin
    //#    method: String render(WeblogEntryComment, String)
    //#    unanalyzed callee: String org.apache.roller.weblogger.util.Utilities:unescapeHTML(String)
                            matcher.start(), matcher.end()));

                    // Build the anchor tag and escape HTML in the URL output
                    result.append("<a href=\"");
                    result.append(Utilities.escapeHTML(url));
    //#LinkMarkupPlugin.java:92: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.util.Utilities:escapeHTML(String)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin
    //#    method: String render(WeblogEntryComment, String)
    //#    unanalyzed callee: String org.apache.roller.weblogger.util.Utilities:escapeHTML(String)
                    result.append("\">");
                    result.append(Utilities.escapeHTML(url));
    //#LinkMarkupPlugin.java:94: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.util.Utilities:escapeHTML(String)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin
    //#    method: String render(WeblogEntryComment, String)
    //#    unanalyzed callee: String org.apache.roller.weblogger.util.Utilities:escapeHTML(String)
                    result.append("</a>");

                    // Increment the starting index
                    start = matcher.end();
                }
                else {
                    // Copy the remainder
                    result.append(text.substring(start, end));

                    // Increment the starting index to exit the loop
                    start = end;
                }
            }
        }
        else {
            result.append(text);    
        }

        return result.toString();
    //#LinkMarkupPlugin.java:113: end of method: String org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin.render(WeblogEntryComment, String)
    }
    
}
    //#LinkMarkupPlugin.java:: end of class: org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin
