1 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 ; 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 40 public abstract class SearchPluginBase { 41 private String baseVersion = "2.1"; 42 private Log mLogger; 43 44 47 public SearchPluginBase() { 48 mLogger = getLogger(); 49 } 50 51 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 70 public String render(WeblogEntryData entry, String str) { 71 Pattern pattern = getPattern(); 72 Matcher m = pattern.matcher(str); 73 StringBuffer result = new StringBuffer (str.length() + 128); Object [] args = new Object []{"", "", null, null}; 75 while (m.find()) { 76 String type = m.group(1); 78 boolean feelinLucky = type.equals("!"); String linkText = m.group(2); 80 String searchText = m.group(3); 81 if (searchText == null || searchText.length() == 0) { 82 searchText = linkText; 83 } 84 85 String encodedSearchText = encodeSearchText(searchText); 87 88 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 m.appendReplacement(result, replacement.toString()); 97 } 98 m.appendTail(result); 99 100 return result.toString(); 101 } 102 103 109 public abstract String getName(); 110 111 117 public abstract String getDescription(); 118 119 124 protected abstract Log getLogger(); 125 126 131 protected abstract String getVersion(); 132 133 140 protected abstract Pattern getPattern(); 141 142 150 protected abstract MessageFormat getLinkFormat(); 151 152 160 protected abstract MessageFormat getLuckyLinkFormat(); 161 162 163 private String encodeSearchText(String searchText) { 165 try { 167 return URLEncoder.encode(searchText, "UTF-8"); 168 } catch (UnsupportedEncodingException uex) { 169 throw new RuntimeException (uex); 171 } 172 } 173 174 } 175 | Popular Tags |