1 18 19 package org.apache.roller.ui.rendering.plugins; 20 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 import org.apache.commons.lang.StringEscapeUtils; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.roller.RollerException; 30 import org.apache.roller.model.RollerFactory; 31 import org.apache.roller.model.UserManager; 32 import org.apache.roller.model.WeblogEntryPlugin; 33 import org.apache.roller.pojos.WeblogEntryData; 34 import org.apache.roller.pojos.WeblogTemplate; 35 import org.apache.roller.pojos.WebsiteData; 36 37 38 46 public class AcronymsPlugin implements WeblogEntryPlugin { 47 48 private static final Log mLogger = LogFactory.getLog(AcronymsPlugin.class); 49 50 protected String name = "Acronyms"; 51 protected String description = "Expands acronyms defined in _acronym page. " + 52 "Example: definition 'HTML=Hyper Text Markup Language' " + 53 "becomes <acronym title='Hyper Text Markup Language'>HTML</acronym>. " + 54 "You must create an " + 55 "<a HREF='page.do?method=editPages&rmik=tabbedmenu.website.pages'>" + 56 "_acronym page</a> to use Acronyms."; 57 58 59 public AcronymsPlugin() { 60 super(); 61 mLogger.debug("AcronymsPlugin instantiated."); 62 } 63 64 65 public String getName() { 66 return name; 67 } 68 69 70 public String getDescription() { 71 return StringEscapeUtils.escapeJavaScript(description); 72 } 73 74 75 public void init(WebsiteData website) throws RollerException {} 76 77 78 public String render(WeblogEntryData entry, String str) { 79 String text = str; 80 81 if (mLogger.isDebugEnabled()) { 82 mLogger.debug("render(entry = "+entry.getId()+")"); 83 } 84 85 88 Properties acronyms = loadAcronyms(entry.getWebsite()); 89 mLogger.debug("acronyms.size()=" + acronyms.size()); 90 if (acronyms.size() == 0) { 91 return text; 92 } 93 94 97 Pattern [] acronymPatterns = new Pattern [acronyms.size()]; 98 String [] acronymTags = new String [acronyms.size()]; 99 int count = 0; 100 for (Iterator iter = acronyms.keySet().iterator(); iter.hasNext();) { 101 String acronym = (String ) iter.next(); 102 acronymPatterns[count] = Pattern.compile("\\b" + acronym + "\\b"); 103 mLogger.debug("match '" + acronym + "'"); 104 acronymTags[count] = 105 "<acronym title=\"" 106 + acronyms.getProperty(acronym) 107 + "\">" 108 + acronym 109 + "</acronym>"; 110 count++; 111 } 112 113 if (acronymPatterns == null || acronymPatterns.length == 0) { 115 return text; 116 } 117 118 return matchAcronyms(text, acronymPatterns, acronymTags); 119 } 120 121 122 128 private Properties loadAcronyms(WebsiteData website) { 129 Properties acronyms = new Properties (); 130 try { 131 UserManager userMgr = RollerFactory.getRoller().getUserManager(); 132 WeblogTemplate acronymsPage = userMgr.getPageByName( 133 website, "_acronyms"); 134 if (acronymsPage != null) { 135 acronyms = parseAcronymPage(acronymsPage, acronyms); 136 } 137 } catch (RollerException e) { 138 mLogger.warn(e); 140 } 141 return acronyms; 142 } 143 144 145 153 private String matchAcronyms(String text, Pattern [] acronymPatterns, String [] acronymTags) { 154 if (mLogger.isDebugEnabled()) { 155 mLogger.debug("matchAcronyms("+text+")"); 156 } 157 158 Matcher matcher = null; 159 for (int i=0; i<acronymPatterns.length; i++) { 160 matcher = acronymPatterns[i].matcher(text); 161 text = matcher.replaceAll(acronymTags[i]); 162 } 163 return text; 164 } 165 166 173 private Properties parseAcronymPage(WeblogTemplate acronymPage, Properties acronyms) { 174 String rawAcronyms = acronymPage.getContents(); 175 176 if (mLogger.isDebugEnabled()) { 177 mLogger.debug("parsing _acronyms template: \n'"+rawAcronyms+"'"); 178 } 179 180 String regex = "\n"; String [] lines = rawAcronyms.split(regex); 182 183 if (lines != null) { 184 for (int i = 0; i < lines.length; i++) { 185 int index = lines[i].indexOf('='); 186 if (index > 0) { 187 String key = lines[i].substring(0, index).trim(); 188 String value = 189 lines[i].substring(index + 1, lines[i].length()).trim(); 190 acronyms.setProperty(key, value); 191 } 192 } 193 } 194 195 return acronyms; 196 } 197 198 } 199 | Popular Tags |