1 18 19 package org.apache.roller.ui.rendering.plugins; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 import java.util.regex.PatternSyntaxException ; 27 import org.apache.commons.lang.StringEscapeUtils; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.roller.RollerException; 31 import org.apache.roller.model.BookmarkManager; 32 import org.apache.roller.model.RollerFactory; 33 import org.apache.roller.model.WeblogEntryPlugin; 34 import org.apache.roller.pojos.BookmarkData; 35 import org.apache.roller.pojos.FolderData; 36 import org.apache.roller.pojos.WeblogEntryData; 37 import org.apache.roller.pojos.WebsiteData; 38 39 40 43 public class BookmarkPlugin implements WeblogEntryPlugin { 44 45 private static Log mLogger = LogFactory.getLog(BookmarkPlugin.class); 46 47 protected String name = "Bookmark Linker"; 48 protected String description = "Automatically uses your Bookmarks to " + 49 "create links. Simply use the Name of a Bookmark and it will be " + 50 "converted into a hyperlink using the Bookmark's URL."; 51 52 53 public BookmarkPlugin() { 54 mLogger.debug("BookmarkPlugin instantiated."); 55 } 56 57 58 public String getName() { 59 return name; 60 } 61 62 63 public String getDescription() { 64 return StringEscapeUtils.escapeJavaScript(description); 65 } 66 67 68 public void init(WebsiteData website) throws RollerException {} 69 70 71 public String render(WeblogEntryData entry, String str) { 72 String text = str; 73 try { 74 BookmarkManager bMgr = RollerFactory.getRoller().getBookmarkManager(); 75 FolderData rootFolder = bMgr.getRootFolder(entry.getWebsite()); 76 text = matchBookmarks(text, rootFolder); 77 text = lookInFolders(text, rootFolder.getFolders()); 78 } catch (RollerException e) { 79 mLogger.warn(e); 82 } 83 return text; 84 } 85 86 87 95 private String lookInFolders(String text, Collection folders) { 96 Iterator it = folders.iterator(); 97 while (it.hasNext()) { 98 FolderData folder = (FolderData)it.next(); 99 text = matchBookmarks(text, folder); 100 101 try { 102 if (!folder.getFolders().isEmpty()) { 103 lookInFolders(text, folder.getFolders()); 104 } 105 } catch (RollerException e) { 106 mLogger.error("Error getting child Folders"); 107 } 108 } 109 return text; 110 } 111 112 113 private String matchBookmarks(String text, FolderData folder) { 114 Iterator bookmarks = folder.getBookmarks().iterator(); 115 String workingText = text; 116 while (bookmarks.hasNext()) { 117 BookmarkData bookmark = (BookmarkData)bookmarks.next(); 118 String bkDescription = bookmark.getDescription(); 119 if (bkDescription == null) bkDescription = ""; 120 String bookmarkLink = "<a HREF=\"" + 121 bookmark.getUrl() + "\" title=\"" + 122 bkDescription + "\">" + 123 bookmark.getName() + "</a>"; 124 try { 125 String regEx = "(<a(?:\\s.*?)??/>)|(<a(?:\\s.*?)??>)|(</a(?:\\s.*?)??>)|(?:\\b)(" + bookmark.getName() + ")(?:\\b)"; 132 Matcher m = Pattern.compile(regEx).matcher(workingText); 133 StringBuffer textBuf = new StringBuffer (workingText.length()); 134 int inLink = 0; 135 while (m.find()) { 136 if (m.group(1) != null) { 137 } else if (m.group(2) != null) { 139 inLink++; 141 } else if (m.group(3) != null) { 142 if (inLink > 0) inLink--; 144 } else if (m.group(4) != null) { 145 if (inLink == 0) m.appendReplacement(textBuf, bookmarkLink); 147 } 148 } 150 m.appendTail(textBuf); 151 workingText = textBuf.toString(); 152 } catch (PatternSyntaxException e) { 153 mLogger.warn("Failed to substitute for bookmark [" + bookmark.getName() + "] due to regular expression characters."); 155 } 156 } 157 return workingText.toString(); 158 } 159 160 } 161 | Popular Tags |