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