1 18 19 package org.apache.roller.ui.rendering.plugins; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.velocity.context.Context; 24 import org.apache.roller.RollerException; 25 import org.apache.roller.config.RollerConfig; 26 import org.apache.roller.model.BookmarkManager; 27 import org.apache.roller.model.RollerFactory; 28 import org.apache.roller.pojos.BookmarkData; 29 import org.apache.roller.pojos.WeblogEntryData; 30 import org.apache.roller.pojos.WebsiteData; 31 import org.apache.roller.model.WeblogEntryPlugin; 32 33 import java.io.UnsupportedEncodingException ; 34 import java.net.URLEncoder ; 35 import java.text.FieldPosition ; 36 import java.text.MessageFormat ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Map ; 41 import java.util.regex.Matcher ; 42 import java.util.regex.Pattern ; 43 import java.util.regex.PatternSyntaxException ; 44 import javax.servlet.ServletConfig ; 45 46 101 public class TopicTagPlugin implements WeblogEntryPlugin 102 { 103 private static final String version = "0.3"; 104 private static final Log mLogger = LogFactory.getFactory().getInstance(TopicTagPlugin.class); 105 106 107 private String defaultTopicBookmarkName = "Default Topic Site"; 109 private String defaultTopicSite = "http://www.technorati.com/tag"; 110 private String tagRegexWithBookmark = "topic:\\{(.*?)\\}\\[(.*?)\\]"; 111 private String tagRegexWithoutBookmark = "topic:\\[(.*?)\\]"; 112 private String linkFormatString = "<a rel=\"tag\" HREF=\"{0}{1}\">{2}</a>"; 113 114 private Pattern tagPatternWithBookmark; 116 private Pattern tagPatternWithoutBookmark; 117 private MessageFormat linkFormat; 118 119 private Map userBookmarks; 122 123 124 public TopicTagPlugin() 125 { 126 } 127 128 135 public void init(WebsiteData website) throws RollerException 136 { 137 if (mLogger.isDebugEnabled()) 138 { 139 mLogger.debug("TopicTagPlugin v. " + version); 140 } 141 142 initializeProperties(); 144 145 userBookmarks = buildBookmarkMap(website); 147 148 BookmarkData defaultTopicBookmark = (BookmarkData) userBookmarks.get(defaultTopicBookmarkName); 150 if (defaultTopicBookmark != null) defaultTopicSite = defaultTopicBookmark.getUrl(); 151 152 if (!defaultTopicSite.endsWith("/")) 154 { 155 defaultTopicSite += "/"; 156 } 157 158 try 160 { 161 tagPatternWithBookmark = Pattern.compile(tagRegexWithBookmark); 162 } 163 catch (PatternSyntaxException e) 164 { 165 throw new RollerException("Invalid regular expression for topic tags with bookmark '" + 166 tagRegexWithBookmark + "': " + e.getMessage()); 167 } 168 int groupCount = tagPatternWithBookmark.matcher("").groupCount(); 169 if (groupCount != 2) 170 { 171 throw new RollerException("Regular expression for topic tags with bookmark '" + tagRegexWithBookmark + 172 "' contains wrong number of capture groups. Must have exactly 2. Contains " + groupCount); 173 } 174 175 try 176 { 177 tagPatternWithoutBookmark = Pattern.compile(tagRegexWithoutBookmark); 178 } 179 catch (PatternSyntaxException e) 180 { 181 throw new RollerException("Invalid regular expression for topic tags without bookmark '" + 182 tagRegexWithoutBookmark + "': " + e.getMessage()); 183 } 184 groupCount = tagPatternWithoutBookmark.matcher("").groupCount(); 185 if (groupCount != 1) 186 { 187 throw new RollerException("Regular expression for topic tags without bookmark '" + tagRegexWithoutBookmark + 188 "' contains wrong number of capture groups. Must have exactly 1. Contains " + groupCount); 189 } 190 191 setLinkFormat(new MessageFormat (linkFormatString)); 193 } 194 195 202 public String render(WeblogEntryData entry, String str) 203 { 204 String entryText = str; 205 StringBuffer result = new StringBuffer (entryText.length()); 206 MessageFormat fmt = getLinkFormat(); 207 208 Matcher m = tagPatternWithBookmark.matcher(entryText); 210 while (m.find()) 211 { 212 String bookmark = m.group(1); 213 String tag = m.group(2); 214 String site = getBookmarkSite(bookmark); 215 if (site == null) 216 { 217 site = getDefaultTopicSite(); 218 } 219 if (!site.endsWith("/")) 220 { 221 site += "/"; 222 } 223 String link = generateLink(fmt, site, tag); 224 m.appendReplacement(result, link); 225 } 226 m.appendTail(result); 227 228 entryText = result.toString(); 230 result = new StringBuffer (entryText.length()); 231 m = tagPatternWithoutBookmark.matcher(entryText); 232 while (m.find()) 233 { 234 String tag = m.group(1); 235 String site = getDefaultTopicSite(); 236 String link = generateLink(fmt, site, tag); 237 m.appendReplacement(result, link); 238 } 239 m.appendTail(result); 240 241 return result.toString(); 242 } 243 244 245 250 public String getName() 251 { 252 return "Topic Tags"; 254 } 255 256 261 public String getDescription() 262 { 263 return "Expands topic tags for <a HREF=\\'http://www.technorati.com\\'>Technorati</a> and similar sites. " + 265 "Topic tags are of the form <code>topic:{topicbookmark}[tag]</code>, where <code>topicbookmark</code> " + 266 "is the name of a bookmark whose URL will be used for the site name in the topic tag. " + 267 "If <code>{topicbookmark}</code> is omitted the plugin will use the URL of the <code>Default Topic Site</code> " + 268 "bookmark, if that is defined, otherwise http://www.technorati.com."; 269 } 270 271 280 protected String generateLink(MessageFormat fmt, String site, String tag) 281 { 282 StringBuffer sb = new StringBuffer (site.length() + tag.length() + getLinkFormatString().length()); 284 fmt.format(new Object []{site, urlEncode(tag), tag}, sb, new FieldPosition (0)); 285 return sb.toString(); 286 } 287 288 295 protected String getBookmarkSite(String bookmarkName) 296 { 297 BookmarkData bookmark = (BookmarkData) getUserBookmarks().get(bookmarkName); 298 return bookmark == null ? null : bookmark.getUrl(); 299 } 300 301 302 307 protected Map buildBookmarkMap(WebsiteData website) throws RollerException 308 { 309 Map bookmarkMap = new HashMap (); 310 if (RollerConfig.getBooleanProperty("plugins.topictag.ignoreBookmarks")) { 311 return bookmarkMap; 312 } 313 if (website == null) 314 { 315 mLogger.debug("Init called without website. Skipping bookmark initialization."); 316 } 317 else 318 { 319 BookmarkManager bMgr = RollerFactory.getRoller().getBookmarkManager(); 320 List bookmarks = bMgr.getBookmarks(bMgr.getRootFolder(website), true); 321 322 for (Iterator i = bookmarks.iterator(); i.hasNext();) 323 { 324 BookmarkData b = (BookmarkData) i.next(); 325 bookmarkMap.put(b.getName(), b); 326 } 327 } 328 return bookmarkMap; 329 } 330 331 332 private void initializeProperties() 334 { 335 setDefaultTopicBookmarkName(getSetting("defaultTopicBookmarkName", getDefaultTopicBookmarkName())); 336 setDefaultTopicSite(getSetting("defaultTopicSite", getDefaultTopicSite())); 337 setTagRegexWithBookmark(getSetting("tagRegexWithBookmark", getTagRegexWithBookmark())); 338 setTagRegexWithoutBookmark(getSetting("tagRegexWithoutBookmark", getTagRegexWithoutBookmark())); 339 setLinkFormatString(getSetting("linkFormatString", getLinkFormatString())); 340 } 341 342 private String getSetting(String propName, String defaultValue) 343 { 344 String fullPropName = "plugins.topictag." + propName; 345 String val = (String ) RollerConfig.getProperty(fullPropName); 346 return (val != null) ? val : defaultValue; 347 } 348 349 350 private String urlEncode(String text) 352 { 353 try 355 { 356 return URLEncoder.encode(text, "UTF-8"); 357 } 358 catch (UnsupportedEncodingException uex) 359 { 360 throw new RuntimeException (uex); 362 } 363 } 364 365 366 368 369 public String getDefaultTopicSite() 370 { 371 return defaultTopicSite; 372 } 373 374 public void setDefaultTopicSite(String defaultTopicSite) 375 { 376 this.defaultTopicSite = defaultTopicSite; 377 } 378 379 public String getTagRegexWithBookmark() 380 { 381 return tagRegexWithBookmark; 382 } 383 384 public void setTagRegexWithBookmark(String tagRegexWithBookmark) 385 { 386 this.tagRegexWithBookmark = tagRegexWithBookmark; 387 } 388 389 public String getTagRegexWithoutBookmark() 390 { 391 return tagRegexWithoutBookmark; 392 } 393 394 public void setTagRegexWithoutBookmark(String tagRegexWithoutBookmark) 395 { 396 this.tagRegexWithoutBookmark = tagRegexWithoutBookmark; 397 } 398 399 public String getLinkFormatString() 400 { 401 return linkFormatString; 402 } 403 404 public void setLinkFormatString(String linkFormatString) 405 { 406 this.linkFormatString = linkFormatString; 407 } 408 409 public MessageFormat getLinkFormat() 410 { 411 return linkFormat; 412 } 413 414 public void setLinkFormat(MessageFormat linkFormat) 415 { 416 this.linkFormat = linkFormat; 417 } 418 419 public Pattern getTagPatternWithBookmark() 420 { 421 return tagPatternWithBookmark; 422 } 423 424 public void setTagPatternWithBookmark(Pattern tagPatternWithBookmark) 425 { 426 this.tagPatternWithBookmark = tagPatternWithBookmark; 427 } 428 429 public Pattern getTagPatternWithoutBookmark() 430 { 431 return tagPatternWithoutBookmark; 432 } 433 434 public void setTagPatternWithoutBookmark(Pattern tagPatternWithoutBookmark) 435 { 436 this.tagPatternWithoutBookmark = tagPatternWithoutBookmark; 437 } 438 439 public String getDefaultTopicBookmarkName() 440 { 441 return defaultTopicBookmarkName; 442 } 443 444 public void setDefaultTopicBookmarkName(String defaultTopicBookmarkName) 445 { 446 this.defaultTopicBookmarkName = defaultTopicBookmarkName; 447 } 448 449 public Map getUserBookmarks() 450 { 451 return userBookmarks; 452 } 453 454 public void setUserBookmarks(Map userBookmarks) 455 { 456 this.userBookmarks = userBookmarks; 457 } 458 459 } 460 | Popular Tags |