You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ga...@apache.org on 2005/12/09 17:56:30 UTC

svn commit: r355536 - in /incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search: ./ GoogleLinkPlugin.java SearchPluginBase.java WikipediaLinkPlugin.java

Author: gangolli
Date: Fri Dec  9 08:54:52 2005
New Revision: 355536

URL: http://svn.apache.org/viewcvs?rev=355536&view=rev
Log:
Added search link plugins.

Added:
    incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/
    incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/GoogleLinkPlugin.java
    incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/SearchPluginBase.java
    incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/WikipediaLinkPlugin.java

Added: incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/GoogleLinkPlugin.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/GoogleLinkPlugin.java?rev=355536&view=auto
==============================================================================
--- incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/GoogleLinkPlugin.java (added)
+++ incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/GoogleLinkPlugin.java Fri Dec  9 08:54:52 2005
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2004,2005
+ * Anil R. Gangolli. All rights reserved.
+ *
+ * Distributed with Roller Weblogger under the terms of the Roller License.
+ */
+
+package org.roller.presentation.velocity.plugins.search;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.roller.model.PagePlugin;
+
+import java.text.MessageFormat;
+import java.util.regex.Pattern;
+
+/**
+ * Google Link Plugin.  This plugin provides a convenient way to write google search links.
+ * <p/>
+ * The plugin will replace strings of the form <code>google:"link text"{search text}</code> with a link that performs a
+ * Google search.  The link will have the visible text "link text" and an href for the Google search.  You may omit the
+ * <code>{search text}</code> portion, and the link text will be used as the search text.   You can also use an
+ * exclamation point (<code>!</code>) instead of the colon (<code>:</code>), to get a lucky (&quot;I'm feeling
+ * lucky&quot;) search, which takes the user directly to the highest ranked Google match.
+ *
+ * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
+ * @version 2.1
+ */
+public class GoogleLinkPlugin extends SearchPluginBase implements PagePlugin {
+    private static final String version = "2.1";
+    private static final Pattern pattern = Pattern.compile("google([:!])\"(.*?)\"(?:\\{(.*?)\\})?");
+    private static final MessageFormat linkFormat = new MessageFormat("<a href=\"http://www.google.com/search?ie=UTF-8&q={3}\">{2}</a>");
+    private static final MessageFormat luckyLinkFormat = new MessageFormat("<a href=\"http://www.google.com/search?ie=UTF-8&q={3}&btnI=on\">{2}</a>");
+
+    private static final Log mLogger = LogFactory.getFactory().getInstance(GoogleLinkPlugin.class);
+
+    public GoogleLinkPlugin() {
+    }
+
+    public String getName() {
+        return "Google Links";
+    }
+
+    public String getDescription() {
+        return "Replace google:&quot;link text&quot;{search text} with a link that performs a google search.  With ! instead of :," + "creates a &quot;I\\'m feeling lucky&quot; search.  With {search text} omitted, uses link text as the value of the search text.";
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public Pattern getPattern() {
+        return pattern;
+    }
+
+    public MessageFormat getLinkFormat() {
+        return linkFormat;
+    }
+
+    public MessageFormat getLuckyLinkFormat() {
+        return luckyLinkFormat;
+    }
+
+    public Log getLogger() {
+        return mLogger;
+    }
+}

Added: incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/SearchPluginBase.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/SearchPluginBase.java?rev=355536&view=auto
==============================================================================
--- incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/SearchPluginBase.java (added)
+++ incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/SearchPluginBase.java Fri Dec  9 08:54:52 2005
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2004,2005
+ * Anil R. Gangolli. All rights reserved.
+ *
+ * Distributed with Roller Weblogger under the terms of the Roller License.
+ */
+
+package org.roller.presentation.velocity.plugins.search;
+
+import org.apache.commons.logging.Log;
+import org.apache.velocity.context.Context;
+import org.roller.RollerException;
+import org.roller.pojos.WeblogEntryData;
+import org.roller.pojos.WebsiteData;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.text.FieldPosition;
+import java.text.MessageFormat;
+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() {
+        mLogger = getLogger();
+    }
+
+    /**
+     * Initialize.  Called once for each request.
+     *
+     * @see org.roller.model.PagePlugin#init(WebsiteData, Object, String baseUrl, org.apache.velocity.context.Context)
+     */
+    public void init(WebsiteData website, Object servletContext, String baseUrl, Context ctx) throws RollerException {
+        if (mLogger.isDebugEnabled()) {
+            mLogger.debug(getClass().getName() + "; version:  " + getVersion() + "; base version " + baseVersion);
+        }
+    }
+
+    /**
+     * Apply plugin to content of specified WeblogEntry.
+     *
+     * @param entry           WeblogEntry to which plugin should be applied.
+     * @param skipFlag        ignored for this plugin.
+     * @return Results of applying plugin to entry.
+     * @see org.roller.model.PagePlugin#render(org.roller.pojos.WeblogEntryData, boolean)
+     */
+    public String render(WeblogEntryData entry, boolean skipFlag) {
+        return render(entry.getText());
+    }
+
+    /**
+     * 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.roller.model.PagePlugin#render(String)
+     */
+    public String render(String str) {
+        Pattern pattern = getPattern();
+        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();
+    }
+
+    /**
+     * Returns the human-friendly name of this Plugin. This is what users will see.
+     *
+     * @return The human-friendly name of this Plugin.
+     * @see org.roller.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.roller.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");
+        } catch (UnsupportedEncodingException uex) {
+            // By Java spec, this should never actually occur for UTF-8.  If it does, we barf bitterly.
+            throw new RuntimeException(uex);
+        }
+    }
+}

Added: incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/WikipediaLinkPlugin.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/WikipediaLinkPlugin.java?rev=355536&view=auto
==============================================================================
--- incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/WikipediaLinkPlugin.java (added)
+++ incubator/roller/trunk/contrib/plugins/src/org/roller/presentation/velocity/plugins/search/WikipediaLinkPlugin.java Fri Dec  9 08:54:52 2005
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2004,2005
+ * Anil R. Gangolli. All rights reserved.
+ *
+ * Distributed with Roller Weblogger under the terms of the Roller License.
+ */
+
+package org.roller.presentation.velocity.plugins.search;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.roller.model.PagePlugin;
+
+import java.text.MessageFormat;
+import java.util.regex.Pattern;
+
+/**
+ * Wikipedia Link Plugin.  This plugin provides a convenient way to write wikipedia search links.
+ * <p/>
+ * The plugin will replace strings of the form <code>wikipedia:"link text"{search text}</code> with a link that performs
+ * a Wikipedia search.  The link will have the visible text "link text" and an href for the Google search.  You may omit
+ * the <code>{search text}</code> portion, and the link text will be used as the search text.   You can also use an
+ * exclamation point (<code>!</code>) instead of the colon (<code>:</code>), to get a lucky (&quot;lucky&quot;) search,
+ * which takes the user directly to the best ranked match.
+ *
+ * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
+ * @version 2.1
+ */
+public class WikipediaLinkPlugin extends SearchPluginBase implements PagePlugin {
+    private static final String version = "2.1";
+    private static final Pattern pattern = Pattern.compile("wikipedia([:!])\"(.*?)\"(?:\\{(.*?)\\})?");
+    private static final MessageFormat linkFormat = new MessageFormat("<a href=\"http://www.wikipedia.org/wiki/Special:Search?search={3}\">{2}</a>");
+    private static final MessageFormat luckyLinkFormat = new MessageFormat("<a href=\"http://www.wikipedia.org/wiki/Special:Search?search={3}&go=Go\">{2}</a>");
+
+    private static final Log mLogger = LogFactory.getFactory().getInstance(WikipediaLinkPlugin.class);
+
+    public WikipediaLinkPlugin() {
+    }
+
+    public String getName() {
+        return "Wikipedia Search Links";
+    }
+
+    public String getDescription() {
+        return "Replace wikipedia:&quot;link text&quot;{search text} with a link that performs a wikipedia search.  With ! instead of :," + "creates a &quot;lucky&quot; search, going directly to the first result.  With {search text} omitted, uses link text as the value of the search text.";
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public Pattern getPattern() {
+        return pattern;
+    }
+
+    public MessageFormat getLinkFormat() {
+        return linkFormat;
+    }
+
+    public MessageFormat getLuckyLinkFormat() {
+        return luckyLinkFormat;
+    }
+
+    public Log getLogger() {
+        return mLogger;
+    }
+}