//# 0 errors, 119 messages
//#
/*
    //#SearchPluginBase.java:1:1: class: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase
    //#SearchPluginBase.java:1:1: method: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init
 * 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 org.apache.commons.logging.Log;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.pojos.WeblogEntry;
import org.apache.roller.weblogger.pojos.Weblog;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.FieldPosition;
import java.text.MessageFormat;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Implements the common portion of search link plugins.
 *
 * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
 * @version 2.1
 */
public abstract class SearchPluginBase {
    private String baseVersion = "2.1";
    private Log mLogger;

    /**
     * Instantiation is per request.
     */
    public SearchPluginBase() {
    //#SearchPluginBase.java:47: method: void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()
    //#SearchPluginBase.java:47: Warning: suspicious precondition
    //#    the precondition for this.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase
    //#    method: void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()
    //#    suspicious precondition index: [2]
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin]
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/SearchPluginBase]
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin]
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): __Descendant_Table[others]
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): __Dispatch_Table.getLogger()Lorg/apache/commons/logging/Log;
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.__Dispatch_Table.getLogger()Lorg/apache/commons/logging/Log;
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.mLogger
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.__Dispatch_Table.getLogger()Lorg/apache/commons/logging/Log;
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.mLogger
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): this
    //#input(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): this.__Tag
    //#output(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): this.baseVersion
    //#output(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): this.mLogger
    //#pre[2] (void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): this.__Tag in {org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin, org/apache/roller/weblogger/business/plugins/entry/SearchPluginBase, org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin}
    //#post(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): this.baseVersion == &"2.1"
    //#post(void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()): init'ed(this.mLogger)
        mLogger = getLogger();
    }
    //#SearchPluginBase.java:49: end of method: void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()

    /**
     * Initialize.  Called once for each request.
     *
     * @see org.apache.roller.weblogger.model.PagePlugin#init(WebsiteData, Object, String baseUrl, org.apache.velocity.context.Context)
     */
    public void init(Weblog website) throws WebloggerException {
        if (mLogger.isDebugEnabled()) {
    //#SearchPluginBase.java:57: method: void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.init(Weblog)
    //#SearchPluginBase.java:57: Warning: method not available
    //#    -- call on bool org.apache.commons.logging.Log:isDebugEnabled()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase
    //#    method: void init(Weblog)
    //#    unanalyzed callee: bool org.apache.commons.logging.Log:isDebugEnabled()
    //#SearchPluginBase.java:57: Warning: suspicious precondition
    //#    the precondition for this.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase
    //#    method: void init(Weblog)
    //#    suspicious precondition index: [2]
    //#    Attribs:  Soft
    //#input(void init(Weblog)): "2.1"._tainted
    //#input(void init(Weblog)): "; base version "._tainted
    //#input(void init(Weblog)): "; version:  "._tainted
    //#input(void init(Weblog)): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin]
    //#input(void init(Weblog)): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/SearchPluginBase]
    //#input(void init(Weblog)): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin]
    //#input(void init(Weblog)): __Descendant_Table[others]
    //#input(void init(Weblog)): __Dispatch_Table.getVersion()Ljava/lang/String;
    //#input(void init(Weblog)): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.__Dispatch_Table.getVersion()Ljava/lang/String;
    //#input(void init(Weblog)): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.__Dispatch_Table.getVersion()Ljava/lang/String;
    //#input(void init(Weblog)): this
    //#input(void init(Weblog)): this.__Tag
    //#input(void init(Weblog)): this.baseVersion
    //#input(void init(Weblog)): this.baseVersion._tainted
    //#input(void init(Weblog)): this.mLogger
    //#pre[5] (void init(Weblog)): this.mLogger != null
    //#pre[2] (void init(Weblog)): (soft) this.__Tag in {org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin, org/apache/roller/weblogger/business/plugins/entry/SearchPluginBase, org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin}
    //#pre[3] (void init(Weblog)): (soft) init'ed(this.baseVersion)
    //#presumption(void init(Weblog)): java.lang.Object:getClass(...)@58 != null
    //#test_vector(void init(Weblog)): org.apache.commons.logging.Log:isDebugEnabled(...)@57: {0}, {1}
            mLogger.debug(getClass().getName() + "; version:  " + getVersion() + "; base version " + baseVersion);
    //#SearchPluginBase.java:58: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase
    //#    method: void init(Weblog)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
        }
    }
    //#SearchPluginBase.java:60: end of method: void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.init(Weblog)
    

    /**
     * Apply plugin to content of specified String.
     *
     * @param str String to which plugin should be applied.
     * @return Results of applying plugin to string.
     * @see org.apache.roller.weblogger.model.PagePlugin#render(String)
     */
    public String render(WeblogEntry entry, String str) {
        Pattern pattern = getPattern();
    //#SearchPluginBase.java:71: method: String org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.render(WeblogEntry, String)
    //#SearchPluginBase.java:71: Warning: suspicious precondition
    //#    the precondition for this.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase
    //#    method: String render(WeblogEntry, String)
    //#    suspicious precondition index: [3]
    //#input(String render(WeblogEntry, String)): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin]
    //#input(String render(WeblogEntry, String)): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/SearchPluginBase]
    //#input(String render(WeblogEntry, String)): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin]
    //#input(String render(WeblogEntry, String)): __Descendant_Table[others]
    //#input(String render(WeblogEntry, String)): __Dispatch_Table.getLinkFormat()Ljava/text/MessageFormat;
    //#input(String render(WeblogEntry, String)): __Dispatch_Table.getLuckyLinkFormat()Ljava/text/MessageFormat;
    //#input(String render(WeblogEntry, String)): __Dispatch_Table.getPattern()Ljava/util/regex/Pattern;
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.__Dispatch_Table.getLinkFormat()Ljava/text/MessageFormat;
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.__Dispatch_Table.getLuckyLinkFormat()Ljava/text/MessageFormat;
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.__Dispatch_Table.getPattern()Ljava/util/regex/Pattern;
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.linkFormat
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.luckyLinkFormat
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin.pattern
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.__Dispatch_Table.getLinkFormat()Ljava/text/MessageFormat;
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.__Dispatch_Table.getLuckyLinkFormat()Ljava/text/MessageFormat;
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.__Dispatch_Table.getPattern()Ljava/util/regex/Pattern;
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.linkFormat
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.luckyLinkFormat
    //#input(String render(WeblogEntry, String)): org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin.pattern
    //#input(String render(WeblogEntry, String)): str
    //#input(String render(WeblogEntry, String)): this
    //#input(String render(WeblogEntry, String)): this.__Tag
    //#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)): str != null
    //#pre[3] (String render(WeblogEntry, String)): this.__Tag in {org/apache/roller/weblogger/business/plugins/entry/GoogleLinkPlugin, org/apache/roller/weblogger/business/plugins/entry/SearchPluginBase, org/apache/roller/weblogger/business/plugins/entry/WikipediaLinkPlugin}
    //#presumption(String render(WeblogEntry, String)): java.lang.String:length(...)@73 <= 4_294_967_167
    //#presumption(String render(WeblogEntry, String)): java.util.regex.Matcher:group(...)@77 != null
    //#presumption(String render(WeblogEntry, String)): java.util.regex.Pattern:compile(...)@43 != null
    //#presumption(String render(WeblogEntry, String)): java.util.regex.Pattern:compile(...)@43 != null
    //#presumption(String render(WeblogEntry, String)): java.util.regex.Pattern:matcher(...)@72 != null
    //#post(String render(WeblogEntry, String)): java.lang.StringBuffer:toString(...)._tainted == 0
    //#post(String render(WeblogEntry, String)): return_value == &java.lang.StringBuffer:toString(...)
    //#unanalyzed(String render(WeblogEntry, String)): Effects-of-calling:java.net.URLEncoder:encode
    //#unanalyzed(String render(WeblogEntry, String)): Effects-of-calling:java.lang.Throwable:__curr_excep_obj
    //#unanalyzed(String render(WeblogEntry, String)): Effects-of-calling:java.lang.RuntimeException
    //#test_vector(String render(WeblogEntry, String)): java.lang.String:equals(...)@78: {0}, {1}
    //#test_vector(String render(WeblogEntry, String)): java.lang.String:length(...)@81: {1..4_294_967_295}, {0}
    //#test_vector(String render(WeblogEntry, String)): java.util.regex.Matcher:find(...)@75: {0}, {1}
    //#test_vector(String render(WeblogEntry, String)): java.util.regex.Matcher:group(...)@80: Addr_Set{null}, Inverse{null}
        Matcher m = pattern.matcher(str);
        StringBuffer result = new StringBuffer(str.length() + 128);   // rough guess at a reasonable length
        Object[] args = new Object[]{"", "", null, null};
        while (m.find()) {
            // parse out the parts of the match
            String type = m.group(1);
            boolean feelinLucky = type.equals("!");   // are ya feelin lucky? are ya punk?
            String linkText = m.group(2);
            String searchText = m.group(3);
            if (searchText == null || searchText.length() == 0) {
                searchText = linkText;
            }

            // URL-encode the search text
            String encodedSearchText = encodeSearchText(searchText);

            // form the replacement string
            MessageFormat linkFormat = feelinLucky ? getLuckyLinkFormat() : getLinkFormat();
            StringBuffer replacement = new StringBuffer(128);
            args[2] = linkText;
            args[3] = encodedSearchText;
            linkFormat.format(args, replacement, new FieldPosition(0));

            // append replacement
            m.appendReplacement(result, replacement.toString());
        }
        m.appendTail(result);

        return result.toString();
    //#SearchPluginBase.java:100: end of method: String org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.render(WeblogEntry, String)
    }

    /**
     * Returns the human-friendly name of this Plugin. This is what users will see.
     *
     * @return The human-friendly name of this Plugin.
     * @see org.apache.roller.weblogger.model.PagePlugin#getName()
     */
    public abstract String getName();

    /**
     * Briefly describes the function of the Plugin. May contain HTML.
     *
     * @return A brief description of the Plugin.
     * @see org.apache.roller.weblogger.model.PagePlugin#getDescription()
     */
    public abstract String getDescription();

    /**
     * Return the logger for this class.
     *
     * @return the logger for this class.
     */
    protected abstract Log getLogger();

    /**
     * Return the implementation version.
     *
     * @return the implementation version.
     */
    protected abstract String getVersion();

    /**
     * Get the regexp pattern for finding search links in the input text.   Three matching groups are expected: (1) The
     * lucky or not indicator (either <code>!</code> or <code>:</code>) (2) the link text (3) the search text (optional,
     * defaults to the link text).
     *
     * @return the regexp pattern for finding search links in the input text
     */
    protected abstract Pattern getPattern();

    /**
     * The MessageFormat for the replacement string (actual HTML link) that will form the replacement in the regular
     * (non-"lucky") case.  This must have two positional parameters "{2} and {3}" which are the link text and
     * (URL-encoded) search text from the regexp pattern.  Note that the parameters "{0}" and "{1}" are not used. They
     * will be empty strings.
     *
     * @return the message format for non-"lucky" search links.
     */
    protected abstract MessageFormat getLinkFormat();

    /**
     * The MessageFormat for the replacement string (actual HTML link) that will form the replacement in the "lucky"
     * case.  This must have two positional parameters "{2} and {3}" which are the link text and (URL-encoded) search
     * text from the regexp pattern.  Note that the parameters "{0}" and "{1}" are not used. They will be empty
     * strings.
     *
     * @return the message format for "lucky" search links.
     */
    protected abstract MessageFormat getLuckyLinkFormat();


    // Private helper to URL encode the search text.
    private String encodeSearchText(String searchText) {
        // URL encode the searchtext
        try {
            return URLEncoder.encode(searchText, "UTF-8");
    //#SearchPluginBase.java:167: method: String org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.encodeSearchText(String)
    //#input(String encodeSearchText(String)): searchText
    //#output(String encodeSearchText(String)): return_value
    //#post(String encodeSearchText(String)): init'ed(return_value)
        } catch (UnsupportedEncodingException uex) {
            // By Java spec, this should never actually occur for UTF-8.  If it does, we barf bitterly.
            throw new RuntimeException(uex);
    //#SearchPluginBase.java:170: end of method: String org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.encodeSearchText(String)
        }
    }
    
}
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/SearchPluginBase]
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.encodeSearchText(Ljava/lang/String;)Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getDescription()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getLinkFormat()Ljava/text/MessageFormat;
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getLogger()Lorg/apache/commons/logging/Log;
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getLuckyLinkFormat()Ljava/text/MessageFormat;
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getName()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getPattern()Ljava/util/regex/Pattern;
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getVersion()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.init(Lorg/apache/roller/weblogger/pojos/Weblog;)V
    //#output(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.render(Lorg/apache/roller/weblogger/pojos/WeblogEntry;Ljava/lang/String;)Ljava/lang/String;
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Descendant_Table[org/apache/roller/weblogger/business/plugins/entry/SearchPluginBase] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.encodeSearchText(Ljava/lang/String;)Ljava/lang/String; == &encodeSearchText
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getDescription()Ljava/lang/String; == &getDescription
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getLinkFormat()Ljava/text/MessageFormat; == &getLinkFormat
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getLogger()Lorg/apache/commons/logging/Log; == &getLogger
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getLuckyLinkFormat()Ljava/text/MessageFormat; == &getLuckyLinkFormat
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getName()Ljava/lang/String; == &getName
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getPattern()Ljava/util/regex/Pattern; == &getPattern
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.getVersion()Ljava/lang/String; == &getVersion
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.init(Lorg/apache/roller/weblogger/pojos/Weblog;)V == &init
    //#post(org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init): __Dispatch_Table.render(Lorg/apache/roller/weblogger/pojos/WeblogEntry;Ljava/lang/String;)Ljava/lang/String; == &render
    //#SearchPluginBase.java:: end of method: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase.org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init
    //#SearchPluginBase.java:: end of class: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase
