KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > plugins > SearchPluginBase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.plugins;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.roller.RollerException;
23 import org.apache.roller.pojos.WeblogEntryData;
24 import org.apache.roller.pojos.WebsiteData;
25
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.net.URLEncoder JavaDoc;
28 import java.text.FieldPosition JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.regex.Matcher JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33
34 /**
35  * Implements the common portion of search link plugins.
36  *
37  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</a>
38  * @version 2.1
39  */

40 public abstract class SearchPluginBase {
41     private String JavaDoc baseVersion = "2.1";
42     private Log mLogger;
43
44     /**
45      * Instantiation is per request.
46      */

47     public SearchPluginBase() {
48         mLogger = getLogger();
49     }
50
51     /**
52      * Initialize. Called once for each request.
53      *
54      * @see org.apache.roller.model.PagePlugin#init(WebsiteData, Object, String baseUrl, org.apache.velocity.context.Context)
55      */

56     public void init(WebsiteData website) throws RollerException {
57         if (mLogger.isDebugEnabled()) {
58             mLogger.debug(getClass().getName() + "; version: " + getVersion() + "; base version " + baseVersion);
59         }
60     }
61     
62
63     /**
64      * Apply plugin to content of specified String.
65      *
66      * @param str String to which plugin should be applied.
67      * @return Results of applying plugin to string.
68      * @see org.apache.roller.model.PagePlugin#render(String)
69      */

70     public String JavaDoc render(WeblogEntryData entry, String JavaDoc str) {
71         Pattern JavaDoc pattern = getPattern();
72         Matcher JavaDoc m = pattern.matcher(str);
73         StringBuffer JavaDoc result = new StringBuffer JavaDoc(str.length() + 128); // rough guess at a reasonable length
74
Object JavaDoc[] args = new Object JavaDoc[]{"", "", null, null};
75         while (m.find()) {
76             // parse out the parts of the match
77
String JavaDoc type = m.group(1);
78             boolean feelinLucky = type.equals("!"); // are ya feelin lucky? are ya punk?
79
String JavaDoc linkText = m.group(2);
80             String JavaDoc searchText = m.group(3);
81             if (searchText == null || searchText.length() == 0) {
82                 searchText = linkText;
83             }
84
85             // URL-encode the search text
86
String JavaDoc encodedSearchText = encodeSearchText(searchText);
87
88             // form the replacement string
89
MessageFormat JavaDoc linkFormat = feelinLucky ? getLuckyLinkFormat() : getLinkFormat();
90             StringBuffer JavaDoc replacement = new StringBuffer JavaDoc(128);
91             args[2] = linkText;
92             args[3] = encodedSearchText;
93             linkFormat.format(args, replacement, new FieldPosition JavaDoc(0));
94
95             // append replacement
96
m.appendReplacement(result, replacement.toString());
97         }
98         m.appendTail(result);
99
100         return result.toString();
101     }
102
103     /**
104      * Returns the human-friendly name of this Plugin. This is what users will see.
105      *
106      * @return The human-friendly name of this Plugin.
107      * @see org.apache.roller.model.PagePlugin#getName()
108      */

109     public abstract String JavaDoc getName();
110
111     /**
112      * Briefly describes the function of the Plugin. May contain HTML.
113      *
114      * @return A brief description of the Plugin.
115      * @see org.apache.roller.model.PagePlugin#getDescription()
116      */

117     public abstract String JavaDoc getDescription();
118
119     /**
120      * Return the logger for this class.
121      *
122      * @return the logger for this class.
123      */

124     protected abstract Log getLogger();
125
126     /**
127      * Return the implementation version.
128      *
129      * @return the implementation version.
130      */

131     protected abstract String JavaDoc getVersion();
132
133     /**
134      * Get the regexp pattern for finding search links in the input text. Three matching groups are expected: (1) The
135      * lucky or not indicator (either <code>!</code> or <code>:</code>) (2) the link text (3) the search text (optional,
136      * defaults to the link text).
137      *
138      * @return the regexp pattern for finding search links in the input text
139      */

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

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

160     protected abstract MessageFormat JavaDoc getLuckyLinkFormat();
161
162
163     // Private helper to URL encode the search text.
164
private String JavaDoc encodeSearchText(String JavaDoc searchText) {
165         // URL encode the searchtext
166
try {
167             return URLEncoder.encode(searchText, "UTF-8");
168         } catch (UnsupportedEncodingException JavaDoc uex) {
169             // By Java spec, this should never actually occur for UTF-8. If it does, we barf bitterly.
170
throw new RuntimeException JavaDoc(uex);
171         }
172     }
173     
174 }
175
Popular Tags