File Source: SearchPluginBase.java
/*
P/P * Method: org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase__static_init
*/
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.weblogger.business.plugins.entry;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.roller.weblogger.WebloggerException;
23 import org.apache.roller.weblogger.pojos.WeblogEntry;
24 import org.apache.roller.weblogger.pojos.Weblog;
25
26 import java.io.UnsupportedEncodingException;
27 import java.net.URLEncoder;
28 import java.text.FieldPosition;
29 import java.text.MessageFormat;
30 import java.util.Map;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
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 baseVersion = "2.1";
42 private Log mLogger;
43
44 /**
45 * Instantiation is per request.
46 */
/*
P/P * Method: void org.apache.roller.weblogger.business.plugins.entry.SearchPluginBase()
*
* Postconditions:
* this.baseVersion == &"2.1"
* init'ed(this.mLogger)
*/
47 public SearchPluginBase() {
48 mLogger = getLogger();
49 }
50
51 /**
52 * Initialize. Called once for each request.
53 *
54 * @see org.apache.roller.weblogger.model.PagePlugin#init(WebsiteData, Object, String baseUrl, org.apache.velocity.context.Context)
55 */
56 public void init(Weblog website) throws WebloggerException {
/*
P/P * Method: void init(Weblog)
*
* Preconditions:
* this.mLogger != null
* (soft) init'ed(this.baseVersion)
*
* Presumptions:
* java.lang.Object:getClass(...)@58 != null
*
* Test Vectors:
* org.apache.commons.logging.Log:isDebugEnabled(...)@57: {0}, {1}
*/
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.weblogger.model.PagePlugin#render(String)
69 */
70 public String render(WeblogEntry entry, String str) {
/*
P/P * Method: String render(WeblogEntry, String)
*
* Preconditions:
* str != null
*
* Presumptions:
* java.lang.String:length(...)@73 <= 4_294_967_167
* java.util.regex.Matcher:group(...)@77 != null
* java.util.regex.Pattern:compile(...)@43 != null
* java.util.regex.Pattern:matcher(...)@72 != null
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* return_value == &java.lang.StringBuffer:toString(...)
*
* Test Vectors:
* java.lang.String:equals(...)@78: {0}, {1}
* java.lang.String:length(...)@81: {1..232-1}, {0}
* java.util.regex.Matcher:find(...)@75: {0}, {1}
* java.util.regex.Matcher:group(...)@80: Addr_Set{null}, Inverse{null}
*/
71 Pattern pattern = getPattern();
72 Matcher m = pattern.matcher(str);
73 StringBuffer result = new StringBuffer(str.length() + 128); // rough guess at a reasonable length
74 Object[] args = new Object[]{"", "", null, null};
75 while (m.find()) {
76 // parse out the parts of the match
77 String type = m.group(1);
78 boolean feelinLucky = type.equals("!"); // are ya feelin lucky? are ya punk?
79 String linkText = m.group(2);
80 String searchText = m.group(3);
81 if (searchText == null || searchText.length() == 0) {
82 searchText = linkText;
83 }
84
85 // URL-encode the search text
86 String encodedSearchText = encodeSearchText(searchText);
87
88 // form the replacement string
89 MessageFormat linkFormat = feelinLucky ? getLuckyLinkFormat() : getLinkFormat();
90 StringBuffer replacement = new StringBuffer(128);
91 args[2] = linkText;
92 args[3] = encodedSearchText;
93 linkFormat.format(args, replacement, new FieldPosition(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.weblogger.model.PagePlugin#getName()
108 */
109 public abstract String 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.weblogger.model.PagePlugin#getDescription()
116 */
117 public abstract String 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 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 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 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 getLuckyLinkFormat();
161
162
163 // Private helper to URL encode the search text.
164 private String encodeSearchText(String searchText) {
165 // URL encode the searchtext
166 try {
/*
P/P * Method: String encodeSearchText(String)
*
* Postconditions:
* init'ed(return_value)
*/
167 return URLEncoder.encode(searchText, "UTF-8");
168 } catch (UnsupportedEncodingException uex) {
169 // By Java spec, this should never actually occur for UTF-8. If it does, we barf bitterly.
170 throw new RuntimeException(uex);
171 }
172 }
173
174 }
SofCheck Inspector Build Version : 2.18479
| SearchPluginBase.java |
2009-Jan-02 14:24:58 |
| SearchPluginBase.class |
2009-Sep-04 03:12:31 |