1 5 package org.roller.presentation.velocity.plugins.bookmarks; 6 7 import org.apache.commons.lang.StringEscapeUtils; 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.apache.velocity.context.Context; 11 import org.roller.RollerException; 12 import org.roller.model.BookmarkManager; 13 import org.roller.model.RollerFactory; 14 import org.roller.pojos.BookmarkData; 15 import org.roller.pojos.FolderData; 16 import org.roller.pojos.WeblogEntryData; 17 import org.roller.presentation.RollerRequest; 18 import org.roller.presentation.velocity.PagePlugin; 19 import java.util.regex.PatternSyntaxException ; 20 import java.util.regex.Matcher ; 21 import java.util.regex.Pattern ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 29 public class BookmarkPlugin implements PagePlugin 30 { 31 protected String name = "Bookmark Linker"; 32 protected String description = "Automatically uses your Bookmarks to " + 33 "create links. Simply use the Name of a Bookmark and it will be " + 34 "converted into a hyperlink using the Bookmark's URL."; 35 36 private static Log mLogger = 37 LogFactory.getFactory().getInstance(BookmarkPlugin.class); 38 39 public BookmarkPlugin() 40 { 41 mLogger.debug("BookmarkPlugin instantiated."); 42 } 43 44 public String toString() { return name; } 45 46 49 public void init(RollerRequest rreq, Context ctx) throws RollerException 50 { 51 } 52 53 58 public String render(String text) 59 { 60 return text; 61 } 62 63 public String render(WeblogEntryData entry, boolean skipFlag) 64 { 65 String text = entry.getText(); 66 try 67 { 68 BookmarkManager bMgr = RollerFactory.getRoller().getBookmarkManager(); 69 FolderData rootFolder = bMgr.getRootFolder(entry.getWebsite()); 70 text = matchBookmarks(text, rootFolder); 71 text = lookInFolders(text, rootFolder.getFolders()); 72 } 73 catch (RollerException e) 74 { 75 mLogger.warn(e); 78 } 79 return text; 80 } 81 82 90 private String lookInFolders(String text, Collection folders) 91 { 92 Iterator it = folders.iterator(); 93 while (it.hasNext()) 94 { 95 FolderData folder = (FolderData)it.next(); 96 text = matchBookmarks(text, folder); 97 98 try 99 { 100 if (!folder.getFolders().isEmpty()) 101 { 102 lookInFolders(text, folder.getFolders()); 103 } 104 } 105 catch (RollerException e) 106 { 107 mLogger.error("Error getting child Folders"); 108 } 109 } 110 return text; 111 } 112 113 private String matchBookmarks(String text, FolderData folder) 114 { 115 Iterator bookmarks = folder.getBookmarks().iterator(); 116 String workingText = text; 117 while (bookmarks.hasNext()) 118 { 119 BookmarkData bookmark = (BookmarkData)bookmarks.next(); 120 String bkDescription = bookmark.getDescription(); 121 if (bkDescription == null) bkDescription = ""; 122 String bookmarkLink = "<a HREF=\"" + 123 bookmark.getUrl() + "\" title=\"" + 124 bkDescription + "\">" + 125 bookmark.getName() + "</a>"; 126 try 127 { 128 String regEx = "(<a(?:\\s.*?)??/>)|(<a(?:\\s.*?)??>)|(</a(?:\\s.*?)??>)|(?:\\b)(" + bookmark.getName() + ")(?:\\b)"; 135 Matcher m = Pattern.compile(regEx).matcher(workingText); 136 StringBuffer textBuf = new StringBuffer (workingText.length()); 137 int inLink = 0; 138 while (m.find()) 139 { 140 if (m.group(1) != null) 141 { 142 } 144 else if (m.group(2) != null) 145 { 146 inLink++; 148 } 149 else if (m.group(3) != null) 150 { 151 if (inLink > 0) inLink--; 153 } 154 else if (m.group(4) != null) 155 { 156 if (inLink == 0) m.appendReplacement(textBuf, bookmarkLink); 158 } 159 } 161 m.appendTail(textBuf); 162 workingText = textBuf.toString(); 163 } 164 catch (PatternSyntaxException e) 165 { 166 mLogger.warn("Failed to substitute for bookmark [" + bookmark.getName() + "] due to regular expression characters."); 168 } 169 } 170 return workingText.toString(); 171 } 172 173 public String getName() { return name; } 174 public String getDescription() { return StringEscapeUtils.escapeJavaScript(description); } 175 } 176 | Popular Tags |