1 6 package org.roller.presentation.velocity.plugins.acronyms; 7 8 import org.apache.commons.lang.StringEscapeUtils; 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.apache.velocity.context.Context; 12 import org.roller.RollerException; 13 import org.roller.model.RollerFactory; 14 import org.roller.model.UserManager; 15 import org.roller.pojos.PageData; 16 import org.roller.pojos.WeblogEntryData; 17 import org.roller.pojos.WebsiteData; 18 import org.roller.presentation.RollerRequest; 19 import org.roller.presentation.velocity.PagePlugin; 20 21 import java.util.Iterator ; 22 import java.util.Properties ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 34 public class AcronymsPlugin implements PagePlugin 35 { 36 39 private static final Log mLogger = LogFactory.getLog(AcronymsPlugin.class); 40 41 44 protected String name = "Acronyms"; 45 protected String description = "Expands acronyms defined in _acronym page. " + 46 "Example: definition 'HTML=Hyper Text Markup Language' " + 47 "becomes <acronym title='Hyper Text Markup Language'>HTML</acronym>. " + 48 "You must create an " + 49 "<a HREF='page.do?method=editPages&rmik=tabbedmenu.website.pages'>" + 50 "_acronym page</a> to use Acronyms."; 51 52 55 public AcronymsPlugin() 56 { 57 super(); 58 mLogger.debug("AcronymsPlugin instantiated."); 59 } 60 61 64 public void init(RollerRequest rreq, Context ctx) throws RollerException 65 { 66 if (mLogger.isDebugEnabled()) { 67 mLogger.debug("init( rreq = "+rreq+", ctx = "+ctx+" )"); 68 } 69 } 70 71 77 private Properties loadAcronyms(WebsiteData website) 78 { 79 Properties acronyms = new Properties (); 80 try 81 { 82 UserManager userMgr = RollerFactory.getRoller().getUserManager(); 83 PageData acronymsPage = userMgr.getPageByName( 84 website, "_acronyms"); 85 if (acronymsPage != null) 86 { 87 acronyms = parseAcronymPage(acronymsPage, acronyms); 88 } 89 } 90 catch (RollerException e) 91 { 92 mLogger.warn(e); 94 } 95 return acronyms; 96 } 97 98 101 public String render(WeblogEntryData entry, boolean skipFlag) 102 { 103 String text = entry.getText(); 104 105 if (mLogger.isDebugEnabled()) { 106 mLogger.debug("render( entry = "+entry.getId()+", skipFlag = "+skipFlag+" )"); 107 } 108 109 112 Properties acronyms = loadAcronyms(entry.getWebsite()); 113 mLogger.debug("acronyms.size()=" + acronyms.size()); 114 if (acronyms.size() == 0) 115 { 116 return text; 117 } 118 119 122 Pattern [] acronymPatterns = new Pattern [acronyms.size()]; 123 String [] acronymTags = new String [acronyms.size()]; 124 int count = 0; 125 for (Iterator iter = acronyms.keySet().iterator(); iter.hasNext();) 126 { 127 String acronym = (String ) iter.next(); 128 acronymPatterns[count] = Pattern.compile("\\b" + acronym + "\\b"); 129 mLogger.debug("match '" + acronym + "'"); 130 acronymTags[count] = 131 "<acronym title=\"" 132 + acronyms.getProperty(acronym) 133 + "\">" 134 + acronym 135 + "</acronym>"; 136 count++; 137 } 138 139 146 147 if (acronymPatterns == null || acronymPatterns.length == 0) { 149 return text; 150 } 151 152 return matchAcronyms(text, acronymPatterns, acronymTags); 153 } 154 155 159 public String render(String text) 160 { 161 return text; 162 } 163 164 167 public String toString() 168 { 169 return name; 170 } 171 172 180 private String matchAcronyms(String text, Pattern [] acronymPatterns, String [] acronymTags) 181 { 182 if (mLogger.isDebugEnabled()) { 183 mLogger.debug("matchAcronyms("+text+")"); 184 } 185 186 Matcher matcher = null; 187 for (int i=0; i<acronymPatterns.length; i++) 188 { 189 matcher = acronymPatterns[i].matcher(text); 190 text = matcher.replaceAll(acronymTags[i]); 191 } 192 return text; 193 } 194 195 202 private Properties parseAcronymPage(PageData acronymPage, Properties acronyms) 203 { 204 String rawAcronyms = acronymPage.getTemplate(); 205 206 if (mLogger.isDebugEnabled()) 207 { 208 mLogger.debug("parsing _acronyms template: \n'"+rawAcronyms+"'"); 209 } 210 211 String regex = "\n"; String [] lines = rawAcronyms.split(regex); 213 214 if (lines != null) 215 { 216 for (int i = 0; i < lines.length; i++) 217 { 218 int index = lines[i].indexOf('='); 219 if (index > 0) 220 { 221 String key = lines[i].substring(0, index).trim(); 222 String value = 223 lines[i].substring(index + 1, lines[i].length()).trim(); 224 acronyms.setProperty(key, value); 225 } 226 } 227 } 228 229 return acronyms; 230 } 231 232 public String getName() { return name; } 233 public String getDescription() { return StringEscapeUtils.escapeJavaScript(description); } 234 } 235 | Popular Tags |