1 31 package org.blojsom.plugin.macro; 32 33 import org.blojsom.blog.Blog; 34 import org.blojsom.blog.Entry; 35 import org.blojsom.plugin.Plugin; 36 import org.blojsom.plugin.PluginException; 37 import org.blojsom.util.BlojsomUtils; 38 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.util.Map ; 42 import java.util.regex.Matcher ; 43 import java.util.regex.Pattern ; 44 45 53 public class MacroExpansionPlugin implements Plugin { 54 55 58 private static final String MACRO_REGEX = "(\\$[^\\$]*\\$)"; 59 60 63 public MacroExpansionPlugin() { 64 } 65 66 71 public void init() throws PluginException { 72 } 73 74 81 private String replaceMacros(String content, Map macros) { 82 if (BlojsomUtils.checkNullOrBlank(content)) { 83 return content; 84 } 85 86 Pattern macroPattern = Pattern.compile(MACRO_REGEX); 87 Matcher matcher = macroPattern.matcher(content); 88 89 while (matcher.find()) { 90 String token = matcher.group(); 91 String macro = token.substring(1, token.length() - 1); 92 if (macros.containsKey(macro)) { 93 content = BlojsomUtils.replace(content, token, (String ) macros.get(macro)); 94 } 95 } 96 97 return content; 98 } 99 100 111 public Entry[] process(HttpServletRequest httpServletRequest, 112 HttpServletResponse httpServletResponse, 113 Blog blog, 114 Map context, 115 Entry[] entries) throws PluginException { 116 Map macros = MacroExpansionUtilities.readMacros(blog); 117 118 for (int i = 0; i < entries.length; i++) { 119 Entry entry = entries[i]; 120 entry.setTitle(replaceMacros(entry.getTitle(), macros)); 121 entry.setDescription(replaceMacros(entry.getDescription(), macros)); 122 } 123 124 return entries; 125 } 126 127 132 public void cleanup() throws PluginException { 133 } 134 135 140 public void destroy() throws PluginException { 141 } 142 } 143 144 145 | Popular Tags |